Oct 10, 2011 at 1:36pm UTC
Hey all..Im a beginner in c++ programming..
I have problem in my program that ive written..i would we very thankful if you point out my mistakes cuz programme is not running as it should.
calculator programme using switch codes
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
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
float x,y;
int choice;
cout<<"Enter 1,2,3,4 for +,-,x,/ respectively" <<endl;
cin>>choice;
cout<<"Enter the numbers" <<endl;
cin>>x>>y;
switch (choice)
case1 :
cout<<x<<" + " <<y<<" = " <<x+y;
cout<<endl;
case2 :
cout<<x<<" - " <<y<<" = " <<x-y;
cout<<endl;
case3 :
cout<<x<<" x " <<y<<" = " <<x*y;
cout<<endl;
case4 :
if (y==0)
cout<<"division not possible" <<endl;
else
cout<<x<<" / " <<y<<" = " <<x/y;
cout<<endl;
getch();
}
Output is
Enter 1,2,3,4 for +,-,x,/ respectively
1
Enter the numbers
20
25
20 - 25 = -5
20 x 25 = 500
20 / 25 = 0.8
So thats the problem..i entered 1,programme should have added 20 and 25 but u can see it did something else..where is my mistake?
Last edited on Oct 10, 2011 at 1:37pm UTC
Oct 10, 2011 at 1:49pm UTC
You need to break your cases, otherwise it will continue. For example:
switch (choice)
case 1: cout<<x<<" + "<<y<< = <<x+y<<endl;
break;
doing that for all your cases should sort your problem out.
Hope that helps :)
PS sorry if formatting or spelling is weird, im on my iphone
Oct 11, 2011 at 5:32pm UTC
Because you forgot to add break at the end of your switch statement.