0

Making a Bar Graph

So I have this code and the issue I'm having is at the end it is suppose to display stars that shows the sales for each of the five stores. It only prints the last value entered so the store amount for "Store 5" would print on all of them. Any suggestions? /*This program will ask the user to input the sales amount for 5 different stores and will display a bar graph based on their input*/ #include <iostream> #include <cmath> using namespace std; int main() { const int SALES = 100; const int STORES = 5; int count; int total; int bar; cout << "Welcome to the Sales Total Bar Charter!" << endl; for (count = 1; count <= STORES; count++) { cout << "Enter today's sales for store " << count << ": " << flush; cin >> total; while (total < 0) { cout << "Invalid: input MUST be positive." << endl; cout << "Enter today's sales for store " << count << ": " << flush; cin >> total; } } cout << "\nSALES BAR CHART" << endl; for (count = 1; count <= STORES; count++) { bar = total / SALES; cout << "Store " << count << ": " << flush; for (int i = 0; i < bar; i++) { cout << '*'; } cout << endl; } cout << "\nThanks for using the Bar Charter" << endl; return 0; }

22nd Feb 2020, 1:41 AM
L'Rynn Ripley
L'Rynn Ripley - avatar
1 ответ
+ 1
L'Rynn Ripley you need arrray to store different values in loop. Once first entry is made in for loop , total value is no longer available as you are overriding the same variable. Below should work : // Declare array at top post you declared store variable int arrTotal[STORES]; // Inside first loop , assign value to array at index after doing cin >> Total arrTotal[count-1]= total; // In second for loop bar = arrTotal[count-1] / SALES; p.s. : why count -1??? reason is that array index start at 0
22nd Feb 2020, 4:16 AM
Ketan Lalcheta
Ketan Lalcheta - avatar