Hey guys, I'm new to these forums and new to C++ in general. I have been looking around the forums and I have found threads with similar issues to mine but unfortunately they don't cover my exact issue. Essentially I want to make a histogram in C++ that takes a series of numbers that the user enters and then creates a histogram out of the ranges. I have made some progress on my own however I am stumped at the moment. Here is my code

#include <iostream>
using namespace std;

int bin_array [3];
int i;
int j;
int data;

int main() 
{

	cin >> grade;

        if (data>=1 && data<9) {bin_array[0]++; }
        else if (data>10 && data<19) {bin_array[1]++; }
        else if (data>20 && data<29) {bin_array[2]++; }
        else if (data>30 && data<39) {bin_array[3]++; }

		cout << "1-9: ";
        for (i=0;i<bin_array[j];i++) {cout << "*" << endl; }

		return 0;
}

as you can see I have managed to code it so the input can be sorted into one of four different ranges, and I can perform an output for when a user enters a value between 1 and 9. 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 so it would come out with something like this:

1-9: ***
10-19: *
20-29: **
30-39: ****

I appreciate any help that can be given :)

Recommended Answers

All 5 Replies

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.

Thanks a million! First of all thanks for not telling me the errors because I'm teaching myself to interpret the error messages on visual studio, and second of all thanks for the code help itself! It's very encouraging to know that I wasn't as useless at C++ as I thought.

In between the time it took me to post here and the reply I had made several attempts at solving it myself and while I didn't manage to, my attempts came close to the solution you just posted.

Another thing, I'm pleasantly how surprised at the response time for my problem as well! That could be due to the fact that my problem was not too big but still it's good to know that there are people I can come to with this sort of thing and get an answer within the hour :) Thanks a bunch!

ok, I've managed to get the histogram to display almost perfectly, there's only one issue I'm having now. Basically it does not seem to count my initial input. For example if I was to enter 20 twice, and then 40 to start this histogram it would only show 2 stars on the 20-29 section. Same way if I was to enter 10 once then 11, 12 and 13 it would only display 3 instead of the four stars. Is there any way for me to fix this?

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.

Is there any way for me to fix this?

Start by posting your current code. Though I'd wager you're doing a cin>>grade prior to the loop as well as in the loop condition, which effectively throws away the first grade.

That was exactly it haha that was a real face palm moment, thanks again!

Start by posting your current code. Though I'd wager you're doing a cin>>grade prior to the loop as well as in the loop condition, which effectively throws away the first grade.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.