While loop to repeat the program
Oct 16, 2014 at 1:00am UTC
Hello everyone,
I have this problem and I don't know how to fix it. I want my program to ask the user to run the program again. I try to use the while loop but it does work. And it has the be the while loop because my professor requested so.
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
//This program belongs to Nghi Tran
// C.S. M10A Fall 2014- Sec. 71245
// Lab07A: Sales Bar Chart
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
//Declare variables
double sale1, sale2, sale3, sale4, sale5, totalsales, starcount;
int star;
char choice;
//Ask the user to put in the sales for each day
while (choice == 'Y' || choice == 'y' )
{
cout << "Please enter today's sales for store 1: " ; cin >> sale1;
cout << "Please enter today's sales for store 2: " ; cin >> sale2;
cout << "Please enter today's sales for store 3: " ; cin >> sale3;
cout << "Please enter today's sales for store 4: " ; cin >> sale4;
cout << "Please enter today's sales for store 5: " ; cin >> sale5;
cout << endl;
//Compute total sales
totalsales = sale1 + sale2 + sale3 + sale4 + sale5;
//Show user the sales chart
cout << "SALES BAR CHART" << endl;
cout << "(Each * = $100)" << endl;
cout << endl;
cout << "Store 1: " ;
starcount = round(sale1 / 100);
for (star = 1; star <= starcount; star++)
cout << "*" ;
cout << endl;
cout << "Store 2: " ;
starcount = round(sale2 / 100);
for (star = 1; star <= starcount; star++)
cout << "*" ;
cout << endl;
cout << "Store 3: " ;
starcount = round(sale3 / 100);
for (star = 1; star <= starcount; star++)
cout << "*" ;
cout << endl;
cout << "Store 4: " ;
starcount = round(sale4 / 100);
for (star = 1; star <= starcount; star++)
cout << "*" ;
cout << endl;
cout << "Store 5: " ;
starcount = round(sale5 / 100);
for (star = 1; star <= starcount; star++)
cout << "*" ;
cout << endl;
//Display total sales
cout << setprecision(2) << fixed << "The total sales is: $" << totalsales << endl;
cout << endl;
//Prompt user to do another one
cout << "Would you like to repeat one more time? (Y or N)" << endl;
cin >> choice;
}
return 0;
}
Oct 16, 2014 at 1:04am UTC
line 16: char choice = 'Y' ; // u need to initialize choice;
Oct 16, 2014 at 1:14am UTC
Thank you very much for your help :D
Topic archived. No new replies allowed.