how to display final grade

Our homework is to create a program that will ask the user to input his name and grade and display the equivalent tally for each student.

Sample output:
What is your name?
Monica
Input your grade here
70

Monica your final grade is 2.75

here's what i have done so far, but the an error keep saying " [Error} switch quantity not an integer "
can anyone please help me?

#include <iostream>
#include <conio.h>
#include <stdlib.h>
using namespace std;
int main ()

{
float grade;
string name;

cout << " Please input your name: " << endl;
getline(cin,name);
cout << " Please input your grade: " << endl;
cin >> grade;

if ( grade >= 96.30 && grade <= 100)
{
cout << " 1.00 " << endl;
}
else if (grade >= 92.40 && grade <= 96.29)
{
cout << "1.25" << endl;
}
else if (grade >= 88.50 && grade <= 92.39)
{
cout << "1.50" << endl;

}
else if ( grade >= 84.60 && grade <= 88.49)
{
cout << " 1.75 " << endl;
}
else if (grade >= 80.70 && grade <= 84.59)
{
cout << " 2.00 " << endl;

}
else if (grade >= 76.80 && grade <= 80.69)
{
cout << " 2.25 " << endl;
}
else if (grade >= 72.90 && grade <= 76.79)
{
cout << " 2.50 " << endl;
}
else if (grade >= 68.90&& grade <= 72.89)
{
cout << " 2.75 " << endl;
}
else if (grade >= 65.00 && grade <= 68.89)
{
cout << " 3.00 " << endl;
}
else if ( grade = 0.00 && grade <= 64.00)
{
cout << " 5.00 " << endl;
}
else
{
cout << " invalid input " << endl;
}

switch (grade)
{
case 'A':
cout << name << "your final grade is " << grade;
break;
case 'B':
cout << name << "your final grade is " << grade;
break;
case 'C':
cout << name << "your final grade is " << grade;
break;
case 'D':
cout << name << "your final grade is " << grade;
break;
case 'E':
cout << name << "your final grade is " << grade;
break;
case 'F':
cout << name << "your final grade is " << grade;
break;
case 'G':
cout << name << "your final grade is " << grade;
break;
case 'H':
cout << name << "your final grade is " << grade;
break;
case 'I':
cout << name << "your final grade is " << grade;
break;
case 'J':
cout << name << "your final grade is " << grade;
break;
default :
cout << "Undefined value! Make sure it's a value from 0-100!";
break;
}
getch ();
system("cls");
return main ();

}
Last edited on
Two things are wrong with that switch. Firstly, switch only works with integer classes (including chars which are convertible) and you are passing a float meaning the switch is invalid. You then compare this float to a char in your case statements meaning none of them would call. Also please use code tags in the future. Other things wrong with this code are return main(); which at the best of times would set up an infinite loop, but even then it is not legal C++. You also use system() functions, which are generally discouraged as is conio as it is non-standard.
First let me put your code into tags so that I can read:
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <iostream>
#include <conio.h>
#include <stdlib.h>
using namespace std;
int main ()

{
float grade;
string name;

cout << " Please input your name: " << endl;
getline(cin,name);
cout << " Please input your grade: " << endl;
cin >> grade; 

if ( grade >= 96.30 && grade <= 100)
{ 
cout << " 1.00 " << endl;
}
else if (grade >= 92.40 && grade <= 96.29)
{
cout << "1.25" << endl;
}
else if (grade >= 88.50 && grade <= 92.39)
{
cout << "1.50" << endl;

}
else if ( grade >= 84.60 && grade <= 88.49)
{
cout << " 1.75 " << endl;
}
else if (grade >= 80.70 && grade <= 84.59)
{
cout << " 2.00 " << endl;

}
else if (grade >= 76.80 && grade <= 80.69)
{
cout << " 2.25 " << endl;
}
else if (grade >= 72.90 && grade <= 76.79)
{
cout << " 2.50 " << endl;
}
else if (grade >= 68.90&& grade <= 72.89)
{
cout << " 2.75 " << endl;
}
else if (grade >= 65.00 && grade <= 68.89)
{
cout << " 3.00 " << endl;
}
else if ( grade = 0.00 && grade <= 64.00)
{
cout << " 5.00 " << endl;
}
else
{
cout << " invalid input " << endl;
}

switch (grade)
{
case 'A':
cout << name << "your final grade is " << grade;
break;
case 'B':
cout << name << "your final grade is " << grade;
break;
case 'C':
cout << name << "your final grade is " << grade;
break;
case 'D':
cout << name << "your final grade is " << grade;
break;
case 'E':
cout << name << "your final grade is " << grade;
break;
case 'F':
cout << name << "your final grade is " << grade;
break;
case 'G':
cout << name << "your final grade is " << grade;
break;
case 'H':
cout << name << "your final grade is " << grade;
break;
case 'I':
cout << name << "your final grade is " << grade;
break;
case 'J':
cout << name << "your final grade is " << grade;
break;
default :
cout << "Undefined value! Make sure it's a value from 0-100!";
break;
}
//getch ();
system("cls");
return main ();

}



Now check your line 8.
float grade;
You've declared grade as a float.

Now check your line 63:
1
2
3
switch (grade)
{
case 'A':

You are checking if float grade equals 'A' which is a character.
How can a float variable equal a character?
Also..
A single equals on line 54 is bad:
grade = 0.00
you want '=='
Topic archived. No new replies allowed.