Expected unqualified id before

I'm new to this and really confused how to fix this:

#include <iostream>

using namespace std;

int main()
{
char cChar;
double dfirstnumber;
double dsecondnumber;
char cDoagain;

}



{
do

system ("CLS")

cout << "Please enter the first number you would like to use";

cin >> dfirstnumber;

cout << "please enter the operation that you would like to complete" << "(+,-,* or /)"

cin >> cChar;

cout << "please enter the second number you would like to use";

cin >> dsecondnumber;

}

{ switch (cChar)

case '+' :
cout << "The answer is : " << dfirstnumber << "+" << dsecondnumber << " = " << (dfirstnumber + dsecondnumber)
break; //This is so it doesn't go through all of them if it finds the operation used.
case '-' :
cout << "The answer is : " << dfirstnumber << "-" << dsecondnumber << " = " << (dfirstnumber - dsecondnumber)
break;
case '*' :
cout << "The answer is : " << dfirstnumber << "*" << dsecondnumber << " = " << (dfirstnumber * dsecondnumber)
break;
case 'x' :
cout << "The answer is : " << dfirstnumber << "x" << dsecondnumber << " = " << (dfirstnumber * dsecondnumber)
break;
case 'X' :
cout << "The answer is : " << dfirstnumber << "X" << dsecondnumber << " = " << (dfirstnumber * dsecondnumber)
break;
case '/' :
if (dsecondnumber == 0)
cout << "That is an invalid operation" << endl
}
else
{
cout << "The answer is : " << dfirstnumber << "/" << dsecondnumber << " = " << (dfirstnumber / dsecondnumber)
break;
default:
cout << "That is an invalid operation" << endl
break;

cout << "Would you like to start again? (y or n)" << endl;
cin >> cDoagain;
while (cDoagain == 'Y' || cDoagain == 'y' );
cin.get();
return 0;
}
[/code]
The errors I get are line 16 expected unqualified-id before "{" token.
Line 35 expected unqualified-id before "{" token.
Line 56 expected unqualified-id before "else"
Your main() function ends at the first }, putting a punch of code in the global scope, most of which is illegal.
You program has many syntax errors, mainly missing/misplaced parentheses and semi columns(;). You're also using the system() function which is, besides evil, declared in windows.h, which you haven't included. Here's the fixed code (syntactically, there might be some logic errors):

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
55
56
57
58
59
60
61
#include <iostream>
#include <windows.h>
using namespace std;

int main()
{
    char cChar;
    double dfirstnumber;
    double dsecondnumber;
    char cDoagain;

    do
    {

    system("CLS");
    cout << "Please enter the first number you would like to use";

    cin >> dfirstnumber;

    cout << "please enter the operation that you would like to complete" << "(+,-,* or /)";

    cin >> cChar;

    cout << "please enter the second number you would like to use";

    cin >> dsecondnumber;

    switch (cChar){

    case '+' :
    cout << "The answer is : " << dfirstnumber << "+" << dsecondnumber << " = " << (dfirstnumber + dsecondnumber);
    break; //This is so it doesn't go through all of them if it finds the operation used.
    case '-' :
    cout << "The answer is : " << dfirstnumber << "-" << dsecondnumber << " = " << (dfirstnumber - dsecondnumber);
    break;
    case '*' :
    cout << "The answer is : " << dfirstnumber << "*" << dsecondnumber << " = " << (dfirstnumber * dsecondnumber);
    break;
    case 'x' :
    cout << "The answer is : " << dfirstnumber << "x" << dsecondnumber << " = " << (dfirstnumber * dsecondnumber);
    break;
    case 'X' :
    cout << "The answer is : " << dfirstnumber << "X" << dsecondnumber << " = " << (dfirstnumber * dsecondnumber);
    break;
    case '/' :
    if (dsecondnumber == 0)
    cout << "That is an invalid operation" << endl;
    else
    cout << "The answer is : " << dfirstnumber << "/" << dsecondnumber << " = " << (dfirstnumber / dsecondnumber);
    break;
    default:
    cout << "That is an invalid operation" << endl;
    break;}

    cout << "Would you like to start again? (y or n)" << endl;
    cin >> cDoagain;}
    while (cDoagain == 'Y' || cDoagain == 'y' );
    cin.get();
    return 0;
}
Last edited on
You're missing the code tag from the beginning of your post. :)

But regardless, this is pretty easy to fix. Essentially, you ended the program before you meant to by placing a rogue } symbol right after char cDoagain;. So, as of right now your int main() body looks like this to the compiler:

1
2
3
4
5
6
7
8
int main()
{
char cChar;
double dfirstnumber;
double dsecondnumber;
char cDoagain;

}


Also, { switch (cChar) should be switch (cChar) {, as the enclosing bracket comes after the switch statement. Same thing goes for your do statement. The brackets are used to tell the compiler that you want it to do all of this stuff enclosed in brackets during whatever kind of loop you're using.

In other words:

1
2
3
4
5
do
{
      // do all this stuff
      // do some more stuff
}while(something);


So, it looks like you need to go back and review some very basic fundamentals when it comes to the usage of brackets.
Last edited on
Topic archived. No new replies allowed.