making a program of addition and substraction

i am just a beginner and dont know a lot about c++ . i was trying to create a program which will prompt u to do addition or substraction
here is the code

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
# include <stdio.h>
# include <iostream>
using namespace std;
char response ;

int main()
{
    
    int a,b,c,d,e;
    
    cout << " would u like too  do addition or substraction?"<<endl;
    cout << " press 1 for add n 2 for subtract " << endl;
    cin>> response ;
    
    if (response >=1 )
    
             {   cout <<" enter ur first digit " <<endl;
                cin  >> a ;
                cout << " enter ur second digit"<< endl;
                cin>> b;
                cout << " the ans is " << (a+b) << endl;}
                
                else if (response >=2)
                {
                     cout <<" enter ur first digit " <<endl;
                cin  >> a ;
                cout << " enter ur second digit"<< endl;
                cin>>   b;
                cout << " the ans is " << (a-b) << endl;
                }   
                scanf ("%d%",&e);
                return 0;
                }

i am sure i have messed up a lot..if i press 1 it does addition if i press 2 it does addition ?? plz help?
Last edited on
That's because you wrote if(response >=1) instead of if(response<=1).
u
ur


*cringes*
Hello, for menu based programs i recommend you using the switch case

check it out here : http://msdn.microsoft.com/en-us/library/k0t5wee3(VS.80).aspx
ok lets forget (response <=1)
if i write (response==1)
n (response ==2)
and compile and then execute it i get a screen where i am told to enter 1 or 2
even if i enter 1 nothing happens and the same with 2
Last edited on
data type for variable response is char
it should be int

int response;
Last edited on
Topic archived. No new replies allowed.