-2

I am writing a switch statement example and everything is fine, no error is given and it runs fine but it's not doing what I want it to do. It shows no output at all, any suggestions please?

I don't know what to try because there is no error, it just doesn't work

 #include <iostream>
 using namespace std;
 main ()
 {
    int student,average,grade,sum;
    char A,a,B,b,C,c,D,d,F,f;
    student = 1;
    average,sum = 0;
    cout << "Please enter the grades of student:";
    cin >> grade;
    switch (grade)
     {
    case 'A' :
    case 'a' :
        cout << "excellent";
        break;
    case 'B' :
    case 'b' :
        cout << "very good";
        break;
    case 'C' :
    case 'c' :
        cout << "good";
        break;
    case 'D' :
    case 'd' :
        cout << "Poor";
        break;
    case 'F' :
    case 'f' :
        cout << "Fail";
        break;
        default:
        cout << "Please enter the grade from A to D & F";
     }
  }

I want it to tell me the remarks of the student's grade but it doesn't show anything at all and it will always show the default message no matter what you enter

dan
  • 1,198
  • 8
  • 25
  • `cout << "Please enter the grades of student:";` maybe you should change that to enter the grade of the student. You are retrieving a single grade not multiple grades. – drescherjm Jan 03 '19 at 18:50
  • `main ()` should be `int main()` – drescherjm Jan 03 '19 at 18:51
  • `average,sum = 0;` the comma operator does not do what you think. https://ideone.com/ndXF4y – drescherjm Jan 03 '19 at 18:52
  • Thank you for trying to answer and i really appreciate it but none of the things you suggested were the problem,The real problem was that i was using char,while i should've used getchar() the guy who commented second he did it fine and so i tried and it worked for me too so you should try it too its a good exercise and thank you again for your effort – Muti Ur Rehman Jan 04 '19 at 16:40
  • I was just pointing out other bugs in your code. The things I posted are bugs. The reason I did not mention the char problem was that @rici had already answered that part when I commented. – drescherjm Jan 04 '19 at 16:41
  • how is it a bug? it works just fine – Muti Ur Rehman Jan 04 '19 at 17:00
  • Working fine does not mean the code has no bugs. The 3 things I mentioned were problems / bugs in your code. – drescherjm Jan 04 '19 at 17:02
  • Okay maybe you are right but whatever i posted the solution too and i tried to resolve your so called bugs there – Muti Ur Rehman Jan 04 '19 at 17:35

3 Answers3

3

You need to make grade a char, not an int.

