Hello! I've made a simple program where a user inserts a test grade number and then receives the letter grade for it. Thanks for looking at it, I am open to any suggestions on improving it thanks :D
/*
Grading Program
Requires:
variables, data types, and numerical operators
basic input/output
logic (if statements, switch statements)
Write a program that allows the user to enter the grade scored in a programming class (0-100).
If the user scored a 100 then notify the user that they got a perfect score.
★ Modify the program so that if the user scored a 90-100 it informs the user that they scored an A
★★ Modify the program so that it will notify the user of their letter grade
0-59 F 60-69 D 70-79 C 80-89 B 90-100 A
*/
#include <iostream>
usingnamespace std;
int score;
int main()
{
cout<<"Hello User, please enter the grade that you have scored in class.\n";
cin>>score;
switch (score)
{case 0 ... 59: {
cout<<"\nLetter Grade: F";
break;}
case 60 ... 69:{
cout<<"\nLetter Grade: D";
break;
}
case 70 ... 79:{
cout<<"\nLetter Grade: C";
break;}
case 80 ... 89:{
cout<<"\nLetter Grade: B";
break;}
case 90 ... 100:{
cout<<"\nLetter Grade: A";
break;}
default:
{cout<<"\nInsert proper score (1-100)"<<endl;}
}
return 0;
}
Like DVS said (although he got the if statement totally wrong :D), in this case, its much better to use if statements rather than 100 cases.
And in the future, please use code tags - http://www.cplusplus.com/articles/jEywvCM9/
#include <iostream>
usingnamespace std;
int main(){
int score;
cout << "Hello User, please enter the grade that you have scored in class."<<endl;
cin >> score;
cout << endl; //endl makes a new line just like \n , but it's more beautiful
if (score>=0 && score<50){
cout << "Letter Grade: F";
}
elseif (score>=50 && score<70){
cout << "Letter Grade: D";
}
elseif (score>=70 && score<80){
cout << "Letter Grade: C";
}
elseif (score>=80 && score<90){
cout << "Letter Grade: B";
}
elseif (score>=90 && score<=100){
cout <<"Letter Grade: A"<<endl;
cout <<"You have got a perfect score!";
}
else {
cout << "Insert proper score (0-100)" << endl;
}
system("pause"); //Stop the program from exiting so fast
return 0;
}
@TarikNeaj I will be sure to keep that in mind when making my next program. if/else statements and case are suited better for different tasks. And cin.ignore(); can be used to pause the program when needed.