Error and if needed before else

Ellow guys, i've been messing around and also got the Code Blocks now, and i've been trying to work out with this code,

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
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string answer=("Yes","yes","No","no");
    string operation=("Multiply","multiply","Summarize","summarize","Substract","substract","Divide","divide");
    int x,y;


    cout << "Hello,my name is Alpha,please state your name before we continue: " << endl;
    string name;
    cin >> name;
    cout << name << ", Good to meet you. Shall we Continue? (Yes or No) " << endl;
    cin >> answer;

    if(answer=="Yes","yes"){cout << "Alright " << name << " what would you like to do? (Multiply,summarize,substract,divide)"  << endl;}
    else{
        if(answer=="No","no"){cout << "Alright " << name << " Hope to talk with you soon again. Have a good time!" << endl;} }

        cin >> operation;

        if(operation=="Multiply","multiply"){cout << "Please give me your first integer: " << endl; }
        cin >> x;
        cout << x << ",alright now give me your second integer: " << endl;
        cin >> y;
        cout << x*y << " There you go, anything else?(Multiply,summarize,substract,divide"  << endl;
        cin >> operation;

        else{
            if(answer=="Summarize","summarize"){cout << "Summarizing eh? Ok,give me your first integer: " << endl;}
            cin >> x;
            cout << x << ",and your second integer?" << endl;
            cin >> y;
            cout << x+y << ",there's your result! Got anything more challenging for me?(Multiply,summarize,substract,divide)" << endl;
            cin >> operation;}

            else{
            if(answer=="Substract","substract"){cout << "Oh i love substracting, give me your first integer: " << endl;}
            cin >> x;
            cout << x << ", and your second integer?" << endl;
            cin >> y;
            cout << x-y << ", hows that for super speedy answer! Do you have anything else that you need me to do?(Multiply,summarize,substract,divide)" << endl;
            cin >> operation;}

            else{
            if(answer=="Divide","divide"){cout << "Division, hmmmm, alright,give me your first integer: " << endl;}
            cin >> x;
            cout << x << ",what would your second integer be?" << endl;
            cin >> y;
            cout << x/y << ",i think i did right! I hate division... well" << name << " got anything else for me?(Multiplay,summarize,substract,divide)" << endl;
            cin >> operation; }



    return 0;

}


I've been struggling for an hour now but i cant seem to figure a fix for this, these are the only errors that im getting while trying to compile:

D:\PROGRAMMING SECTION\InteractiveCalculatro vCodeBlocks\Interactive Calculator\main.cpp|32|error: 'else' without a previous 'if'|

D:\PROGRAMMING SECTION\InteractiveCalculatro vCodeBlocks\Interactive Calculator\main.cpp|40|error: 'else' without a previous 'if'|

D:\PROGRAMMING SECTION\InteractiveCalculatro vCodeBlocks\Interactive Calculator\main.cpp|48|error: 'else' without a previous 'if'|


