a syntax error?

Hello, I've wrote this code
When I launch it, it displays everything.
for example if I write = 5, it will display like this
You got F
You got D
You got C
You got B
you got 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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
using namespace std;
void main()
{

double mark;
cout<<"Enter your mark = ";
cin>>mark;
if(mark==100)
{
	cout<<"You got a perfect score";

}
else if (mark<=59)
{
	cout<<"You got F\n";
}


if (mark>=60<=69);

{
	cout<<"You got D\n";

}

if(mark>=70<=79);
{
cout<<"You got C\n";
}
if(mark>=80<=89);
{
	cout<<"You got B\n";


}
if(mark>=90<=99);
{
	cout<<"You got A/n";
}












	main();
}
First of all use "else if" after "if" statements (instead of using multiple "if" statements)

The problem is mark>=60<=69 is not a valid statement. What you need to do is use and/or operands ( || for "or" and && for "and")

So for example your code on line 20 should look like this:
else if (mark>=60 && mark<=69)

And like i said in your previous post. Don't use ";" after if statements
Last edited on
I see, Thank you Fovv
And what's wrong with using ;'s?(Sorry for annoying newbish questions)

Okay I figured out why I shouldn't use ;
Thanks.
Last edited on
Thanks Fovv

Another question if I may
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
#include <iostream>
using namespace std;
void main()
{

double mark;
cout<<"Enter your mark = ";
cin>>mark;
if(mark==100)
{
	cout<<"You got a perfect score";

}
else if (mark<=59)
{
	cout<<"You got  F\n";
}


if  (mark>=60 && mark<=69)

{
	cout<<"You got  D\n";

}

else if (mark>=70 && mark<=79)
{
cout<<"You got an C\n";
}
if(mark>=80 && mark<=89)
{
	cout<<"You got  B\n";


}
else if(mark>=90 && mark<=99);                      
{
	cout<<"You got  A\n";

}


	main();
}

When I run the code and type in 65
I get a following message:
You got A
You get D
Why is that?
I'm also a newbie at programming haha.
I'm not entirely sure on the reason why, but it's in good programming practice not to put them after "if" statements.
Okay thanks
This is from Beginning c++ through game programming.

"you don't use a semicolon after the closing parenthesis of the expression you test in an if statement. If you were to do this, you'd create an empty statement that would be paired with the if statement, essentially rendering the if statement useless. "

Basically it ignores the if statement and displays all the information after the if statement, regardless of the argument being true or false.
Last edited on
Perhaps a little whitespace between each of your if/if else clauses might shed some light on the matter.

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
#include <iostream>
using namespace std;
void main()
{
 
   double mark;
   cout<<"Enter your mark = ";
   cin>>mark;

   if(mark==100)
   {
      cout<<"You got a perfect score";
 
   }
   else if (mark<=59)
   {
      cout<<"You got  F\n";
   }
 
 
   if  (mark>=60 && mark<=69)
   {
      cout<<"You got  D\n";
    }
   else if (mark>=70 && mark<=79)
   {
      cout<<"You got an C\n";
   }


   if(mark>=80 && mark<=89)
   {
      cout<<"You got  B\n";
   }
   else if(mark>=90 && mark<=99);
   {
      cout<<"You got  A\n";
   }
 
}


Also you should not be calling main(). The main() function can not be called recursively.

Sorry I didn't see you post the second set of code.

The problem is you left a semi colon after the last else if statement.


Also use int main() not void main()

You should revise your code to look something like this:
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
#include <iostream>
using namespace std;
int main()
{
    double mark;
    cout<<"Enter your mark = ";
    cin>>mark;
    if(mark==100)
    {
        cout<<"You got a perfect score";
    }
    
    else if (mark<=59)
    {
        cout<<"You got  F\n";
    }


    else if (mark>=60 && mark<=69)
    {
        cout<<"You got  D\n";
    }

    else if (mark>=70 && mark<=79)
    {
        cout<<"You got an C\n";
    }
    
    else if(mark>=80 && mark<=89)
    {
        cout<<"You got  B\n";
    }   

    else if(mark>=90 && mark<=99)                    
    {
        cout<<"You got  A\n";
    }
}
Last edited on
I see, thank you

Is there a reason why I should int main() instead of void main()?
This is quoted off another post in a different forum.

">whats the reason behind using int main over void main?
Because the standard dictates that every C/C++ program must return a value to indicate its success. It also says that a program must return either 0 or EXIT_SUCCESS if the execution was successful. Declaring main() as void doesn't mean the program won't return a value -- it just means that the value it returns will probably not be 0.

And that's bad because now the operating system thinks that the execution failed. Other horrible things can happen, too. The excuse "it works for me" is not valid because it's quite possible it will eventually screw you up, and you probably don't want to spend hours looking for a bug that came of bad coding habits."

More info:
http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1044841143&id=1043284376
http://users.aber.ac.uk/auj/voidmain.shtml
Thanks, will keep that in mind.
Topic archived. No new replies allowed.