Whats wrong with my code???

// code error in line five
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;
int main()
{  do while 
 int num=9;
    cout<<"Pick a number 1-10"<<endl;
    cin>> age;
    if age==9
{
    cout<<"correct"<<endl;
}
else if
{
    cout<<"incorrect"<<endl;
}
{
    while (num!=9){
    cout<<"try again"<<endl;
    return 0;
}

    
    
}

Last edited on
My error messages:

In function 'int main()':
Line 5: error: expected `(' before 'int'
compilation terminated due to -Wfatal-errors.
The problem is that you treat if incorrectly. It looks like so:
1
2
3
4
5
6
7
8
if (age==9) // Note: ()
{
cout<<"correct"<<endl;
}
else // Note: no if
{
cout<<"incorrect"<<endl;
}


Please use code tags: [code]Your code[/code]
See: http://www.cplusplus.com/articles/z13hAqkS/
Please put code tags around code.

The proper syntax for do-while is:
1
2
3
4
5
do
{
  // things
}
while (/*condition*/);
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;
int main()
{
     do {
 int age=9;
    cout<<"Pick a number 1-10"<<endl;
    cin>> age;
    if (age==9)
{
    cout<<"correct"<<endl;
}
else
{
    cout<<"incorrect"<<endl;
          }while (age!=9);
}
 
}




So I corrected some things but I still get these:

1 In function 'int main()':
2 Line 19: error: expected `while' before '}' token
3 compilation terminated due to -Wfatal-errors.
The errors would be a lot easier to see if you properly indented:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;
int main()
{
    do
    {
        int age=9;
        cout<<"Pick a number 1-10"<<endl;
        cin>> age;
        if (age==9)
        {
            cout<<"correct"<<endl;
        }
        else
        {
            cout<<"incorrect"<<endl;
        }while (age!=9);
    }
}


Notice how your 'while' statement is tacked on to the end of the 'else' block, not at the end of the 'do' block.
Topic archived. No new replies allowed.