What I would like to do however is code it so the user can enter multiple numbers until the user enters 40 and then display the histogram accordingly
You have the basic idea of building a histogram, so just throw that inside of a loop:
while (std::cin>> grade && grade > 0 && grade < 40) {
if (grade >= 1 && grade <= 9)
++bin_array[0];
else if (grade >= 10 && grade <= 19)
++bin_array[1];
else if (grade >= 20 && grade <= 29)
++bin_array[2];
else if (grade >= 30 && grade <= 39)
++bin_array[3];
} Displaying the histogram is a smidge harder because you need to display the header as well (1-9, 10-29, etc...). Doing it cleanly may be a bit much for a beginner, so you'll most likely end up using several loops in sequence:
std::cout<<"1-9: ";
for (int j = 0; j < bin_array[0]; j++)
std::cout<<'*';
std::cout<<'\n';
std::cout<<"10-19: ";
for (int j = 0; j < bin_array[1]; j++)
std::cout<<'*';
std::cout<<'\n';
std::cout<<"20-29: ";
for (int j = 0; j < bin_array[2]; j++)
std::cout<<'*';
std::cout<<'\n';
std::cout<<"30-39: ";
for (int j = 0; j < bin_array[3]; j++)
std::cout<<'*';
std::cout<<'\n'; Note that your code is currently rather broken, but I'll refrain from detailing the errors since you asked about how to achieve your desired output rather than how to fix non-working code.
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401