Need Help With Code

I'm not quite sure what to do here but it says that a was not declared for switch
but as far as i can tell it is and it says it is expecting an initializer before
cin << a;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
   int main(){
     cout << "what is your grade in percent?";
     int a
     cin << a;
     
switch(a){
    case a < 59:
        cout << "You got an F";
    break;
    case a < 69:
        cout << "You got a D";
    break;
    case a < 79:
        cout << "You got a C";
    break;
    case a < 89:
        cout << "You got a B!";
    break;
    case a < 100:
        cout << "You got an A!";
    break;
    
    return 0;
    
}
 }
There's something missing from line 3...
i wanna die OMG
Oh um... I fixed it and it still says compilation error
Lol... I'm guessing most of us here still make that mistake from time to time. Especially those of us who have to switch between languages often.

What error?

EDIT: I'm assuming it's that you have a path through your main function that doesn't return a value?

Look at where you've put your return statement.

Also, I'd strongly advise you to adopt a consistent, sensible indentation style. You would have seem the problem yourself much more easily, if you'd done this.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

int main() {

    std::cout << "what is your grade in percent? ";
    int a ;
    std::cin >> a; // *** note: >>

    if( a < 0 || a > 100 ) std::cout << "invalid input\n" ;
    else if( a < 60 ) std::cout << "You got an F\n";
    else if( a < 70 ) std::cout << "You got a D\n";
    else if( a < 80 ) std::cout << "You got a C\n";
    else if( a < 90 ) std::cout << "You got a B\n";
    else std::cout << "You got an A\n";
}
Thank You, MikeyBoy for the advice and thanks to you for the code, but i don't need the code I was just doing beginner challenges to get better using this language.
Topic archived. No new replies allowed.