help - switch loop

IF I run it, it does not let me introduce any numbers. Why? I do not have any failures or something else. Everything is ok.

#include <iostream>
using namespace std;

int main() {
int grade;
int i;

switch (1) {
case '1':
cin >> grade;
if (grade >= 0 && grade <= 29)
for (i = 0; i < 20; i++)
cout << grade++ << endl;
cout << "Category 1 - **" << endl;
break;
case '2':
if (grade >= 30 && grade <= 39)
for (i = 0; i < 20; i++)
cout << grade++ << endl;
cout << "Category 2 - *****" << endl;
break;

case '3':
if (grade >= 40 && grade <= 69)
for (i = 0; i < 20; i++)
cout << grade++ << endl;
cout << "Category 3 - ********" << endl;
break;

case '4':
if (grade >= 70 && grade <= 100)
for (i = 0; i < 20; i++)
cout << grade++ << endl;
cout << "Category 4 - ****" << endl;
break;
}

return 0;



switch (1)
This is not the proper way to use a switch statement. Normally you use a variable inside it.
Can you please show me what line is wrong and how can I correct it ? Thanks
What is the code supposed to be achieving? What inputs and outputs do you expect?
The program should allow the tutor to enter in the various marks which the students
have been awarded, until the tutor entering 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.
Thank you. From that description, it sounds as though there should be a loop, to obtain all of the inputs. I'm not sure whether it should output anything at all during that loop.

Perhaps the data for the students should be stored in an array, and processed later, after the mark exceeding 100 has been entered?

If that is so, then the if statements you have would need also to be inside a loop (or make it into a separate function, which is called inside a loop).

I still have difficulty understanding the code posted so far. I was thinking of something like this, but I still don't understand:
1
2
3
4
5
6
7
8
9
10
11
12
cout << grade << endl;

if (grade < 0)
    cout << "error\n";
else if (grade <= 29)
    cout << "Category 1 - **" << endl;
else if (grade <= 39)
    cout << "Category 2 - *****" << endl;
else if (grade <= 69)
    cout << "Category 3 - ********" << endl;
else if (grade <= 100)
    cout << "Category 4 - ****" << endl;

For example, why is the grade incremented with the ++ operator like this:
 
    cout << grade++ << endl;

and what is the purpose of the for-loop.

Also why are there fewer asterisks printed for the highest scoring students (70 to 100 get only four stars)?

I'm sure there's a bigger picture here but I'm not getting it yet.
Last edited on
Thank you , yeah I used an array to finish my code. The code above is what a teacher told me to do but I did not work out.
Topic archived. No new replies allowed.