Expression must be a modifiable lvalue

For some reason I can't get my addNewPurchase function to work. No matter which way I spin it, it will usually give me an error message stating that I need to have a modifiable lvalue, and if it does not give me that error then it makes the numbers extremely large. Just looking for a hint as to what I am doing wrong.

I want it to be able to add a new variable to the old one and then have the sum take the place when I display it. So if someone bought 2 books and then 3, I want the next display to say they bought 5 in total.

Member.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <string>
#include <iostream>
#include "member.h"

// Write code for the following member functions:
/*Constructor. The constructor should accept the person’s name and member identification number. 
						These values should be assigned to the object’s memberName and memberID data members. 
						The constructor should also assign 0 to numBooks and purchaseAmt.*/

memberType::memberType(string name, string id) {

	memberName = name;
	memberID = id;
	int numBooks = 0;
	double purchaseAmt = 0;

}

/*Accessor.	Appropriate accessor functions to get the values stored in an object’s memberName, 
					memberID, numBooks, and purchaseAmt data members.*/

string memberType::getName() {

	return memberName;

}

string memberType::getId() {

	return memberID;

}

int memberType::getNumBooks() {


	return numBooks;
}

double memberType::getAmt() {


	return purchaseAmt;
}

/*addNewPurchase. The addNewPurchase function should accept the number of books for a new purchase and 
				the total amount of the purchase in dollars as arguments. 
				The function should add these argument values to the existing numBooks and purchaseAmt data member.

*/

void memberType::addNewPurchase(int numOfBooks, double amount) {

	numBooks + numOfBooks = numBooks;
	purchaseAmt + amount = purchaseAmt;

}


Member.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#ifndef MEMBER_H
#define MEMBER_H
#include <string>
#include <iostream>

using namespace std;

//define the class members and member function prototypes here

class memberType {

private:

	string memberName;
	string memberID;
	int numBooks;
	double purchaseAmt;

public:

	memberType(string name, string id);
	string getName();
	string getId();
	int getNumBooks();
	double getAmt();

	void addNewPurchase(int numOfBooks, double amount);

};

#endif 


Client.Cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
#include <string>
#include <iomanip>
#include "member.h"

using namespace std;

int main() {

	string Name, ID;
	int Books;
	double Amt;

	cout << "Welcome! Thank you for choosing Enmanuel's Book Store.\n";

	//Promput user for Name and ID

	cout << "Please enter your Member Name: \n";
	cin >> Name;
	cout << endl;

	cout << "Please enter your Member ID: \n";
	cin >> ID;
	cout << endl;

	//Creates the memberType object

	memberType obj(Name, ID);

	//Prompt user for Number of Books and Amount

	cout << "Please enter the amount of books you are purchasing: \n";
	cin >> Books;
	cout << endl;

	cout << "Please enter the cost for all of your books: \n";
	cin >> Amt;
	cout << endl;

	//Displays member info as well as purchase info

	cout << "Your Member Name is: " << obj.getName() << endl;
	cout << "Your Member ID is: " << obj.getId() << endl;
	cout << "The number of books you purchased is: " << obj.getNumBooks() << endl;
	cout << "The total cost of your purchase is:  " << obj.getAmt() << endl;

	cout << endl;

	//Second Purchase

	cout << "Please enter the amount of books you are purchasing: \n";
	cin >> Books;
	cout << endl;

	cout << "Please enter the cost for all of your books: \n";
	cin >> Amt;
	cout << endl;

	obj.addNewPurchase(Books, Amt);

	cout << "Your Member Name is: " << obj.getName() << endl;
	cout << "Your Member ID is: " << obj.getId() << endl;
	cout << "The number of books you purchased is: " << obj.getNumBooks() << endl;
	cout << "The total cost of your purchase is:  " << obj.getAmt() << endl;

	cout << endl;

	//Third Purchase

	cout << "Please enter the amount of books you are purchasing: \n";
	cin >> Books;
	cout << endl;

	cout << "Please enter the cost for all of your books: \n";
	cin >> Amt;
	cout << endl;

	obj.addNewPurchase(Books, Amt);

	cout << "Your Member Name is: " << obj.getName() << endl;
	cout << "Your Member ID is: " << obj.getId() << endl;
	cout << "The number of books you purchased is: " << obj.getNumBooks() << endl;
	cout << "The total cost of your purchase is:  " << obj.getAmt() << endl;

	system("pause");
	return 0;
}
Last edited on

1
2
	numBooks + numOfBooks = numBooks;
	purchaseAmt + amount = purchaseAmt;


You can't assign things like this. The explanation depends on how technical you want to get, but basically when you add two ints together, you get a temporary value that can be used for further calculator or assignment, but it doesn't make any sense to assign a value to a temporary value.

The = operator always assigns the right-hand side value to the left-hand side. It doesn't work the other way around. i.e the information always flows to the left.

Even if legal, it would make something like this occur:
1
2
3
int temp = numBooks + numOfBooks;
temp = numBooks;
// numBooks, numOfBooks are still unchanged 


What you're actually trying to do is increment numBooks and purchaseAmt, it looks like.

numBooks = numBooks + numOfBooks;
or, shorter:
numBooks += numOfBooks;

PS: I would urge you to have better names for your variables. What is the logical difference between numBooks and numOfBooks? Perhaps changed "numOfBooks" to "numBooksPurchased".
Last edited on
You are trying to assign the new value to the existing variable. The syntax is:

<var> = <expression>

You were very close. Use these lines instead:

1
2
numBooks = numBooks + numOfBooks;
purchaseAmt = purchaseAmt + amount;


Topic archived. No new replies allowed.