8/31/2018 if statement - Grading system in C++ - Stack Overflow
Join us in building a kind, collaborative learning community via our updated Code of Conduct.
Grading system in C++ Ask Question
So this is my C++ question :
Write a program that translates a letter grade into a number grade. Letter grades are
A, B, C, D and F, possibly followed by + or -. Their numeric values are 4, 3, 2, 1 and
0. There is no F+ or F-. A + increases the numeric value by 0.3, a – decreases it by
0.3. However, an A+ has value 4.0.
Enter a letter grade: B
The numeric value is 2.7
And here is my code :
int main ()
{
char grade;
int value;
cout << "Enter letter grade : " ;
cin >> grade;
switch(grade)
{
case 'A' : value = 4;
break;
case 'B' : value = 3;
break;
case 'C' : value = 2;
break;
case 'D' : value = 1;
break;
case 'E' : value = 0;
break;
default : cout << "Wrong input " << endl;
break;
}
cout << value;
system("PAUSE");
return 0;
It able to print out 4 for A, 3 for B and
so on. But however, the question
required us to make calculation for
the + and - also. Am I supposed to
use Join
if elseStack Overflow
statement after to
thelearn, share knowledge, and build your career.
switch?
This site uses cookies to deliver our services and to show you relevant ads and job listings. By using our site, you
acknowledge that Any
you have
help read andbe
would understand our Cookie Policy, Privacy Policy, and our Terms of Service. Your use of
appreciated.
Thanks and
Stack Overflow’s Products Services, including the StackSign
in advance. Up Network, is subject to these policies and terms.
Overflow
https://stackoverflow.com/questions/16188085/grading-system-in-c 1/6
8/31/2018 if statement - Grading system in C++ - Stack Overflow
c++ if-statement switch-statement
edited Apr 24 '13 at 13:23
asked Apr 24 '13 at 9:15
Rauryn
97 1 3 14
1 First, your char variable grade cannot
take strings which consist of more
than single character i.e., A+, A-. So
use char array instead. Second, then
using if on string comparisons will be
easier. Please redesign. –
Atiq Rahman Apr 24 '13 at 9:19
1 The question also seems to suggest
that 'B' should be worth 3, and then
it's given as an example worth 2.7... –
Chowlett Apr 24 '13 at 9:23
That's why type of value should not be
'int' – Atiq Rahman Apr 24 '13 at 9:24
Okay okay noted. Thanks. – Rauryn
Apr 24 '13 at 9:33
I had some time to spare (on a break),
so I figured I'd whip something up for
fun. Here's a link to the working code
You can see the different outputs,
depending on the input, at the bottom
of the page. Hopefully, you'll find it
useful :) – ShadowScripter Apr 24 '13
at 16:44
3 Answers
If the input is A+ or B- , then it is not
a character anymore. So, get it using
a string of chars, and check the
values.
char a[3];
float m,n;
cin>>a;
if(a[1]=='+')
m=0.3;
else if (a[1]=='-')
m=-0.3;
else
m=0;
switch(a[0])
{ to learn, share knowledge, and build your career.
This site uses cookies//Assign
to deliverthe
ourvalue
services and to show you relevant ads and job listings. By using our site, you
of n as 4,3,2,1 d
acknowledge that you
} have read and understand our , , and our . Your use of
cout<<n+m;
Stack Overflow’s Products and Services, including the Stack Overflow Network, is subject to these policies and terms.
https://stackoverflow.com/questions/16188085/grading-system-in-c 2/6
8/31/2018 if statement - Grading system in C++ - Stack Overflow
will print the grade
edited Apr 24 '13 at 9:27
answered Apr 24 '13 at 9:20
Aswin Murugesh
6,273 4 25 52
How about this? the grade is not
changed if there is no + or - . If
there is none, then a[1] will be \0 –
Aswin Murugesh Apr 24 '13 at 9:23
1 Where is the storage for termination
symbol null? – Atiq Rahman Apr 24
'13 at 9:23
if the second character is null, it will be
\0 and m will be assigned 0 . So
what is the problem here? –
Aswin Murugesh Apr 24 '13 at 9:27
It works but I want to know what's the
char a[3]? I thought it supposed to be
char a[1] since A+,A- only contains
two char. – Rauryn Apr 24 '13 at
9:32
1 That's the storage for null termination.
You need to allocate an extra byte for
that. – Atiq Rahman Apr 24 '13 at 9:35
I'd deal with each character in turn.
You know that the first one should be
a letter grade, so you look at that one
first, and you could just use a switch
statement like you've already got for
that.
uint value = 0;
char grade_letter = grade[0];
switch (grade_letter) {
// assign value as appropriate
}
to learn, share knowledge, and build your career.
This site uses cookies to deliver our services and to show you relevant ads and job listings. By using our site, you
acknowledge that Then, if the
you have second
read character
and understand exists,
our , , and our . Your use of
check if it's a + or a - and modify the
Stack Overflow’s Products and Services, including the Stack Overflow Network, is subject to these policies and terms.
https://stackoverflow.com/questions/16188085/grading-system-in-c 3/6
8/31/2018 if statement - Grading system in C++ - Stack Overflow
value you got from the switch
statement accordingly.
if (grade.length() > 1) {
char modifier = grade[1];
switch (modifier) {
case '+': value += 0.3;
// etc.
}
}
Before all that of course, you'll want
to check for the special case of A+
and skip everything else.
An alternative approach would be to
replace the switch statements with
lookups in a previously-prepared data
structure, something like a
std::map<char, int> , which, even if
the initialisation code prepared it from
hardcoded data, would open the way
to loading the grade information from
a configuration file or database at
some point in the future. Special-
casing A+ in that case becomes
slightly trickier though.
answered Apr 24 '13 at 9:35
Matthew Walton
8,001 1 20 35
As OP mentioned other characters
than F can also be followed by + or -.
So special case is not A but F? –
Atiq Rahman Apr 24 '13 at 9:38
But there's an error at the char
grade_letter = grade[0]; this line –
Rauryn Apr 24 '13 at 9:42
Declare grade as string before that.. –
Atiq Rahman Apr 24 '13 at 11:59
@Rauryn it's not a complete solution,
just intended to be an idea of what
might be a good way to do it, and
assumes some things from your
original code. – Matthew Walton Apr
25 '13 at 7:51
Simply adding more cases would
help. Modifying your code to
as learn,
below:share knowledge, and build your career.
This site uses cookies to deliver our services and to show you relevant ads and job listings. By using our site, you
acknowledge that you have read and understand our
int main ()
, , and our . Your use of
Stack Overflow’s Products
{ and Services, including the Stack Overflow Network, is subject to these policies and terms.
String grade;
https://stackoverflow.com/questions/16188085/grading-system-in-c 4/6
8/31/2018 if statement - Grading system in C++ - Stack Overflow
float value;
cout << "Enter letter grade : " ;
cin >> grade;
switch(grade)
{
case "A" : value = 4;
break;
case "A+" : value = 4.3;
break;
case "A-" : value = 3.7;
break;
case "B" : value = 3;
break;
case "B+" : value = 3.3;
break;
case "B-" : value = 2.7;
break;
case "C" : value = 2;
break;
case "C+" : value = 2.3;
break;
case "C-" : value = 1.7;
break;
case "D" : value = 1;
break;
case "D+" : value = 1.3;
break;
case "D-" : value = 0.7;
break;
case "F" : value = 0;
break;
default : cout << "Wrong input " <
break;
}
cout << value;
system("PAUSE");
return 0;
This is a straightforward extension of
what you did, there can be million
other ways.
edited Apr 24 '13 at 10:24
answered Apr 24 '13 at 9:31
Alan Francis
721 4 15
Okay okay I've thought of this too but
it's too long. Thanks tho – Rauryn
Apr 24 '13 at 9:34
The statement containing case 'F'
should be in double quotes. Please
correct. – Atiq Rahman Apr 24 '13 at
9:34 to learn, share knowledge, and build your career.
This site uses cookies to deliver our services and to show you relevant ads and job listings. By using our site, you
acknowledge that you Ithave read
works and
fine, butunderstand our not at
it's clunky and , , and our . Your use of
all extensible. And, I suspect, you get
Stack Overflow’s Products and Services, including the Stack Overflow Network, is subject to these policies and terms.
more marks for writing something that
expresses in code the idea of + and
https://stackoverflow.com/questions/16188085/grading-system-in-c 5/6
8/31/2018 if statement - Grading system in C++ - Stack Overflow
expresses in code the idea of + and -
being modifiers, rather than "B+"
having an absolute value in a lookup
table. – Matthew Walton Apr 24 '13 at
9:36
Is it possible to use a string in a switch
statement? – Luke B. Apr 24 '13 at
13:28
No. C++ string cannot be used in
switch statement as it is not defined
as type but class. – Atiq Rahman Apr
25 '13 at 8:09
to learn, share knowledge, and build your career.
This site uses cookies to deliver our services and to show you relevant ads and job listings. By using our site, you
acknowledge that you have read and understand our , , and our . Your use of
Stack Overflow’s Products and Services, including the Stack Overflow Network, is subject to these policies and terms.
https://stackoverflow.com/questions/16188085/grading-system-in-c 6/6