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.
//
// Calculator program.
// Just a learning program
#include <cstdio>
#include <cstdlib>
#include <iostream>
usingnamespace 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
elseif (z==d)
{
l=x/y;
}
//multiplication
elseif (z==m)
{
l=x*y;
}
//subtraction
elseif (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.
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