simple calculator gone wron need help.

hi im a newbie her (forgive my poor english) i have an assignment to vreat a simple calculator that lets a user input 2 separate numbers and perform any chosen operation...

#include <iostream>

using namespace std;

int main()
{
int ichi, ni;
int a, b, c, d;
int sum, subs, m;
float qout;
int ans=ans;
labelA:
cout<<"Boss please enter the first Value: ";
cin>>ichi;
cout<<"\n";
cout<<"Boss please enter the second value: ";
cin>>ni;
cout<<"\n";
cout<<"Please select what operation you're going to use \n";
cout<<"#################################\n";
cout<<"a---Addition \n"<<"b---Subtraction \n"<<"c---Multiplication \n"<<"d---Division \n";
cout<<"#################################\n";
cin>>ans;
if(ans==a);
{
cout<<"The sum of the 2 numbers you have given is: "<<ichi+ni<<"\n";
goto labelA;
}
if(ans==b);
{
cout<<"The Difference of the 2 numbers you have given is "<<ichi-ni<<"\n";
goto labelA;
}
return 0;
}


i havent done the division and multiplication yet since i have encounter a problem on "if" the program only reads the if==a it wont read the if==b which is on subtraction part, and on the output it keeps on looping endlessly uncontrollable like crazy spamming >.< im lost please help.
Last edited on
well, I suggest you should first try and write the program without using goto since it is very dangerous. But the main problem is after all your if statements you have semicolons. Get rid of those.

The second problem is that you are trying to compare against integer values which have not been initialized. Try changing ans to type char and comparing the input against a character, like this:
if (ans == 'a')
Last edited on
Tnx for the answer :D i'am getting it now... i appreciate the quick reply sir...more powers to you and this site :)
#include <iostream>

using namespace std;

int main()
{
int ichi, ni;
char ans;
cout<<"Boss please enter the first Value: ";
cin>>ichi;
cout<<"\n";
cout<<"Boss please enter the second value: ";
cin>>ni;
cout<<"\n";
cout<<"Please select what operation you're going to use \n";
cout<<"#################################\n";
cout<<"a---Addition \n"<<"b---Subtraction \n"<<"c---Multiplication \n"<<"d---Division \n";
cout<<"#################################\n";
cin>>ans;

switch(ans){
case 'a':cout<<"The sum of the 2 numbers you have given is: "<<ichi+ni<<"\n";
break;
case 'b':
cout<<"The Difference of the 2 numbers you have given is "<<ichi-ni<<"\n";
break;
case 'c':
cout<<"The Multiplication of the 2 numbers you have given is "<<ichi*ni<<"\n";
break;
case 'd':
cout<<"The Difference of the 2 numbers you have given is "<<ichi/ni<<"\n";
break;
}

return 0;
}
why you dont use a switch? is more easy
ups:

case 'd':
cout<<"The Divisio of the 2 numbers you have given is "<<ichi/ni<<"\n";
break;
}
Topic archived. No new replies allowed.