You are using a version without Ads of this website. Please, consider donating:

### switch case

Trying to write a program that calculates a students grade based on how many assignments have been graded. I am using a switch case since there is a total of 5 assignments, however when I enter case 1, and enter in how many I got out of 100, it just closes the program it doesn't go to the next part any ideas?

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475`` `````` #include using namespace std; char calculategrade(int total); int main() { // local variable declaration: char input; int assign1, assign2, assign3, assign4, assign5, midterm, final; int grade; int total; cout<<"Enter in the amount of assignment that has been graded: "; cin>>input; switch( input ) { case 1: cout << "What did you get on the first assignment that is out of 100: " << endl; cin >> assign1; total = assign1 / 100; calculategrade(total); case 2: break; case 3: cout << "" << endl; break; case 4: cout << "" << endl; break; case 5: cout << "" << endl; break; default : cout << "Invalid grade" << endl; } return 0; } char calculategrade(int total) { char letterGrade; if (total >= 90) letterGrade = 'A'; else if (total >= 80) letterGrade = 'B'; else if (total >= 70) letterGrade = 'C'; else if (total >= 60) letterGrade = 'D'; else letterGrade = 'F'; return letterGrade; }``````
you check a char with numeric value, should be :

 ``12345`` ``````switch(input) { case '1': break; case '2': break; }``````

or change data type of input
still does the same thing, it gets to

 ``12`` ``````cout << "What did you get on the first assignment that is out of 100: " << endl; cin >> assign1; ``````

after I enter what I got, it closes.
 `` `` ``cout<

and i don't know what this mean:
 `` `` ``total = assign1 / 100;``
1.
Change the input to an int and remove the ' ' to make the switch work more smoothly.

2.
The /100 is unnecessary unless the score is not out of 100.
eg. if the score was out of 40 not out of 100 it would be:

`(assign1 / 40) * 100`

3.
Change grade to char variable. then do this to out put it:

 ``12`` ``````grade = calculategrade(total); cout<< "Your grade is: "<< grade <

4. Add some method of stopping the program to view the result, I wouldn't recommend it for an important program but this should work here if you put it before return 0;

`system("PAUSE");`

Basically your math was a bit off and you weren't outputting the result properly.
Last edited on
Your problem is that you don't have a loop. Assuming you enter '1' as the input, you enter the grade, calculate a 'total', calculate a grade, drop out of the switch statement and exit the program.

My guess is you want an outer loop like
`for (int input = 1; input <= 5; input++) ...`

This would replace lines 19 and 20.

At the end of the loop you would calculate the total and grade letter.
A loop? Don't think he needs one, the way he's structured it you would put in the number of assignments, say 3, then the scores for those, say 98, 15 and 40, then those would be totaled as a percentage based on max scores and weighting and then converted to a lettered grade.

So each case would include the calculations and inputs from the cases before it plus the calculation and input for the last assign graded.

It's easier with a loop and that's the way I'd do it too, but he said he wanted to use a switch statement.
Last edited on
Topic archived. No new replies allowed.

You are using a version without Ads of this website. Please, consider donating: