user1953285
user1953285

Reputation:

C++ When inputting, program ends without continuing

EDIT: Here is the If-Else. Please see what is going on. And can someone tell me how to return if the answer is wrong? Like, INcorrect answer, enter again?

using namespace std;

int main()
{

cout<<"Welcome to the Grade Database. Please insert your domain: " ;
cout<<"\n";
int d, n;
cin>>d;
cout<<"Now enter your total grade(between 0-100): " ;
cin>>n;
if (n>0 && n<59){
    cout<<"See you next year then :(" ;
    cout<<"F-"<<n;}
else if (n<60 && n>=69){
    cout<<"Well...you pass ;D" ;
    cout<<"E-"<<n<<"  ~"<<d;}
else if (n>70 && n<=79){
    cout<<"Better than the average!";
        cout<<"D-"<<n<<"  ~"<<d ;}
else if (n>80 && n<=89){
    cout<<"Very well sir!";
    cout<<"C-"<<n<<"  ~"<<d;}
else if (n>90 && n<=99){
    cout<<"Wow, amazing! One of the best!";
    cout<<"B-"<<n<<"  ~"<<d;}
else if(n==100){
    cout<<"Well, hello there Mr. Stephen Hawking.";
    cout<<"A-"<<n<<"  ~"<<d;}
else{
    cout<<"Invalid Entry.";}

return 0;

}

Upvotes: 0

Views: 90

Answers (1)

Ben Voigt
Ben Voigt

Reputation: 283901

switch in C++ doesn't support ranges or conditions, only exact matches. Since you have conditions, try using if and else, like this:

cin>>n;
if (n>0 && n<59) {
    cout<<"See you next year then :(" ;
    cout<<"F-"<<n;
}
else if (n>=60 && n<=69) {
    cout<<"Well...you pass ;D" ;
    cout<<"E-"<<n<<"  ~"<<d;
}
else if (n>=70 && n<=79) {
    cout<<"Better than the average!";
    cout<<"D-"<<n<<"  ~"<<d ;
}
else if (n>=80 && n<=89) {
    cout<<"Very well sir!";
    cout<<"C-"<<n<<"  ~"<<d;
}
else if (n>=90 && n<=99) {
    cout<<"Wow, amazing! One of the best!";
    cout<<"B-"<<n<<"  ~"<<d;
}
else if (n==100) {
    cout<<"Well, hello there Mr. Stephen Hawking.";
    cout<<"A-"<<n<<"  ~"<<d;
}
else {
    cout<<"Invalid Entry.";
}

You probably also want some newline characters. Simply writing cout << a second time won't start a new line, have a look at std::endl.

Upvotes: 5

Related Questions