[Error] 'else' without a previous 'if'


#include <iostream>
#include <math.h>
using namespace std;

int main()
{
float a,b,c,d,e,f,g,h,i;
float average;

cout << "this is the grading for students ;" << endl;
cout << "A = 90-98 ," << "B 85-89 ," << "C 80-84 ," << "D = 75-79 ," << "F = 74below: " << endl;
cout << "input grade in gen math: ";
cin >> a;
cout << "input grade in perdev: ";
cin >> b;
cout << "input grade in kommunikasyon: ";
cin >> c;
cout << "input grade in comprog: ";
cin >> d;
cout << "input grade in css: ";
cin >> e;
cout <<"input grade in els: ";
cin >> f;
cout <<"input grade in emtech: ";
cin >> g;
cout <<"input grade in oral: ";
cin >> h;
cout <<"input grade in pe: ";
cin >> i;
cout << "the total average is: " << (a + b + c + d + e + f + g + h + i) /9 << endl;


else if ((98 >= 90))
{
cout << "your grade is A, Exellent" << endl;
}
else if ((85 >= 89))
{
cout << "your grade is B, impressive" << endl;
}
else if ((80 >= 84))
{
cout << "your grade is C, goodjob" << endl;
}
else if ((75 >= 79))
{
cout << "your grade is D, verygood" << endl;
}
else if ((74 >= 65))
{
cout <<"your grade is F, maybe nexttime" << endl;
}

return 0;

}
Last edited on
the first else... what if goes with it? you can't just say 'else'. Else can ONLY follow an if statement.

also, your code does not make much sense.
yes, 74 is greater than or equal to 65.
did you mean to put a variable in that?! All your conditions are just checking constants, with a known and obvious result that didn't even need to be checked. They are ALL true.
you probably wanted something like:
double average = (a + b + c + d + e + f + g + h + i) /9.0;
if(average < 75)
cout << "fail"
else if(average < 80)
cout << "D" //which has never been "very good" for anyone not in special ed.
else... etc

c++ uses <cmath> not math.h .. it works, but it is a C header that does not properly register in the namespace.
Last edited on
Hello genesis224,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.


Using "else if" is fine, but the language needs an "if" statement to start the chain. Technically there is no "else if" in the language it really looks like:
1
2
3
4
5
6
7
else
{
    if
    {
        //code here.
    }
}

The nice part is that you can write
1
2
3
4
else if
{
    // code here.
}

and it works just fine.

Prefer to use "double"s over "float"s. The IDE and compilers these days tend to consider a floating point number a "double".

double a, b, c, d, e, f, g, h, i;. All these variable names are cute, but have no real meaning and are hard to keep track of. More so in larger programs.

The letters " i, j and k" tend to be used in for loops. Outside of a for loop these letters might be confusing.

Taking a closer look at you "else if" statements they will not work. else if ((98 >= 90)) this will always be true and nothing else will be checked.

You have a variable for average, (average), but never use it.

What you want is something like this:
1
2
3
4
5
if (average >= 90.0)
{
    cout << "your grade is A, Exellent" << endl;
}
else if (average >= 85.0)


Andy
We do not need all these variables (a,b,c,d,e,f etc.).
We can maintain a running total as the grades are entered.

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <cmath>

int main()
{
    std::cout << "compute the overall grade\n"
              << "A = 90 or above , B 85-89 , C 80-84 , D = 75-79 , F = 74 or below\n\n" ;

    double total = 0.0 ;
    int num_grades = 0 ;

    // get the grade for each course and add it to the total
    // auto: https://www.stroustrup.com/C++11FAQ.html#auto
    // range based loop: https://www.stroustrup.com/C++11FAQ.html#for
    for( const auto course : { "math", "perdev", "kommunikasyon", "comprog" /* etc. (add other subjects) */ } )
    {
        std::cout << "input grade in " << course << " [0 to 100]: " ;
        double grade ;
        if( std::cin >> grade && grade >= 0.0 && grade <= 100.0 ) // if a valid grade was entered
        {
             total += grade ; // add this grade to the total
             ++num_grades ; // increment the number of grades
        }

        else // input error: non-numeric input or the entered value is out of range
        {

             std::cout << "invalid input. run the program once again.\n" ;
             return 1 ; // give up; exit program 
             // (as of now, we haven't yet learnt how to recover from this kind of error).
        }

    } // end for

    // compute the average
    const double dbl_average = total / num_grades ;

    // round it to the nearest integer value
    // https://en.cppreference.com/w/cpp/numeric/math/round
    const long int_average = std::lround(dbl_average) ;

    std::cout << "\nyour average grade is " << int_average << "\nyour grade is " ;
    if( int_average > 89 ) std::cout << "A, excellent\n" ; // 90 or above
    else if( int_average > 84 ) std::cout << "B, impressive\n" ; // 85 to 89
    else if( int_average > 79 ) std::cout << "C, good\n" ; // 80 to 84
    else if( int_average > 74 ) std::cout << "D, ok\n" ; // 75 to 79
    else std::cout << "F, maybe next time\n" ; // below 75
}
Topic archived. No new replies allowed.