Expected Primary-expression

I've been looking through this code for 2 hours, been on google.
But i still can't fing the answer to this:

expected primary-expression before "else"


The problem is in part of the if and else-if section but I don't know how to set it right.


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
#include <iostream>
#include <windows.h>
#include <string>
using namespace std;


int main ()
{
    string mystr;
    int a;
    cout << "Welcome Competitor!\n\a";
    Sleep (3000);
    cout << "Your name:" ;
    getline (cin, mystr);
    Sleep (2000);
    cout << "Name confirmed. \n\a" ;
    cout << "Hello "; cout << mystr; cout << " and welcome.\a\n" ;
    Sleep (5000);
    cout << "Your age:" ;
    cin >> a ;
    cout << "Age confirmed\n\a";
    cout << "Your are "; cout << a ; cout << "\n\n\a";
    Sleep (5000) ;
    
    
if (a ==1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39 );
  {  
      cout << "You are "; cout << a ; cout << ", so your age is an-odd number" ;
      Sleep (8000);
   }     
else if (a =="");
           {
             cout << "Well its seems you have no age, that's a big shame" ;
           Sleep (5000);
           }
else if (a == 2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40);
      {    
        cout << "You are " cout << a ; cout << ", so you are still ";
        cout << "younger than me" ;
         Sleep (10000) ; 
           
       }   
else
       { 
         cout << "" ;
         Sleep (6000);   
       }
       
    return 0;
}    





- Thanks
if statements don't need an ';'

Actually if you do that, then the if statement becomes an empty or Null statement meaning that the following code within those { } braces do not belong to the if-else condition and will run no matter what.

IE:
1
2
if( i == 1)
  i = 5;


No need for {} because the compiler understands that just that line belongs in the if statement. It will only run that code if i is 1.

1
2
3
if( i == 1 )   //this is the same as if(i == 1);
   ;
i = 5;


In this example, it has become Null, no code is attached to the if statement and i = 5; will always run no matter the value of i. The reason I'm saying never use a ';' at the end of a if like you did is because there will be a use for that particular statement sometimes...just not in your example.

I don't know what happens when you have a comma inside a if statement like that but it is not general practice. If you want to compare multiple statements you can do something like:

if( a==1 || a==3 )

However in your case I see it is a series of odd numbers starting from 1 and ending in 39, so here is the condition for that:

 
if( 1 <= a && a <= 39 && a%2==1 ) //ask if you don't know what % does 


Next, you have
 
else if (a=="")  

a is of type int you cannot compare it with a string.

Incorrect syntax missing ';'
 
cout << "You are " cout << a ; cout << ", so you are still ";

Last edited on
Thanks. I somehow knew the semicolon wasn't supposed to be at the end of the if statements...........
Note that you can 'chain' desired output using cout and multiple <<
so
1
2
 
    cout << "You are "; cout << a ; cout << ", so your age is an-odd number" ;

can be replaced by
 
     cout << "You are " << a << ", so your age is an-odd number" ;

which is often easier to read.
Topic archived. No new replies allowed.