samble calculator with if and else ;

hi , i need to create a sample calculator with IF And Else

----------------
----------------------------------------------------------------------
#include<iostream>
using namespace std;
int main ()
{
int x,y;
char z;
cout<<("Plz Enter The Equl : ");
cin>>"%d",&x,"%c",&z ,"%d",&y;
cin>>"%d",&y;
cin>>"%c",&z;
if(z=='+')
cout<<"total = %d \n",x+y;
else
if (z=='-')
cout<<"subtract = %d",x-y;
else
if(z=='*')
cout<<"multiply = %d" ,x*y;
else
if(z=='/')
cout<<"divide = %d",x/y;
else
("invild mark");
}




}
Last edited on
Why don't you start with a tutorial? You have no idea what c++ syntax is.. http://www.cplusplus.com/doc/tutorial/
You can also go to youtube and get great intro C++ videos. I find it easier to see someone do examples than read documentation (that stuff is too dry).

http://www.youtube.com/user/antiRTFM

antiRTFM does a series called Spoonfeed, which would be a great way for you to get a jump start on C++ programming.
switch case would be better rather than using if and else
Last edited on
what is wrong in this code ?

--------------------------------------

#include<iostream>
using namespace std;
int main ()
{
int x,y;
char z;
cout<<("Plz Enter The Equl : ");
cin>>"%d",&x,"%c",&z ,"%d",&y;
cin>>"%d",&y;
cin>>"%c",&z;
if(z=='+')
cout<<"total = %d \n",x+y;
else
if (z=='-')
cout<<"subtract = %d",x-y;
else
if(z=='*')
cout<<"multiply = %d" ,x*y;
else
if(z=='/')
cout<<"divide = %d",x/y;
else
("invild mark");
}

---------------------------------------------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream>
using namespace std;
int main ()
{
int x,y;
char z;
cout<<("Plz Enter The Equl : ");
cin>>"%d",&x,"%c",&z ,"%d",&y;
cin>>"%d",&y;
cin>>"%c",&z;
if(z=='+')
cout<<"total = %d \n",x+y;
else 
if (z=='-')
cout<<"subtract = %d",x-y;
else 
if(z=='*')
cout<<"multiply = %d" ,x*y;
else 
if(z=='/')
cout<<"divide = %d",x/y;
else
("invild mark");
}
code tags, so that I can refer to lines by numbers.

line 7: not really a problem, but you don't need the parenthesis.
lines 8, 9, 10: that's some sort of crazy mix of cin >> and scanf. The right way is cin >> x >> z >> y;. operator >> acts differently depending on the type of the argument.
lines 12, 15, 18, 21: the same here. Right way is cout << "total = " << x+y;
line 23: ? You almost know how to do output, so why not write that here?
Topic archived. No new replies allowed.