Hi guys, i'm desperate for an answer here i cant figure this crap out. What i have to do is make a simple calculator that by pressing a number itl do a type of math and for some reason i cant get it to work. i have to use if statements and floats, nothing too complicated. Here is my code:
#include <iostream.h>
int main ()
{
cout<<"Welcome to the best FREE calculator you'll ever use!!\n\n";
float x,y,total,type;
cout<<"Select the number that corresponds to the type of math you wish to use.\n\n";
cout<<"1 = Addition\n";
cout<<"2 = Subtraction\n";
cout<<"3 = Divison\n";
cout<<"4 = Multiplication\n";
cout<<"5 = Size\n\n";
cin>>type;
if (type==1)
cout<<"Please enter the first number you wish to add:\n";
cin>>x;
cout<<"Please enter the second number you wish to add:\n";
cin>>y;
total= x+y;
cout<<"According to my calculations, the sum of the two numbers entered is: "<<total<<endl;
if (type==2)
cout<<"Please enter the first number you wish to subtract:\n";
cin>>x;
cout<<"Please enter the second number you wish to subtract:\n";
cin>>y;
total= x-y;
cout<<"Acording to my calculations, the difference of the two numbers entered is: "<<total<<endl;
if (type==3)
cout<<"Please enter the first number you wish to divide:\n";
cin>>x;
cout<<"Please enter the second number you wish to divide:\n";
cin>>y;
total= x/y;
cout<<"Acording to my calculations, the quotient of the two numbers entered is: "<<total<<endl;
if (type==4)
cout<<"Please enter the first number you wish to multiply:\n";
cin>>x;
cout<<"Please enter the second number you wish to multiply:\n";
cin>>y;
total= x*y;
cout<<"Acording to my calculations, the product of the two numbers entered is: "<<total<<endl;
if (type==5)
cout<<"Enter 2 numbers\n";
cin>>x>>y;
if (x>y)
cout<<x<<" is greater than "<<y<<endl;
if (y>x)
cout<<y<<" is greater than "<<x<<endl;
if (x==y)
cout<<x<<" is equal to "<<y<<endl;
#include <iostream.h>
int main ()
{
cout<<"Welcome to the best FREE calculator you'll ever use!!\n\n";
float x,y,total,type;
cout<<"Select the number that corresponds to the type of math you wish to use.\n\n";
cout<<"1 = Addition\n";
cout<<"2 = Subtraction\n";
cout<<"3 = Divison\n";
cout<<"4 = Multiplication\n";
cout<<"5 = Size\n\n";
cin>>type;
if (type==1)
{
cout<<"Please enter the first number you wish to add:\n";
cin>>x;
cout<<"Please enter the second number you wish to add:\n";
cin>>y;
total= x+y;
cout<<"According to my calculations, the sum of the two numbers entered is: "<<total<<endl;
}
if (type==2)
{
cout<<"Please enter the first number you wish to subtract:\n";
cin>>x;
cout<<"Please enter the second number you wish to subtract:\n";
cin>>y;
total= x-y;
cout<<"Acording to my calculations, the difference of the two numbers entered is: "<<total<<endl;
}
if (type==3)
{
cout<<"Please enter the first number you wish to divide:\n";
cin>>x;
cout<<"Please enter the second number you wish to divide:\n";
cin>>y;
total= x/y;
cout<<"Acording to my calculations, the quotient of the two numbers entered is: "<<total<<endl;
}
if (type==4)
{
cout<<"Please enter the first number you wish to multiply:\n";
cin>>x;
cout<<"Please enter the second number you wish to multiply:\n";
cin>>y;
total= x*y;
cout<<"Acording to my calculations, the product of the two numbers entered is: "<<total<<endl;
}
if (type==5)
{
cout<<"Enter 2 numbers\n";
cin>>x>>y;
if (x>y)
{
cout<<x<<" is greater than "<<y<<endl;
}
if (y>x)
{
cout<<y<<" is greater than "<<x<<endl;
}
if (x==y)
{
cout<<x<<" is equal to "<<y<<endl;
}
}
return 0;
}
... In fact, I always use curly brackets with my ifs, even if they're only one line long. It reduces the chances of introducing bugs later on a thousandfold.