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.
#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;
}
#include <iostream>
#include <string>
#include <iomanip>
#include "member.h"
usingnamespace 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;
}
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.
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".