I have been a histogram assignments to do in C++ although i kno how to do in Visual Basic.
I have tried to do it in C++ but it seems ive missed out a few steps.
below is what i have done so far, please correct me where am wrong. thank Ellie

The program should allow the tutor to enter in the various marks which the students have been awarded, until the tutor enters in a mark exceeding 100. At this point the program should display a histogram. Each star represents a student who achieved a module mark in the range shown.
This is an example of the output. The example below shows the distribution of marks for 20 students. Your program should work with any number of student marks entered.
0-29 ***
30-39 *****
40-69 ********
70-100 ****

20 students in total.
• As the tutor enters each mark, a counter should count the number of student’s marks which have been entered.
• Use the same 4 category ranges shown here.
• Make sure the display is neatly formatted as above.
• Your program should make use of ‘loops’ for the display of each category.

#include <iostream>
	using namespace std;
	int main ()
     {
        int Grade1 = 0-29; 
        int Grade2 = 30-39; 
        int Grade3 = 40-69; 
        int Grade4 = 70-100;
		int Mark = 0;
		int Totalmark = 0;

			 while (Mark <= 100)

		     cout<< Grade1<< endl;

             (Mark >= 0 && Mark <= 29)
               Grade1 = Grade1 + 1       // am finding a problem on this line
             Totalmark = Marks + Totalmark
	
       return 0;
	}

Recommended Answers

All 6 Replies

So what's the problem on that line? We can't read your mind.

I see no problem at all. If that line is executed once, Grade1 contain -28. If that's what you see, that's what you should expect.

Just to point out, when the you define your 'grades', the initial values are:
Grade1 = -29
Grade2 = -9
Grade3 = -29
Grade4 = -30
This info may be of interest.

0-29 is a label (string), not a value (int). Initialize the ints called Grade1,...,Grade4 to 0. Use a loop to input the grades. Use an input with a value of over 100 to stop input after an arbitrary number of inputs. You can either use an infinite loop and use the keyword break to get out of the loop if the input has a value of over 100, or you can use a flag (or sentinnel) to stop the input if the input has a value of over 100. If the input has a value between zero and 29, inclusive, then increment Grade1 by one. Otherwise if the input has a value between 30 and 39, inclusive, then increment Grade2 by 1, etc.

Once the input loop has stopped then use each value of the GradeXs to control a loop to display as many stars after the appropriate range label, one loop for each GradeX.

If you know about arrays, then instead of using 4 stand alone ints, you could use an array of 4 ints, and instead of 4 stand alone strings acting as range labels you could use an array of strings. If you don't know about arrays, that's okay, too. You will learn about them shortly. Given the instructions you posted, I suspect you don't know about arrays yet.

0-29 is a label (string), not a value (int). Initialize the ints called Grade1,...,Grade4 to 0. Use a loop to input the grades. Use an input with a value of over 100 to stop input after an arbitrary number of inputs. You can either use an infinite loop and use the keyword break to get out of the loop if the input has a value of over 100, or you can use a flag (or sentinnel) to stop the input if the input has a value of over 100. If the input has a value between zero and 29, inclusive, then increment Grade1 by one. Otherwise if the input has a value between 30 and 39, inclusive, then increment Grade2 by 1, etc.

Once the input loop has stopped then use each value of the GradeXs to control a loop to display as many stars after the appropriate range label, one loop for each GradeX.

If you know about arrays, then instead of using 4 stand alone ints, you could use an array of 4 ints, and instead of 4 stand alone strings acting as range labels you could use an array of strings. If you don't know about arrays, that's okay, too. You will learn about them shortly. Given the instructions you posted, I suspect you don't know about arrays yet.

I have not done arrays yet? am not really good at C++ but kno more about Visual Basic.I kno i might sound like a pain in the neck but can u plse re-phrase or explain it in simple terms on what you just explaned above. Thank U

strings are groups of characters that are frequently used to represent words, sentences, equations, basically anything other than an individual char or a numerical value, such as an int or float or a double. The most common types of strings are C style strings and STL string objects. 0-29 is to be used as a string, used a label or phrase, to represent "zero to twenty nine". It is not to be interpreted zero minus 29 (or negative 29 as implied by WaltP) which is what you had written.

if you haven't had arrays, then ignore the last paragraph of my first post.

This is an infinite loop:

int temp;
while(cin >> temp)
{}

meaning it will run forever, or as long as cin remains uncorrupted, unless you you put something like

if temp is over 9990
  break;

within the body of the loop in which case the keyword, break, will stop the loop from further activity if temp is over 9990. This;

bool more = true;
int temp;
while(more)
{
   cout >> "enter a grade.  to stop entering grades, enter a value above 9990";
   cin >> temp
   if(temp > 9990)
     more = false;
}

is a loop controlled by a boolean variable called more. As long as more is true,the loop will work another time. Some people like to use the keyword break, and others like to use flag/sentinnel variables to control how long to control input.

strings are groups of characters that are frequently used to represent words, sentences, equations, basically anything other than an individual char or a numerical value, such as an int or float or a double. The most common types of strings are C style strings and STL string objects. 0-29 is to be used as a string, used a label or phrase, to represent "zero to twenty nine". It is not to be interpreted zero minus 29 (or negative 29 as implied by WaltP) which is what you had written.

if you haven't had arrays, then ignore the last paragraph of my first post.

This is an infinite loop:

int temp;
while(cin >> temp)
{}

meaning it will run forever, or as long as cin remains uncorrupted, unless you you put something like

if temp is over 9990
  break;

within the body of the loop in which case the keyword, break, will stop the loop from further activity if temp is over 9990. This;

bool more = true;
int temp;
while(more)
{
   cout >> "enter a grade.  to stop entering grades, enter a value above 9990";
   cin >> temp
   if(temp > 9990)
     more = false;
}

is a loop controlled by a boolean variable called more. As long as more is true,the loop will work another time. Some people like to use the keyword break, and others like to use flag/sentinnel variables to control how long to control input.

Thank U so much, atleast now i quite understand what you mean. Thank u again

I think the below code can work

#
#      \\Include necessary headers
#   

void main()
{int grade[4]={0,0,0,0},mark,i=0;
 
  while((mark<100) || (i<20))
   {
     cout<<"Enter marks for student"<<i+1;
     cin>>mark;
 

    if((mark>=0) && (mark<=29))
       grade[0]+ = 1;

    if((mark>=30) && (mark<=39))
       grade[1]+ = 1;

    if((mark>=40) && (mark<=69))
       grade[2]+ = 1;

    if((mark>=70) && (mark<=100))
       grade[3]+ = 1;  
     }

  cout<<"0 - 29";
   for(i=0;i<grade[0];++i)
        cout<<"*";
  cout<<"\n";

   cout<<"30 - 39";
   for(i=0;i<grade[1];++i)
        cout<<"*";
  cout<<"\n";

   cout<<"40 - 69";
   for(i=0;i<grade[2];++i)
        cout<<"*";
  cout<<"\n";

  cout<<"70 - 100";
   for(i=0;i<grade[3];++i)
        cout<<"*";
  cout<<"\n";
}

P.S - Meant for turbo c++

commented: We do not write code for people here. We help them write their own code by making suggestions. -4
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.