Problem with calculator program

Hi, I am very new to C++ and programming in general. I decided to make a simple calculator program to help cement what I have learned so far, but I keep getting random outputs. I have determined that this is caused by the char variables and have gotten it working without them, but i have to use 1 for addition, 2 for subtraction, etc. I'm sure this is just something simple that I am missing, but I just can't seem to make it work. 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
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
//
// Calculator program.
// Just a learning program
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

int main(int nNumberofArgs, char* pszArgs[])
{
    //declaring variables
    int l;
    int x;
    int y;
    char z;
    char a;
    char s;
    char m;
    char d;
    cout<< "Enter a number: ";
    cin>>x;
    cout<<"\nNow do you want to add(a), subtract(s)"
        <<" multiply(m), or divide(d)? ";
    cin>>z;
    cout<<"\nAnd now enter another number: ";
    cin>> y;
    for(;;)
    {
    //addition
    if (z == a)
    {
        l= (x+y);
    }
    //division
    else if (z==d)
    {
        l=x/y;
    }
    //multiplication
    else if (z==m)
    {
        l=x*y;
    }
    //subtraction
    else if (z==s)
    {
        l=x-y;
    }
    cout<<"\n\nThe total is: "
        << l
        <<"\n";
        break;
}
    

    
    //wait until the user is ready before terminating the program
    //to allow the user to see the program results
    system("PAUSE");
    return 0;
}



Thanks for any help and sorry if this is just a simple noob mistake.
Last edited on
remove lines z to 19. what you want are constants and not variables. A char constant is expressed like so 'a' or upper case 'A'. So change line 30 to if (z == 'a') and the other ifs accordingly
Thank you so much for the fast reply. That did it.
Topic archived. No new replies allowed.