### [Error] 'else' without a previous 'if'

It says that the else on line 53 isn't connected to an if. I am not sure what exactly it wants me to do here.

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273`` ``````#include using namespace std; int main() { double FirstNum, SecondNum, Output; int FirstNumInt, SecondNumInt, OutputInt; char oper; char cont = 0; while (cont == 0); { cout<<"Enter Two Numbers!"<>FirstNum; cin>>SecondNum; FirstNumInt = FirstNum; SecondNumInt = SecondNum; cout<<"Enter a mathmatical operator! Valid options are +, -, *, /, %, <, and >"<>oper; if (oper == '+') { Output = FirstNum + SecondNum; cout<') { if (FirstNum > SecondNum); {cout<<"True"<
You have some braces out of place, try this...

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374`` ``````#include using namespace std; int main() { double FirstNum, SecondNum, Output; int FirstNumInt, SecondNumInt, OutputInt; char oper; char cont = 0; while (cont == 0) { cout<<"Enter Two Numbers!"<>FirstNum; cin>>SecondNum; FirstNumInt = FirstNum; SecondNumInt = SecondNum; cout<<"Enter a mathmatical operator! Valid options are +, -, *, /, %, <, and >"<>oper; if (oper == '+') { Output = FirstNum + SecondNum; cout<') { if (FirstNum > SecondNum) {cout<<"True"<
Probably copy/paste errors.

You have mismatching {braces}

This is the code you have:
 ``1234567`` `````` else if (oper == '<') { if (FirstNum < SecondNum); {cout<<"True"<

Fixing the indentation to match the braces:
 ``12345678`` `````` else if (oper == '<') { if (FirstNum < SecondNum); // <- PS, this semicolon is wrong {cout<<"True"<

notice the extra } before the else is screwing you up.

Also... don't put semicolons after else (or if) statements.

OPINION EDIT:

This is one reason why I don't like to put other things on the same line as open or close braces. It makes mistakes like this harder to spot.
Last edited on
Thank you! My only problem now is that it says system was not declared in this scope. More brace problems?
Ah, nevermind that second question, I figured it out. Thank you very much for your help.
Well for starters you shouldn't be using system (see http://www.cplusplus.com/forum/articles/11153/ )

But if you really want to, you need to include the header that defines it. I think it's in <cstdlib>
Well, now it will compile, it just doesn't do anything.
if it does nothing that must mean oper is failing to get a value.

`cout << oper << endl;`
Still isn't working. It doesn't say anything at all.
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465`` ``````#include using namespace std; int main() { double FirstNum, SecondNum, Output; int FirstNumInt, SecondNumInt, OutputInt; char oper; char cont = 0; cout <<"Enter two numbers"<>FirstNum; cin>>SecondNum; FirstNumInt = FirstNum; SecondNumInt = SecondNum; cout<<"Enter a mathmatical operator such as +, -, *, /, %, <, and >"<< endl; cin>>oper; if (oper == '+') { Output = FirstNum + SecondNum; cout<') { if (FirstNum > SecondNum) { cout<<"True"<

Try something like that(removing if else and just using if and else).
This runs fine, however, I am sure it's not exactly what you need. Try tweaking this code into what you need.
Last edited on
All right, I am almost at the finish line. I hope you will all forgive me for my ignorance, this is the first real C++ program I have ever designed. I have this code.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374`` ``````#include #include using namespace std; int main() { double FirstNum, SecondNum, Output; int FirstNumInt, SecondNumInt, OutputInt; char oper; char cont = 0; while (cont == 0) { cout<<"Enter Two Numbers!"<>FirstNum; cin>>SecondNum; FirstNumInt = FirstNum; SecondNumInt = SecondNum; cout<<"Enter a mathmatical operator! Valid options are +, -, *, /, %, <, and >"<>oper; if (oper == '+') { Output = FirstNum + SecondNum; cout<') { if (FirstNum > SecondNum) {cout<<"True"<

And that code works, except if I enter N to terminate it it goes into an infinite loop. Other than that I think it works.
Wait, never mind, that actually does work! I was entering N as a number. When entered as an operator it works. Not the most robust program I admit, but it works! Thank you all for your help on this.
Topic archived. No new replies allowed.