Did your teacher provide sample output he wants to see from every program?
Did your teacher provide sample output he wants to see from every program?
Assignment:
CS 231 Banking Assignment 2 Spring 2010
In continuation of your previous lab, you will be adding a feature that records all the transactions a user makes. Every time they deposit or withdraw money, your program will make a note of it.
To accomplish this, you need to make a new class called Transaction. This class is very simple. Following is its header information:
class Transaction {
private:
vector <string> transactionRecord;
public:
void addNewTransaction(string);
};
A transaction record is simply a string that looks like one of the following:
“Withdrew $200”
“Deposited $500.32”
etc.
You will need to have one object of the Transaction class in your main file. Also, you need to add a new menu choice that prints out all the transactions so far. This should work as shown:
Please make a selection:
1) Display Current Account Balance
2) Deposit Money
3) Withdraw Money
4) Display all Previous Transactions
5) Exit
< user enters 4 >
Deposited $37.12
Withdrew $48
Deposited $100
ok this is what i have so far:
Im geting 3 errors:Code:#include <iostream> #include <string> #include <fstream> #include <sstream> #include <vector> using namespace std; class Transaction { private: vector <string> transactionRecord; public: void addNewTransaction(string transactionRecord); }; void Transaction::addNewTransaction(string transactionRecord) { for (int i=0; i<transactionRecord.size(); i++) { cout<<transactionRecord.at(i)<<"\n"; } class Account { private: float balance; float deposit; float withdraw; public: void depositFunds(float balance); void withdrawFunds(float balance); float getBalance(float balance); }; void Account::depositFunds(float balance) { float numb = 0; cout << "How much would you like to deposit: " << endl; cin >> numb; float dtotal = balance + numb; cout << "You have deposited "<<dtotal<<" dollars"<< endl; system ("PAUSE"); } void Account::withdrawFunds(float balance) { float numb = 0; cout << "How much would you like to withdraw today: " << endl; cin >> numb; float wtotal = balance - numb; cout << "You have withdrawn "<<wtotal<<" dollars" << endl; system ("PAUSE"); } float Account::getBalance(float balance) { balance = balance; return balance; } void main() { int menu; int trigger = 0; float balance = 2000; Account myAccount; string transactionRecord; Transaction myTransaction; while(trigger==0){ cout << "1. Get a balance inquiry." << endl; cout << "2. Make a deposit." << endl; cout << "3. Make a withdrawal." << endl; cout << "4. Recent Transactions." << endl; cout << "5. Exit." << endl; cout << "what would you like to do?:"; cin >> menu; switch (menu){ case 1: //Current balance cout << myAccount.getBalance(balance); system ("PAUSE"); break; case 2://does the deposit myAccount.depositFunds(balance); break; case 3://does the withdrawal myAccount.withdrawFunds(balance); break; case 4://Recent Transactions myTransaction.addNewTransaction(transactionRecord); break; default:// exit out the loop cout << "Thank you for using our serivice"<<endl; cout << "Goodbye" << endl; system ("PAUSE"); trigger=1; } system ("cls"); } }
1) Line 33: Identifier ; depositFunds; cannot have a type qualifier in function Transaction::addNewTransaction(string)
2) Line 98: Declaration missing ; in function Transaction::addNewTransaction(string)Code:void Account::depositFunds(float balance) {
3) line 98: Compound statement missing } in function Transaction::addNewTransaction(string)Code:}
Code:}
All im trying to do so far is set up the class, and call the function that will push out the stored information from the vector. Am i on the right track?
Also how would i make a constructor for the transaction class.
You're missing the closing bracket on your for() loop inside addNewTransaction and that's causing the compiler errors. There are other problems (read my posts above) but that'll get you past that error.
All errors fixed. How do i push the information into the vector now?
I tryed to putCode:#include <iostream> #include <string> #include <fstream> #include <sstream> #include <vector> using namespace std; class Transaction { private: vector <string> transactionRecord; public: void addNewTransaction(string transactionRecord); }; void Transaction::addNewTransaction(string transactionRecord) { for (int i=0; i<transactionRecord.size(); i++) { cout<<transactionRecord.at(i)<<"\n"; } } class Account { private: float balance; float deposit; float withdraw; public: void depositFunds(float balance); void withdrawFunds(float balance); float getBalance(float balance); }; void Account::depositFunds(float balance) { float numb = 0; cout << "How much would you like to deposit: " << endl; cin >> numb; float dtotal = balance + numb; cout << "You have deposited "<<dtotal<<" dollars"<< endl; //transactionRecord.push_back("You have deposited"<<dtotal<<" dollars."); system ("PAUSE"); } void Account::withdrawFunds(float balance) { float numb = 0; cout << "How much would you like to withdraw today: " << endl; cin >> numb; float wtotal = balance - numb; cout << "You have withdrawn "<<wtotal<<" dollars" << endl; //transactionRecord.push_back("You have withdrawn"<<wtotal<<" dollars."); system ("PAUSE"); } float Account::getBalance(float balance) { balance = balance; return balance; } void main() { int menu; int trigger = 0; float balance = 2000; Account myAccount; string transactionRecord; Transaction myTransaction; while(trigger==0){ cout << "1. Get a balance inquiry." << endl; cout << "2. Make a deposit." << endl; cout << "3. Make a withdrawal." << endl; cout << "4. Recent Transactions." << endl; cout << "5. Exit." << endl; cout << "what would you like to do?:"; cin >> menu; switch (menu){ case 1: //Current balance cout << myAccount.getBalance(balance); system ("PAUSE"); break; case 2://does the deposit myAccount.depositFunds(balance); break; case 3://does the withdrawal myAccount.withdrawFunds(balance); break; case 4://Recent Transactions myTransaction.addNewTransaction(transactionRecord); break; default:// exit out the loop cout << "Thank you for using our serivice"<<endl; cout << "Goodbye" << endl; system ("PAUSE"); trigger=1; } system ("cls"); } }
in each segement for withdraw and deposit but that didnt workCode:transactionRecord.push_back("You have deposited"<<dtotal<<" dollars.");
I mentioned the ambiguity problem before. Your class Transaction has a member variable named transactionRecord of type vector<string> your function addNewTransaction() takes in a argument bound to a local variable of type string also named transactionRecord. The at() function exists in both the vector and string interfaces, so the compiler doesn't know which one you're talking about.Code:class Transaction { private: vector <string> transactionRecord; public: void addNewTransaction(string transactionRecord); }; void Transaction::addNewTransaction(string transactionRecord) { for (int i=0; i<transactionRecord.size(); i++) { cout<<transactionRecord.at(i)<<"\n"; } }
To begin with, the purpose of addNewTransaction only seems to be to add a string to your vector. That isn't happening anywhere in your function.
They way i was looking at it was that my withdraw/deposit function would push the information into the vector, and addNewTransaction would just print it.
That's not going to work, as the vector is a private member of the Transaction class. I'm thinking you will have to expand the interface of Transaction to include a printTransactions() function that uses the for loop, while addNewTransaction(string) merely calls push_back() on the private vector transactionRecord.
What you really want out of your program structure is something like this
and then write a printTransactions() function in your Transaction class (update the header accordingly) that loops through the private vector<string> and prints them out one per line.Code:case 2://does the deposit myAccount.depositFunds(balance); myTransaction.addNewTransaction("blah"); //construct your string here break; case 3://does the withdrawal myAccount.withdrawFunds(balance); myTransaction.addNewTransaction("blooh"); // construct your string here break;
Jeeez your naming conventions are awkward. You have a class that represents one transaction and inside it you have a list of every transaction ever made. It's the other way around
EDIT: Actually, nevermind, apparently that's from your assignment instructions. It's still awkward as hell to me, but if that's what your instructor wants, I guess you can just use what he provided.Code:class Record { public: void addTransaction(std::string newTransaction); void printTransactions(); private: std::vector<string> transactionRecord; };
Also, your deposit and withdraw functions aren't actually modifying the balance in anyway. They're displaying the right balance (which will only be right on the first call to each function), but they're not saving it anywhere.
Another thing is you can't call stream operators without having a stream to put the data in (i.e. "You have deposited"<<dtotal<<" dollars."). Since c++'s string class is retarded and doesn't provide an easy way to convert numerical values to strings, you'll have to do that yourself.
Code:float f = 5.73; std::string str; // One way char buf[256] = {0}; sprintf("You have deposited %0.2f dollars", f); str.assign(buf); // Another way std::stringstream stream; stream << "You have deposited" << f << "dollars"; str = stream.str(); // You can also kind of cheat and use itoa, passing the integral and // floating point parts separately, put a dot in between and concatenate // them, but that's such a roundabout way of doing it, I'm not even going // to write up the code for it when the other two methods are far easier.
Lol Kaelan yes they are extremly weird but thats how he wants them. He wants to know exactly what they are and what they do. I perfer to abreavate. Ex: my main class would be Bank, and the transaction class be History
Current Status:
Code:#include <iostream> #include <string> #include <fstream> #include <sstream> #include <vector> using namespace std; class Transaction { private: vector <string> transactionRecord; void timeStamp(); public: void addNewTransaction(string transactionRecord); void printTransactions(string transactionRecord); }; void Transaction::timeStamp() { //*******put this in a header file********** time_t rawtime; struct tm * timeinfo; string timeStamp; time ( &rawtime ); timeinfo = localtime( &rawtime ); timeStamp = asctime (timeinfo); cout<<timeStamp; //******put this in a header file************ } void Transaction::printTransactions(string transactionRecord) { for (int i=0; i<transactionRecord.size(); i++) { cout<<transactionRecord.at(i)<<"\n"; } } void Transaction::addNewTransaction(string transactionRecord) { } class Account { private: float balance; float deposit; float withdraw; public: void depositFunds(float balance); void withdrawFunds(float balance); float getBalance(float balance); }; void Account::depositFunds(float balance) { float numb = 0; cout << "How much would you like to deposit: " << endl; cin >> numb; float dtotal = balance + numb; cout << "You have deposited "<<dtotal<<" dollars"<< endl; std::stringstream stream; stream << "You have deposited" << dtotal << "dollars"; string str = stream.str(); //transactionRecord.push_back("str"); system ("PAUSE"); } void Account::withdrawFunds(float balance) { float numb = 0; cout << "How much would you like to withdraw today: " << endl; cin >> numb; float wtotal = balance - numb; cout << "You have withdrawn "<<wtotal<<" dollars" << endl; std::stringstream stream; stream << "You have withdrawn" << wtotal << "dollars"; string str = stream.str(); //transactionRecord.push_back("str"); system ("PAUSE"); } float Account::getBalance(float balance) { balance = balance; return balance; } void main() { int menu; int trigger = 0; float balance = 2000; Account myAccount; string transactionRecord; Transaction myTransaction; while(trigger==0){ cout << "1. Get a balance inquiry." << endl; cout << "2. Make a deposit." << endl; cout << "3. Make a withdrawal." << endl; cout << "4. Recent Transactions." << endl; cout << "5. Exit." << endl; cout << "what would you like to do?:"; cin >> menu; switch (menu){ case 1: //Current balance cout << myAccount.getBalance(balance); cout << ""<<endl; system ("PAUSE"); break; case 2://does the deposit myAccount.depositFunds(balance); myTransaction.addNewTransaction("blooh"); break; case 3://does the withdrawal myAccount.withdrawFunds(balance); myTransaction.addNewTransaction("blah"); break; case 4://Recent Transactions myTransaction.printTransactions(transactionRecord); break; default:// exit out the loop cout << "Thank you for using our serivice"<<endl; cout << "Goodbye" << endl; system ("PAUSE"); trigger=1; } system ("cls"); } }
Need to do:
1) add printTransactions(); function.
2) add a function to convert into strings. (would this be a seprate function? or just coded into the existing functions?
3) turn balance into a array so it can actualy store the information and keep accurate track of the current balance.
anything else?
Also new update to the assignment, but i still need to get this working first. i Think kalean's string think will take care of converting the numbers to strings.
Code:In continuation of our previous “bank account” program, you will now be adding a timestamp to each transaction. Every time a user deposits or withdraws money, your program will make a note of it. Now a transaction record is simply a string that looks like one of the following: “Withdrew $200, Fri Mar 14 08:25:42 2008” “Deposited $500.32, Sat Mar 15 09:27:36 2008” You should make use of the following code samples: • getting the current time as a string • converting integer to string
lol now he gives you a code sample to convert base type values to strings. Shitty professor is shitty.
I don't see a reason to do this. The only use you'd have for it is to print out the balance history, but since you record that in the strings you are adding to your transactionRecord, there's no point to duplicate the data given the parameters of the assignment.3) turn balance into a array so it can actualy store the information and keep accurate track of the current balance.
Actually, no, the compiler doesn't--and shouldn't--have a problem with the way he's written those lines. The transactionRecord he has declared in the parameter list will have scope in that function, and thus the for loop, as written, will always run at() on the string parameter and not the vector class member. Is it bad practice and confusing to users? Absolutely, and the names should be changed to avoid conflict. But the compiler isn't going to have a problem at all resolving that function, even if he MEANS something different.Originally Posted by Correction
If this is a class on programming in C++, use stringstream for base type conversions to string. While occasionally "useful" as shortcuts, sprintf() and itoa() are C functions and itoa() isn't even in the C standard.
Sorry OP, but you'd be having a lot easier time programming this assignment if you sat down and worked on design and understanding the objects you're supposed to be using before jumping right into code.
based on what i currently have (see above) do you think it would be best for me to keep trying to do it in a function? or just scrap the function, put the vector in the account class and just go from there? all im worried about is puting the timestamp on the transaction records.
What i was trying to do so far:
Code:#include <iostream> #include <string> #include <fstream> #include <sstream> #include <vector> using namespace std; class Account { private: float balance; float deposit; float withdraw; public: void depositFunds(float balance); void withdrawFunds(float balance); float getBalance(float balance); void printTransactions(string transactionRecord); }; void Account::printTransactions(string transactionRecord) { for (int i=0; i<transactionRecord.size(); i++) { cout<<transactionRecord.at(i)<<"\n"; } } void Account::depositFunds(float balance) { float numb = 0; cout << "How much would you like to deposit: " << endl; cin >> numb; float dtotal = balance + numb; cout << "You have deposited "<<dtotal<<" dollars"<< endl; std::stringstream stream; stream << "You have withdrawn" << dtotal << "dollars"; string str = stream.str(); system ("PAUSE"); } void Account::withdrawFunds(float balance) { float numb = 0; cout << "How much would you like to withdraw today: " << endl; cin >> numb; float wtotal = balance - numb; cout << "You have withdrawn "<<wtotal<<" dollars" << endl; std::stringstream stream; stream << "You have withdrawn" << wtotal << "dollars"; string str = stream.str(); system ("PAUSE"); } float Account::getBalance(float balance) { balance = balance; return balance; } void main() { int menu; int trigger = 0; float balance = 2000; Account myAccount; vector <string> transactionRecord; while(trigger==0){ cout << "1. Get a balance inquiry." << endl; cout << "2. Make a deposit." << endl; cout << "3. Make a withdrawal." << endl; cout << "4. Recent Transactions." << endl; cout << "5. Exit." << endl; cout << "what would you like to do?:"; cin >> menu; switch (menu){ case 1: //Current balance cout << myAccount.getBalance(balance); cout << ""<<endl; system ("PAUSE"); break; case 2://does the deposit myAccount.depositFunds(balance); transactionRecord.push_back("str"); break; case 3://does the withdrawal myAccount.withdrawFunds(balance); transactionRecord.push_back("str"); break; case 4://Recent Transactions myAccount.printTransactions(transactionRecord); break; default:// exit out the loop cout << "Thank you for using our serivice"<<endl; cout << "Goodbye" << endl; system ("PAUSE"); trigger=1; } system ("cls"); } }
Getting 2 errors with this both in line 90:
1) cannot convert 'vector<string, allocator<string> >' to ; string; in function main()Code:myAccount.printTransactions(transactionRecord);
2) type mispatch in parameter 'transactionRecord' {wanted 'string', got 'vector<string, allocator<string> >' to ; string; in function main()
You want printTransactions to accept a vector<string> is a parameter, not a string