cin >> grade; is trying to read an integer from the input stream, because grade is an int. If you make grade a char, cin >> grade will read just a single character. (In both cases, it first skips whitespace, so you cannot read a single whitespace character. But that doesn't matter here.)

It's probably worth adding that when you try to read a number and there is no number to read (because the user typed a letter grade) then the input stream is put into an error state. Until the error is reset with std::cin.clear() any input attempt will fail without reading anything. See std::istream::clear, especially the example code provided.

rici
  • 234,347
  • 28
  • 237
  • 341
  • 5
    Also, I have no idea why you do this: `char A,a,B,b,C,c,D,d,F,f;`. It is completely unnecessary. – rici Jan 03 '19 at 18:48
  • First of all thank you for your effort and then your answer was kinda helpful i changed char to getchar() and it worked fine the guy above your answer suggested it,Actually i am talking things slow my teacher didn't taught me the code u gave and can't understand it yet but still thank you – Muti Ur Rehman Jan 04 '19 at 16:45
  • and i added a,b,c,d etc because it think its better to understand what are the characters anybody can enter.I was just trying to make a good program – Muti Ur Rehman Jan 04 '19 at 16:47
  • 1
    @MutiUrRehman: That's not what `char a;` means. `char a;` declares a variable named `a`; it has nothing to do with the character value `'a'`. (And, for what its worth, my suggestion was to change `int grade` to `char grade;`. I think you need to work on understanding what variables are and how declarations work.) – rici Jan 04 '19 at 16:48
  • Yes you are right I gave it a thought and was able to make the programme in my own way – Muti Ur Rehman Jan 05 '19 at 21:59
0

Okay so i asked this question yesterday and thank you guys for your help i used getchar to the the answer but honestly i didn't knew what was the purpose of getchar because i didn't learned it yet so i kept trying and i solved it at last i am posting this only because you guys shared your knowledge and helped me so i want to share what i learned to increase your knowledge the code is here

#include <iostream>
using namespace std;
main ()
{
    char grade;
    cout << "Please enter the grades of student:";
    cin >> grade;
    switch (grade)
    {
        case 'A' :
        case 'a' :
            cout << "excellent";
            break;
        case 'B' :
        case 'b' :
            cout << "very good";
            break;
        case 'C' :
        case 'c' :
            cout << "good";
            break;
        case 'D' :
        case 'd' :
            cout << "Poor";
            break;
        case 'F' :
        case 'f' :
            cout << "Fail";
            break;
            default:
            cout << "Please enter the grade from A to D & F";
    }
}
-1

This should work. I used getchar() instead of cin cause getchar will only get one character (char=single character, string multiple characters). If you use cin and a user type in more then one character, cin will only use the first character.

Another point is your declaration of char a-f,A-B. This is useless. A, B, C, D,E,F are just names, you have to compare to a value. In your own switch you not using all of these chars, you compare them with a character not with a variable. case 'A': check if grade=='A'NOT if grade==a.

Another point is this: average,sum=0 . I dont really now what you want to do but if you trying to set them booth to 0 thats the wrong way. Then you should do in in the declaration like int student, average=0,sum=0; . You declared 3 integers, give them a value but never used them, maybe later? I changed main() to int main() (returntype=integer) More information about the main: What is the proper declaration of main?

I added return 0; to the main, cause a function should have return!

I also added linebreaks to your output to make it better to read from the console.

i commented thinks out where i think they are useless or wrong. If you have any questions or if i did a mistake pls let me know.

#include <iostream>
using namespace std;
int main ()
 {
    int student, average=0,sum=0; //dont know for what these integers are. All getting set but never been used.
     //char A,a='a',B,b,C,c,D,d,F,f; //Completly Useless and not used
    char grade;
    student = 1;
 // average,sum = 0;      What is the expected result of this?!

  cout << "Please enter the grades of student:\n";
  //cin >> grade; replaced with getchar();
    grade=getchar(); //get single char from console

    switch (grade)
     {
    case 'A' :
    case 'a' :
        cout << "excellent\n";
        break;
    case 'B' :
    case 'b' :
        cout << "very good\n";
        break;
    case 'C' :
    case 'c' :
        cout << "good\n";
        break;
    case 'D' :
    case 'd' :
        cout << "Poor\n";
        break;
    case 'F' :
    case 'f' :
        cout << "Fail\n";;
        break;
        default:
        cout << "Please enter the grade from A to D & F\n";
        break;
     }
    return 0;
  }
Max
  • 62
  • 1
  • 7
  • thank you very very much i changed it to getchar and it worked fine so thank you again i really really appreciate it – Muti Ur Rehman Jan 04 '19 at 16:36
  • Got to hear and see that you improve your code by your own to! – Max Jan 07 '19 at 09:31
  • yeah i did and i posted it but actually it was you who helped me doing it directly and indirectly so thank you again – Muti Ur Rehman Jan 07 '19 at 14:29
  • Yes i saw, but cant comment it. Glad to be helpfull, and keep working. Your improved code is much better but dont forget the break by the default statement. – Max Jan 07 '19 at 14:42
  • is it necessary to apply break after default statement because it would end anyway even if i don't write break plus it works just fine – Muti Ur Rehman Jan 07 '19 at 15:16
  • I know that i works fine but its a better style in my opinion – Max Jan 07 '19 at 15:19