#include <iostream>
using namespace std;

int main ()
{	
	int choice,num1,num2,answer;
	cout<<"This is a caculator program which will do math for you\n";
	cout<<"Please Choose From one of 4 math operators\n";
	cout<<"1 +\n";
	cout<<"2 *\n";
	cout<<"3 -\n";
	cout<<"4 \\n";
	cin>>choice;
if (choice==1)
{
	cout<<"please enter in two numbers to add" <<answer<<endl;
	cin>>num1;
	cout<<"you entered"<<num1<<endl;
	cin>>num2;
	cout<<"you entered"<<num2<<endl;
	answer=num1+num2;
	cout<<"the answer is "<<answer<<endl;
}
else if(choice==2)
(
	cout<<"please enter in two numbers to multiply" <<answer<<endl;
	cin>>num1;
	cout<<"you entered"<<num1<<endl;
	cin>>num2;
	cout<<"you entered"<<num2<<endl;
	answer=num1*num2;
	cout<<"the answer is "<<answer<<endl;
	)
else if (choice==3)
(
 
cout<<"please enter in two numbers to subtract" <<answer<<endl;
	cin>>num1;
	cout<<"you entered"<<num1<<endl;
	cout<<"num1/\n";
	cin>>num2;
	cout<<"you entered"<<num2<<endl;
	answer=num1-num2;
	cout<<"the answer is "<<answer<<endl;
	
	)
 else if(choice==4)

 
cout<<"please enter in two numbers to divide" <<answer<<endl;
	cin>>num1;
	cout<<"you entered"<<num1<<endl;
	cin>>num2;
	cout<<"you entered"<<num2<<endl;
	answer=num1/num2;
	cout<<"the answer is "<<answer<<endl;

	
return 0;
)

i know i am doing something wrong with the if else statements but i cant figure out what it is the program seems to look right but its giving me errors such as illegal else/if and syntax errors i know overall it is almost there if someone can guide me through the last part i would appreciate it greatly

Recommended Answers

All 5 Replies

your last else if is missing an opening and closing brace. { }

And in a couple of spots, you are using parenthesis where they should be braces.

#include <iostream>
using namespace std;

int main ()
{
    int choice,num1,num2,answer;
    cout<<"This is a caculator program which will do math for you\n";
    cout<<"Please Choose From one of 4 math operators\n";
    cout<<"1 +\n";
    cout<<"2 *\n";
    cout<<"3 -\n";
    cout<<"4 \\n";
    cin>>choice;
    if (choice==1){
        cout<<"please enter in two numbers to add" <<answer<<endl;
        cin>>num1;
        cout <<"you entered"<<num1<<endl;
        cin>>num2;
        cout<<"you entered"<<num2<<endl;
        answer=num1+num2;
        cout<<"the answer is "<<answer<<endl;
    } else if(choice==2) {
        cout<<"please enter in two numbers to multiply" <<answer<<endl;
        cin>>num1;
        cout<<"you entered"<<num1<<endl;
        cin>>num2;
        cout<<"you entered"<<num2<<endl;
        answer=num1*num2;
        cout<<"the answer is "<<answer<<endl;
    } else if (choice==3) {
        cout<<"please enter in two numbers to subtract" <<answer<<endl;
        cin>>num1;
        cout<<"you entered"<<num1<<endl;
        cout<<"num1/\n";
        cin>>num2;
        cout<<"you entered"<<num2<<endl;
        answer=num1-num2;
        cout<<"the answer is "<<answer<<endl;
    } else if(choice==4) {
        cout<<"please enter in two numbers to divide" <<answer<<endl;
        cin>>num1;
        cout<<"you entered"<<num1<<endl;
        cin>>num2;
        cout<<"you entered"<<num2<<endl;
        answer=num1/num2;
        cout<<"the answer is "<<answer<<endl;
    }


return 0;
}

