Switch problem

Hi, (I am one hell of a newbie in C++)
The Question is:
Write a C++ program that prompt user to enter obtained(0 - 100) marks for five course. Calculate grade for each course using Switch Expression. Display the grade for each course on new line.
i came up with this code... and its not working.. (but when it does, it only shows the grade for the last subject. But the question is asking grades for each subject)
any help.? Please ?
Sorry, if there are mistakes in my coding.

#include<iostream>
using namespace std;
int main()
{
int Marks;
char grade;
cout<<"Enter the obtained marks :"<<endl<<endl;
cout<<"English : ";
cin>>Marks;
cout<<"Programming : ";
cin>>Marks;
cout<<"Calculus : ";
cin>>Marks;
cout<<"Electronics : ";
cin>>Marks;
cout<<"Statistics : ";
cin>>Marks;

switch(Marks/10)
{
case 10 : grade='A';
break;

case 9 : grade='A';
break;

case 8 : grade='A';
break;

case 7 : grade='B';
break;

case 6 : grade='C';
break;

case 5 : grade='D';
break;

default : grade='F';
}
system("pause");
return 0;
}
Last edited on
You should put your code [code]between code tags[/code] so that it has proper syntax highlighting and line numbers. There was a template for you when you started the topic but you seem to have deleted it.

The reason your code only processes the last grade is because you overwrite the previous grades without doing anything with them. Don't use copy-and-paste to fix this, by the way (if you don't know what I mean, that's good). I would recommend using a function and calling the function to decide the letter grade after each input.

Your switch statement is "clever" but not the ideal way to process the grades - it's better to use if-else-if statements with the > operator. What if a student gets a bonus 10 points on a grade? With your current code, they fail the class for going the extra mile.
Last edited on
Each time the statement "cin >> Marks" is executed, it overwrites the values previously stored in the variable Marks.

To continue with the algorithm you have chosen, you could declare one variable for each of the subjects.

 
int mark_english, mark_programming, mark_calculus, mark_electronics, mark statistics;
@LB : Thanks for the advice, i used if else if statement. and it worked


@Squished18 : Many Thanks to u too Sir
Topic archived. No new replies allowed.