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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75





#include <iostream>
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 :

1
2
3
4
5
switch(input)
{
       case '1': break;
       case '2': break;
}


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


1
2
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.
your calculategrade function only return a value, you should add an output:
 
cout<<calculategrade(total);


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:

1
2
grade = calculategrade(total);
cout<< "Your grade is: "<< grade <<endl;


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.