Would anyone be kind enough to spare some time and guide me through this? and/or give me some feedback on efficiency of this code :(

The error is exactly what the message says, you have stray elses in the middle of the landscape:

1
2
3
 cin >> operation;

 else{
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string answer=("Yes","yes","No","no");
    string operation=("Multiply","multiply","Summarize","summarize","Substract","substract","Divide","divide");
    int x,y;


    cout << "Hello,my name is Alpha,please state your name before we continue: " << endl;
    string name;
    cin >> name;
    cout << name << ", Good to meet you. Shall we Continue? (Yes or No) " << endl;
    cin >> answer;

    if(answer=="Yes","yes"){cout << "Alright " << name << " what would you like to do? (Multiply,summarize,substract,divide)"  << endl;}
    else{
        if(answer=="No","no"){cout << "Alright " << name << " Hope to talk with you soon again. Have a good time!" << endl;} }

        cin >> operation;

    if(operation=="Multiply","multiply"){cout << "Please give me your first integer: " << endl;}
        cin >> x;
        cout << x << ",alright now give me your second integer: " << endl;
        cin >> y;
        cout << x*y << " There you go, anything else?(Multiply,summarize,substract,divide"  << endl;
        cin >> operation;
    else{
            if(answer=="Summarize","summarize"){cout << "Summarizing eh? Ok,give me your first integer: " << endl;}
            cin >> x;
            cout << x << ",and your second integer?" << endl;
            cin >> y;
            cout << x+y << ",there's your result! Got anything more challenging for me?(Multiply,summarize,substract,divide)" << endl;
            cin >> operation;}
    else{
            if(answer=="Substract","substract"){cout << "Oh i love substracting, give me your first integer: " << endl;}
            cin >> x;
            cout << x << ", and your second integer?" << endl;
            cin >> y;
            cout << x-y << ", hows that for super speedy answer! Do you have anything else that you need me to do?(Multiply,summarize,substract,divide)" << endl;
            cin >> operation;}
    else{
            if(answer=="Divide","divide"){cout << "Division, hmmmm, alright,give me your first integer: " << endl;}
            cin >> x;
            cout << x << ",what would your second integer be?" << endl;
            cin >> y;
            cout << x/y << ",i think i did right! I hate division... well" << name << " got anything else for me?(Multiplay,summarize,substract,divide)" << endl;
            cin >> operation; }



    return 0;

}



Still gives the exact same error :(
Last edited on
First, format your code properly. Curly braces each go on their own line, the code between them must be indented. Once you're done with that, it will be easy to see any mismatched elses.

1
2
3
4
5
6
7
8
if (answer=="Yes" || answer=="yes")
{
    //code
}
else
{
    //code
}


Also, note the changed if condition. The comma operator doesn't do what you think it does:
http://www.cplusplus.com/doc/tutorial/operators/
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
nt main()
{
    string answer=("Yes","yes","No","no");
    string operation=("Multiply","multiply","Summarize","summarize","Substract","substract","Divide","divide");
    int x,y;


    cout << "Hello,my name is Alpha,please state your name before we continue: " << endl;
    string name;
    cin >> name;
    cout << name << ", Good to meet you. Shall we Continue? (Yes or No) " << endl;
    cin >> answer;

    if(answer=="Yes","yes"){
		cout << "Alright " << name << " what would you like to do? (Multiply,summarize,substract,divide)"  << endl;
	} 
	else {
        if(answer=="No","no"){
			cout << "Alright " << name << " Hope to talk with you soon again. Have a good time!" << endl;
		} 
	}

    cin >> operation;

    if(operation=="Multiply","multiply"){
		cout << "Please give me your first integer: " << endl;
	}
    cin >> x;
    cout << x << ",alright now give me your second integer: " << endl;
    cin >> y;
    cout << x*y << " There you go, anything else?(Multiply,summarize,substract,divide"  << endl;
    cin >> operation;
            else


After reformatting for legibility, the problem is you have intervening code between the closing bracket of the last if I cited and the else. The compiler is taking that as the end of the conditional. So you in fact do have a dangling else.

In fact, you have the problem in several places in your code.
Last edited on
Thank you for your link :D and advice, the errors are now gone and the code is compilable ^^

thanks, this is the final code

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 <string>

using namespace std;

int main()
{
    string answer=("Yes","yes","No","no");
    string operation=("Multiply","multiply","Summarize","summarize","Substract","substract","Divide","divide");
    int x,y;


    cout << "Hello,my name is Alpha,please state your name before we continue: " << endl;
    string name;
    cin >> name;
    cout << name << ", Good to meet you. Shall we Continue? (Yes or No) " << endl;
    cin >> answer;

    if(answer=="Yes" || "yes"){cout << "Alright " << name << " what would you like to do? (Multiply,summarize,substract,divide)"  << endl;}
    else{
        if(answer=="No" || "no"){cout << "Alright " << name << " Hope to talk with you soon again. Have a good time!" << endl;} }

        cin >> operation;

    if(operation=="Multiply" || "multiply")
    {cout << "Please give me your first integer: " << endl;
        cin >> x;
        cout << x << ",alright now give me your second integer: " << endl;
        cin >> y;
        cout << x*y << " There you go, anything else?(Multiply,summarize,substract,divide)"  << endl;
        cin >> operation;}
    else{
            if(answer=="Summarize" || "summarize")
            {cout << "Summarizing eh? Ok,give me your first integer: " << endl;
            cin >> x;
            cout << x << ",and your second integer?" << endl;
            cin >> y;
            cout << x+y << ",there's your result! Got anything more challenging for me?(Multiply,summarize,substract,divide)" << endl;
            cin >> operation;}
    else{
            if(answer=="Substract" || "substract")
            {cout << "Oh i love substracting, give me your first integer: " << endl;
            cin >> x;
            cout << x << ", and your second integer?" << endl;
            cin >> y;
            cout << x-y << ", hows that for super speedy answer! Do you have anything else that you need me to do?(Multiply,summarize,substract,divide)" << endl;
            cin >> operation;}
    else{
            if(answer=="Divide" || "divide")
            {cout << "Division, hmmmm, alright,give me your first integer: " << endl;
            cin >> x;
            cout << x << ",what would your second integer be?" << endl;
            cin >> y;
            cout << x/y << ",i think i did right! I hate division... well" << name << " got anything else for me?(Multiplay,summarize,substract,divide)" << endl;
            cin >> operation; } } } }



    return 0;

}
Last edited on
All of the conditions are still wrong. The indendation (or the lack of it) is too.

Here, one run of AStyle later:

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
#include <string>

using namespace std;

int main()
{
  string answer=("Yes","yes","No","no");
  string operation=("Multiply","multiply","Summarize","summarize","Substract","substract","Divide","divide");
  int x,y;


  cout << "Hello,my name is Alpha,please state your name before we continue: " << endl;
  string name;
  cin >> name;
  cout << name << ", Good to meet you. Shall we Continue? (Yes or No) " << endl;
  cin >> answer;

  if (answer=="Yes" || "yes")
  {
    cout << "Alright " << name << " what would you like to do? (Multiply,summarize,substract,divide)"  << endl;
  }
  else
  {
    if (answer=="No" || "no")
    {
      cout << "Alright " << name << " Hope to talk with you soon again. Have a good time!" << endl;
    }
  }

  cin >> operation;

  if (operation=="Multiply" || "multiply")
  {
    cout << "Please give me your first integer: " << endl;
    cin >> x;
    cout << x << ",alright now give me your second integer: " << endl;
    cin >> y;
    cout << x*y << " There you go, anything else?(Multiply,summarize,substract,divide)"  << endl;
    cin >> operation;
  }
  else
  {
    if (answer=="Summarize" || "summarize")
    {
      cout << "Summarizing eh? Ok,give me your first integer: " << endl;
      cin >> x;
      cout << x << ",and your second integer?" << endl;
      cin >> y;
      cout << x+y << ",there's your result! Got anything more challenging for me?(Multiply,summarize,substract,divide)" << endl;
      cin >> operation;
    }
    else
    {
      if (answer=="Substract" || "substract")
      {
        cout << "Oh i love substracting, give me your first integer: " << endl;
        cin >> x;
        cout << x << ", and your second integer?" << endl;
        cin >> y;
        cout << x-y << ", hows that for super speedy answer! Do you have anything else that you need me to do?(Multiply,summarize,substract,divide)" << endl;
        cin >> operation;
      }
      else
      {
        if (answer=="Divide","divide")
        {
          cout << "Division, hmmmm, alright,give me your first integer: " << endl;
          cin >> x;
          cout << x << ",what would your second integer be?" << endl;
          cin >> y;
          cout << x/y << ",i think i did right! I hate division... well" << name << " got anything else for me?(Multiplay,summarize,substract,divide)" << endl;
          cin >> operation;
        }
      }
    }
  }

  return 0;
}
string answer=("Yes","yes","No","no");
What the hell is this?

Here, try this code:
1
2
3
4
5
6
7
8
9
#include <iostream>
#include <string>
using namespace std;

int main()
{
  string answer=("Yes","yes","No","no");
  cout << answer;
}


It's been said before...
The comma operator doesn't do what you think it does:

.. and I'll say it again. The comma operator doesn't do what you think it does.
Last edited on
Woah ^^ , thank you guys so much, i am a total newbie at this but trying to get the basics right :) , practicing a lot, and you guys did correct many of my mistakes by now,thank you :)
Topic archived. No new replies allowed.