Hi I need guidance for this program and it seems i don't understand basic fundamentals of creating histogram with c++. I'm not asking for spoonfeeding or anything i just need to know what i'm doing wrong and is there anything i can include or to improve it.

This programe needs to print out a histogram about students and their marks, this program should allow a user to key in a total of 20 marks (20 students) and these marks have been keyed in (1-100 marks) it should print a histogram something like this.

0-29 *****
30-39 *****
40-69 *****
70-100 *****

This is what i've written so far and it doesn't seem so good.

#include <iostream>

using namespace std;

int main()
{
   int i, j;
   int count;
   int Input, FirstInterval, SecondInterval, ThirdInterval;

   cout << "How many marks did the student recieve?" <<endl;
   cin >> Input;
   
   if ( Input < 29 )

   FirstInterval++;
   for(i = 0; i < FirstInterval ; i++)count;
   {
      cout <<"*" << endl;
   }
   if ( Input < 39 )
   SecondInterval++;
   for(i = 0; i < SecondInterval ; i++)count;
   {
      cout <<"*" << endl;
   }
   if ( Input < 69 )
   ThirdInterval++;
   for(i = 0; i < ThirdInterval ; i++)count;
   {
      cout <<"*" << endl;
   }
   return 0;
}

What my program does is, it can key in the number but when it keys in any number it dots down 3 stars below the number.

28
*
*
*
[Please press any button to continue]

thats what comes up after i run my program.

Any assistance is appreciate thanks for your time.

If it's possible my whole coding is wrong, do I have to assign a variable to 0-29 in order to store the stars?

append an else before the second and third ifs.

append an else before the second and third ifs.

When I do put an 'else' before the second and third 'if' the entire code becomes an error and unable to build.

This is what you want to do :

#include <iostream>

using namespace std;

void printGraph(const int max){
 for(int i = 0; i < max; ++i){
   cout <<"*";
  }
 cout << endl;
}
int main()
{
   int i, j;
   int count;
   int Input, FirstInterval = 0, SecondInterval = 0, ThirdInterval = 0;

   cout << "How many marks did the student recieve?" <<endl;
   cin >> Input;
   
   if ( Input < 29 ){
     FirstInterval++;
   }
   else if ( Input < 39 ){
     SecondInterval++;
   }
   else if ( Input < 69 ){
     ThirdInterval++;
   }
   cout << "First interval : ";
   printGraph(FirstInterval);
   cout << "Second interval : ";
   printGraph(SecondInterval);
   cout << "Thrid interval : ";
   printGraph(ThirdInterval);

   return 0;
}

//mistake

Thanks for the help, now i can construct a histogram with out assistance :)
One question though, how do I enable the program run long enough so I can key in atleast 20 marks with out the program closing on me on the first data entry.

Thanks alot u don't know what this means to me :)

You need to wrap line 16 to line 28 with a loop like so :

int Input, FirstInterval = 0, ...
int MAX_INPUT = 10;
for(int i = 0; i < MAX_INPUT; ++i){
   int input = ask("How many marks did the student recieve");
   if(input < 29){ ... }
   else if(input < 39) { ... }
   else if(input < 69) { ... }
}
 /* printGraph(...);

You need to wrap line 16 to line 28 with a loop like so :

int Input, FirstInterval = 0, ...
int MAX_INPUT = 10;
for(int i = 0; i < MAX_INPUT; ++i){
   int input = ask("How many marks did the student recieve");
   if(input < 29){ ... }
   else if(input < 39) { ... }
   else if(input < 69) { ... }
}
 /* printGraph(...);

Thanks mate

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.