This week I have to make a 4 function calcualtor ( + - / *)
But here is the catch...I have to enter a number, enter a operator, then enter another number. I can write the code for (num1, num2, operator) but thinking this one out is KILLING me..
I have the loop created for X/x to exit the program, and C/c to clear the calc...but how in the heck should I approach having a number, an operator, and then another number?
Here is what I have so far (psuedocode included that I'm still trying to code from)
#include <iostream>
usingnamespace std;
#include <stdlib.h>
void main ()
{
//read first number
long L1;
long L2;
long L3;
cout << "Enter a number ";
cin >> L1 ;
//Read in the operator
// Is the operator valid?
//yes - continue
//no - go back and read operater again
// Is the operator X?
// yes-stop
// no - go on with program
// Is operator C?
// yes - (option 1 ) reset first # to 0, go get another operator
// yes- (option 2) go read another first number
// no - go on with program
// doing math
// read second number
// do math operation
// Print result
// Go read in another operator
char Op;
bool Valid;
//so were going to do a do-while loop..it always does the loop at least once
do{
Valid = true;
cout << "Enter an operator (+ - / * C c X x): " ;
cin >> Op; //read in an operator
switch (Op)
{
case'+':
case'-':
case'*':
case'/':
Valid = true;
break;
case'X':
case'x':
cout << " Terminiating Program " <<endl;
exit(0); //this causes the program to immediatly terminate
case'C':
case'c':
//we want C to clear the calc
cout << "Clearing the Calculator " <<endl;
Valid = false;
break;
default:
Valid = false;
}
} while (!Valid);
cout << "Enter another number" << endl;
cin >> L2 ;
}
Any hints? How do can I tie the L1 (Op) L2 together at the end? Its driving me nuts!
Or you could add a part in the function where it also finds the result.
But that all depends on weather or not you can ask it directly after you got the operator.
If you have any questions (or if its unclear) please ask :)
Cheers!
Edit: but coming back to the question, you could do something like: if (operator = '+' ) { result = number1+number2} so basically the same as you'd always do :) Using if loops in here should be easier anyway :)
#include <iostream>
usingnamespace std;
#include <stdlib.h>
void main ()
{
//read first number
long L1;
long L2;
long L3;
cout << "Enter a number ";
cin >> L1 ;
//Read in the operator
// Is the operator valid?
//yes - continue
//no - go back and read operater again
// Is the operator X?
// yes-stop
// no - go on with program
// Is operator C?
// yes - (option 1 ) reset first # to 0, go get another operator
// yes- (option 2) go read another first number
// no - go on with program
// doing math
// read second number
// do math operation
// Print result
// Go read in another operator
char Op;
bool Valid;
//so were going to do a do-while loop..it always does the loop at least once
do{
Valid = true;
cout << "Enter an operator (+ - / * C c X x): " ;
cin >> Op; //read in an operator
switch (Op)
{
case'+': cout<<"enter another number"<<endl;
case'-': cout<<"enter another number"<<endl;
case'*': cout<<"enter another number"<<endl;
case'/': cout<<"enter another number"<<endl;
Valid = true;
break;
case'X':
case'x':
cout << " Terminiating Program " <<endl;
exit(0); //this causes the program to immediatly terminate
case'C':
case'c':
//we want C to clear the calc
cout << "Clearing the Calculator " <<endl;
Valid = false;
break;
default:
Valid = false;
}
cin >> L2 ;
if (operator = '+' )
{cout<< L1 + L2 <<endl;}
else { cout << "Help me" <<endl;
}
} while (!Valid);
}
#include <iostream>
int main ()
{
usingnamespace std;
int num = 5, newNum;
char symbol;
cout << "5";
do
{
cout << "\n> ";
cin >> symbol >> newNum;
switch (symbol)
{
case'*':
num = num*newNum;
cout << "\n" << num;
break;
case'E': //falls through...
case'e': //falls here...
break; //breaks
default: //if anything else occurs
break;
}
} while (symbol != 'E' || symbol != 'e'); //as long as it is not e, otherwise the loop is exited
return 0;
}
Just noticed my mistake in arguments. Also added default.
Well because num is an int, typing in anything but a number would to do terrible, terrible things. That's why I used 0 instead of a char. You could other things to allow you to use a char, but unless you want to get into some complicated territory, 0 maybe your best bet. Who would want to do mathematical equations to 0 anyway? :)
I was thinking stdlib.h and using exit?
You could if you want. I wouldn't personally :)
C/c needs to clear the Calculator and return to the top, would I do a for loop here?
Doesn't it already do this?
Please enter a number. 0 to exit
> 5
> +5
= 10
> +5
= 15
> e
Please enter a number. 0 to exit
> 2
> +2
= 4
>
If you want to clear screen then I suggest to use:
cout << string(50, '\n');
wherever you want to clear the screen. 50 = the number of enters ('\n') occur. You can change both to whatever you want.
One thing you should do is make the user more aware of what to input. Even the first input is a bit confusing as to why you should input a number. Adding some more output would help greatly.
but really you should be using double's because 34.2534 is a valid number for a calculator, but it's not an integer.
edit: If you get this advanced and decie to use it, don't forget to read up the API for istringstream//sstream, and make sure you add in the correct includes.