+ 1

Im new to this. 2nd day. How can I make this better and make it start over after each grade entered?

Grade Letter #include <iostream> using namespace std; int main() { int x; cout << "enter grade" << endl; cin>> x; if (x==100) cout<< "Perfect A" << endl; if (x<=59) cout<< "F" << endl; if (x>=60 && x<=69) cout<< "D" << endl; if (x>=70 && x<=79) cout<< "C" << endl; if (x>=80 && x<=89) cout << "B" << endl; if (x>=90 && x<=99) cout << "A" << endl; return 0; }

20th Feb 2017, 9:58 PM
Josh Hernandez
9 Respuestas
+ 6
Tashi N This seems to work at least in sololearn compiler #include <iostream> using namespace std; int score; int main() { cout<<"Enter score"<<endl; cin>>score; do{ switch(score){ case 100: cout<<"Your score is Perfect"<<endl; break; case 90 ... 99: cout<<"You got A"<<endl; break; case 80 ... 89: cout<<"You got B"<<endl; break; case 70 ... 79: cout<<"You got C"<<endl; break; case 60 ... 69: cout<<"You got D"<<endl; break; case 50 ... 59: cout<<"You got E"<<endl; break; case 0 ... 49: cout<<"You got F"<<endl;} cout<<"\nto continue press 1"; cin>>score; }while(score==1) return 0; }
21st Feb 2017, 6:00 AM
Megatron
Megatron - avatar
+ 5
Thx, Megatron and Josh. I missed the braces and x at least. I'm quite sure there is a way to switch ranges in c++. But I think now that I messed up the syntax 😳 I'll give it a try later and correct my reply then.
21st Feb 2017, 5:40 AM
Tashi N
Tashi N - avatar
+ 5
I'm sorry guys, Megatron's is right, of course! Edited my post so that everybody can see it was wrong.
21st Feb 2017, 3:52 PM
Tashi N
Tashi N - avatar
+ 4
// Messy syntax, won't work // Go for Megatron's solution, that is correct. switch(x){ case >= 100: cout<< "Perfect A" << endl; break; case >= 90: cout << "A" << endl; break; case >= 80: cout << "B" << endl; break; case >= 70: cout<< "C" << endl; break; case >= 60: cout<< "D" << endl; break; default: cout << "F" << endl; }
20th Feb 2017, 10:49 PM
Tashi N
Tashi N - avatar
+ 2
Tashi N are you sure I am not sure in other language cases. But still this should not work as switch only works with discrete values and not with ranges. Josh 1.Use else if to compare the same value more than once. 2.use do while to repeat until user wants. 3.the code for you #include <iostream> using namespace std; int main() { int x; do { cout<<"Enter marks"; cin>>x; cout<<endl; if (x==100) cout<< "Perfect A" << endl; else if (x>=90) cout << "A" << endl; else if (x>=80) cout << "B" << endl; else if(x>=70) cout<< "C" << endl; else if(x>=60) cout<< "D" << endl; else cout << "F" <<endl; cout<<"To continue press 1\n"; cin>>x; }while(x==1); return 0; }
21st Feb 2017, 5:16 AM
Megatron
Megatron - avatar
+ 2
Megatron Thank you. That does exactly what I had in my head. Thanx for taking the time to help me learn new things.
21st Feb 2017, 5:31 AM
Josh Hernandez
+ 1
Tashi N... Didn't work. Says that the expression results are unused. Always gives answer as "F"
21st Feb 2017, 3:57 AM
Josh Hernandez
+ 1
Thanx Tashi N.
21st Feb 2017, 4:11 PM
Josh Hernandez
- 1
Use a while loop or put it in a separate function.
20th Feb 2017, 10:33 PM
aklex
aklex - avatar