Help to using Switch case in c++ ?

How do I use switch case in this code ??
I have tried several times , But I really do not know how to make it without error .

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
 
#include<iostream.h>
int x,y;
int sum(int a,int b)
{
   int c;
   c = a+b;
   return (c);
}
int sub (int a ,int b)
{
int c;
c = a-b ;
return (c);
}
int multi ( int a, int b )
{
int c ;
c = a*b;
return (c);
}
float div ( int a , int b)
{
float c;
c = a/b ;
return (c);
}
main()
{
cout<<"enter the value of x = ";
cin>>x;
cout<<"enter the value of y = ";
cin>>y;
cout<<"x + y  = "<< sum(x,y);
cout<<"\n x - y  = "<< sub(x,y);
cout<<"\n x * y  = "<< multi(x,y);
cout<<"\n x /y  = "<< div (x,y);
cin>>"\n";
}
I am sorry if I misunderstand, but a switch loop is used instead of a lot of if's and else if's. You seem to not be doing that so I don't see wherefore the switch would serve. Please tell me if i am wrong!
You are right in that the code contains neither an if-else nor a switch-case. You are also right that it can be used instead of a lot of if-else statements. One thing not right, it isn't any kind of loop.

See tutorial:
http://www.cplusplus.com/doc/tutorial/control/
Last edited on
Thanks for correcting me Chervil!
Topic archived. No new replies allowed.