Mikehalo2

Ok I'm new to programming. This is my second program, its an addition and subtraction calculator. I keep getting this error when I try to compile

error C2059: syntax error : 'if'

Here is My program

//

//Program that adds and subtracts integers

//

#include <stdio.h>

#include <iostream>

using namespace std;

int main(int nNumberofArgs , char* pszArgs[])

{

//Enter Problem type

cout << "What kind of problem would you like to do :"

;cin >> if"Add:" goto Add;

;cin >> if"Subtract:" goto Subtract;

Add:

//Addition Problems

int Variable1;

cout << "Enter first number of equation:"

;cin >> Variable1;

int Variable2;

cout << "Enter second number of equation:"

;cin >> Variable2;

int Answer;

Answer = Variable1 + Variable2;

cout << "The answer is:";

cout << Answer;

return 0;

};

Any help or advice on how to fix this would be appreciated



Re: Visual C++ Express Edition error C2059: syntax error : 'if'

Mikehalo2

Also note I have not written the subtraction part yet





Re: Visual C++ Express Edition error C2059: syntax error : 'if'

nobugz

This is simply not valid C++. I don't see a lot of point in trying to explain why, get a book before you commit anymore gotos.





Re: Visual C++ Express Edition error C2059: syntax error : 'if'

Pintu Shukla

As Already mention that above Code is not a valid C++ code. Just go through any good book first and then try again. if and switch is used for condition checking and you can use it in the following manner

Note :- Following code is just a idea .

Code Block

#include <stdio.h>

#include <iostream>

using namespace std;

int main(int nNumberofArgs , char* pszArgs[])

{

char ch='a';

cout << "What kind of problem would you like to do a :addition ,s for subtraction :";

cin>>ch;

//Now you can check it like

if((ch=='a')||(ch=='A'))

{

//perform addition here

}

else

{

}

return 0;

}

So better pick some good book and read it carefully .and remember it is not a good practice to use goto in your program.

Thanx






Re: Visual C++ Express Edition error C2059: syntax error : 'if'

Mikehalo2

I guess I should have read up more. I have two good books, just thought I would try something off what I learned so far. Thanks guys




Re: Visual C++ Express Edition error C2059: syntax error : 'if'

Mikehalo2

I just got it working thanks to Pintu's code and part of code I wrote. Thanks alot

PS. The book I'm using is about 8 years old so Im going to get a new one. I guess the code changes alot overtime.





Re: Visual C++ Express Edition error C2059: syntax error : 'if'

Pintu Shukla

you welcome dude . Don't forget to close the thread.

Thanx