Welcome to the club! I started a bit before you but I"m still learning so take some of what I say as experience and some as wishful thinking :)
I put in your code and it complied no problems, that's step 1.
Good testing will prevent users and even yourself from doing something stupid in the future, I ran your code, put in the word test and it looped into never ever land. Also putting in a period causes the loop, but a .1 works ok.
So I recommend testing your code #2 and putting in some comments to the user so they know your program expects a number and not text. for Item:
I know this is personal taste, but I would change (-1) to close to (0) to close if that's not much trouble. again, personal taste, won't affect the program just the useability of it.
I would also change the loop method, someone ran the program, no reason to make them type Y to run it the first time. Put the Y/N after it's been ran one time. they can always type -1 to exit if they change their minds.
I also find this confusing, while perfectly legal.
cout << "-------------------\n" << endl;
If your goal is to make a blank line, I recommend
cout << "-------------------" << endl<<endl;
one last suggestion to make your program useful, after each Receipt, I would cout the total. So your new output would look something like :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
Receipt calculator by Ironman
Open new receipt? y/n y
Receipt 1 - (0) to close
=====================
Item Amount: 1
Item Amount: 2
Item Amount: 3
Item Amount: 0
|
Total = 6
Open new receipt? y/n y
Receipt 2 - (0) to close
=====================
Item Amount: 2
Item Amount: 3
Item Amount: 4
Item Amount: 0
-------------------
Total = 9
Open new receipt? y/n n
Receipt 1: 6
Receipt 2: 9
=====================
Total spent: 15$ |
All in all, well written, and documented, so it's easy for others and yourself to understand it.
Below are my recommendations:
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
|
//============================================================================
// Name : Receipts.cpp
// Author : Ironman
// Version : 0.3
// Copyright : Your copyright notice
// Description : ReceiptsCalculator
//============================================================================
#include <iostream>
#include <vector>
using namespace std;
int main(){
char choice; //used to store user action choice
float userInputNumber; // used to store user price input
float receiptTotal = 0; // used to temporally store receipt sum total before its final value is passed to vector element
int receiptNum = 1; // used to store receipt number so it match's proper receipt enumeration before price input in each receipt
vector<float> totalStorage; //used to store receipts total as a vector element
float totalStorageSum = 0; //used to store sum of all receipts total that are stored in vector
//welcome message
cout << "Receipt calculator by Ironman \n" << endl;
//ask user which action he wants to perform
cout << "Open new receipt? y/n ";
cin >> choice;
//as long user input is not n, run this loop
while(choice != 'n'){
//check if users input is valid
while (choice != 'y' && choice != 'n'){
cout << "invalid input!! try again" << endl;
cin >> choice;
}
// reset receipt total to 0, print receipt current number and ask for item price input
receiptTotal = 0;
cout << "\nReceipt " << receiptNum << " - (0) to close" << "\n=====================" << endl;
cout << "Item Amount: ";
cin >> userInputNumber;
//take user input, add it to receipt total and continue to do so until user enters 0
while(userInputNumber != 0){
receiptTotal += userInputNumber;
cout << "Item Amount: ";
cin >> userInputNumber;
}
cout << "-------------------" << endl;
cout << "Total = " << receiptTotal << endl<< endl;
//take receipt total and store it in a vector after it's last existing element and add 1 to receiptNumber.
totalStorage.push_back(receiptTotal);
receiptNum++;
//ask user which action he wants to perform
cout << "Open new receipt? y/n ";
cin >> choice;
}
cout << endl;
//print all receipts and their total in chronological order on screen
for(int x=0; x<int(totalStorage.size()); x++){
int z = (x+1);
cout << "Receipt "<< z << ": " << totalStorage[x] << endl;
totalStorageSum += totalStorage[x];
}
//print sum of all receipts total on screen
cout << "====================="<< endl;
cout << "Total spent: " << totalStorageSum << "$";
}
|