And you also might want to change line 10 ( cout << "4 \\n"; ) to: cout << "4 \\\n"; Your line will output 4 \ n , but I think you want your output to be: 4 \ [newline] read more about escape-characters here

thank you i fixed that and now i am getting an error that says answer is not being initalized when i get it to run and key in my selection?

#include <iostream>
using namespace std;

int main ()
{	
	int choice,num1,num2,answer;
	cout<<"This is a caculator program which will do math for you\n";
	cout<<"Please Choose From one of 4 math operators\n";
	cout<<"1 +\n";
	cout<<"2 *\n";
	cout<<"3 -\n";
	cout<< "4 \\\n";  
	cin>>choice;
if (choice==1)
{
	cout<<"please enter in two numbers to add" <<answer<<endl;
	cin>>num1;
	cout<<"you entered"<<num1<<endl;
	cin>>num2;
	cout<<"you entered"<<num2<<endl;
	answer=num1+num2;
	cout<<"the answer is "<<answer<<endl;
}
else if(choice==2)
{
	cout<<"please enter in two numbers to multiply" <<answer<<endl;
	cin>>num1;
	cout<<"you entered"<<num1<<endl;
	cin>>num2;
	cout<<"you entered"<<num2<<endl;
	answer=num1*num2;
	cout<<"the answer is "<<answer<<endl;
}
else if (choice==3)
{
 
cout<<"please enter in two numbers to subtract" <<answer<<endl;
	cin>>num1;
	cout<<"you entered"<<num1<<endl;
	cout<<"num1/\n";
	cin>>num2;
	cout<<"you entered"<<num2<<endl;
	answer=num1-num2;
	cout<<"the answer is "<<answer<<endl;
	
}
 else if(choice==4)

 
cout<<"please enter in two numbers to divide" <<answer<<endl;
	cin>>num1;
	cout<<"you entered"<<num1<<endl;
	cin>>num2;
	cout<<"you entered"<<num2<<endl;
	answer=num1/num2;
	cout<<"the answer is "<<answer<<endl;

	
return 0;
}

When posting c++ code, please use c++ code tags
[code=c++] // Your code here

[/code]

Don't try to print answer on the 'please enter in two numbers' lines.

the answer shouldn't appear in the code until its have its own value.
i test your code and the following its run correctly:

#include <iostream>
using namespace std;

int main ()
{	
	int choice,num1,num2,answer;
	cout<<"This is a caculator program which will do math for you\n";
	cout<<"Please Choose From one of 4 math operators\n";
	cout<<"1 +\n";
	cout<<"2 *\n";
	cout<<"3 -\n";
	cout<<"4 \\n";
	cin>>choice;

if (choice==1)
{
	cout<<"please enter in two numbers to add" <<endl;
	cin>>num1;
	cout<<"you entered"<<num1<<endl;
	cin>>num2;
	cout<<"you entered"<<num2<<endl;
	answer=num1+num2;
	cout<<"the answer is "<<answer<<endl;
}
else if(choice==2)
{
	cout<<"please enter in two numbers to multiply" <<endl;
	cin>>num1;
	cout<<"you entered"<<num1<<endl;
	cin>>num2;
	cout<<"you entered"<<num2<<endl;
	answer=num1*num2;
	cout<<"the answer is "<<answer<<endl;
}
else if (choice==3)
{
 
cout<<"please enter in two numbers to subtract" <<endl;
	cin>>num1;
	cout<<"you entered"<<num1<<endl;
	cout<<"num1/\n";
	cin>>num2;
	cout<<"you entered"<<num2<<endl;
	answer=num1-num2;
	cout<<"the answer is "<<answer<<endl;
	
}
 else if(choice==4)

 {
cout<<"please enter in two numbers to divide" <<endl;
	cin>>num1;
	cout<<"you entered"<<num1<<endl;
	cin>>num2;
	cout<<"you entered"<<num2<<endl;
	answer=num1/num2;
	cout<<"the answer is "<<answer<<endl;
}

	
return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.