I NEED HELP!

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;


return 0;
}



Any help is greatly appreciated!!!!
Last edited on
Remember: if your "if" statements extend for more than one line, you MUST enclose that block in curly brackets.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#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.
Last edited on
THANKS! That was a big help!
Topic archived. No new replies allowed.