Hi there,
this is my first class in c++ and I hope I can pass it.
i'm stuck in ( menu driven, Loop, and if )
my prgoram is:
a store program, when the user run the program,
the program should display 4 menu choices.
1- sales
2- open salary file
3- delete salary file
4- end program
and then, if user choose 1: it should display another menu
1- football sales
2- t-shirt sales
3- shoes sales
if the user choice anyone of the 3 sales, it should ask the user three questions and must be answered by user (cin) :
1- how many you bought? (user answer)
2- what is the price? (user answer)
3- do you want to enter more sales? (y or n) : (user answer)
I did all of the previous, but someone told me that my code is too long and I can make it more shorter than what I did.
the problems: 1- I want to save the ( input sales ) to a sales.txt file, and the file must be acoording to what the user did, for example, if I run the program and choose 1 which is "sales" and then choose 2 which is "t-shirt sales" and then I enter 8 for 1st question and 5 for second question, the sales.txt file must save that:
t-shirt (the name of the item that he choose to enter, in our example is t-shirt ).
8*4=40 (in this line must save the calculation of the two questions )
how can I did that?
last question: if the user answer this question ( 3- do you want to enter more sales? (y or n) : ) by yes, the program must display this menu again
1- football sales
2- t-shirt sales
3- shoes sales
and let the user choose any and enter new information and that information save under the old one.
if the user choose NO, the main menu should display which is:
1- sales
2- open salary file
3- delete salary file
4- end program
and let the user choose any thing he wants.
that all of my questions, I hope you guys can help me
here is my code ( I used different names in this code so don't confuse)
if there is anything not good, please correct me to do it in best way.
Best regards,
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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
|
#include<iostream>
#include<iomanip>
#include<ctime>
#include<fstream>
using namespace std;
int main()
{
int choice; // To hold a menu choice
int choice_dep; // To hold a department choice
float itemPrice; // To hold the price of the item
int quantity; // To hold the quantity of of the item
const float TAX = 1.0975; // Putnam County Tax rate
float total; // To hold the calculation of ( item price * quantity * TAX )
ofstream salesFile; // Sales.txt file
char answer ;
// Constants for menu choices
const int EnterSales_CHOICE = 1,
TallyDailySales_CHOICE = 2,
DeleteDailySales_CHOICE = 3,
EndProgram_CHOICE = 4;
// Display the menu and get a choice
cout << "\tPlease select from the following menu items:\n\n"
<< "\t1. ENTER SALES\n"
<< "\t2. TALLY DAILY SALES\n"
<< "\t3. DELETE DAILY SALES\n"
<< "\t4. END PROGRAM\n\n"
<< "\tChoose 1 - 4: ";
cin >> choice;
// Respond to user's menu selection
if (choice == EnterSales_CHOICE)
{
// Constants for department choices
const int Candy_CHOICE = 1 ,
Costumes_CHOICE = 2,
Decoration_CHOICE = 3;
cout << "\n\n---------------------------ENTER SALES---------------------------\n\n"
<< "\tChoose the department from the following menu:\n\n"
<< "\t1. CANDY\n"
<< "\t2. COSTUMES\n"
<< "\t3. DECORATIONS\n\n"
<< "\tChoose 1, 2 or 3: ";
cin >> choice_dep;
// Respond to user's department selection
if (choice_dep == Candy_CHOICE)
{
cout << "\n\tWhat is the price of the item? ";
cin >> itemPrice;
cout << "\n";
cout << "\tHow many did they buy ? ";
cin >> quantity;
cout << "\n";
total = (itemPrice * quantity) * TAX;
salesFile.open("sales.txt", fstream::app);
salesFile << "Candy" << endl;
salesFile << total << endl;
salesFile.close();
cout << "\tDo you want to enter more sales? (y or n):";
}
else if (choice_dep == Costumes_CHOICE)
{
cout << "\n\tWhat is the price of the item? ";
cin >> itemPrice;
cout << "\n";
cout << "\tHow many did they buy ? ";
cin >> quantity;
cout << "\n";
total = (itemPrice * quantity) * TAX;
salesFile.open("sales.txt", fstream::app);
salesFile << "Costumes" << endl;
salesFile << total << endl;
salesFile.close();
cout << "\tDo you want to enter more sales? (y or n):";
}
else if (choice_dep == Decoration_CHOICE)
{
cout << "\n\tWhat is the price of the item? ";
cin >> itemPrice;
cout << "\n";
cout << "\tHow many did they buy ? ";
cin >> quantity;
cout << "\n";
total = (itemPrice * quantity) * TAX;
salesFile.open("sales.txt", fstream::app);
salesFile << "Decoration" << endl;
salesFile << total << endl;
salesFile.close();
cout << "\tDo you want to enter more sales? (y or n):";
}
}
return 0;
}
|