A simple addition,substraction,multiplication and division program
A simple C++ calculator program to calculate addition, subtraction, multiplication and addition...
#include <iostream>
#include <string>
using namespace std;
int main()
{
//input declarations as doubles for total and counter
double total = 0, counter =0;
//input declarations sign and Q as character
char sign, Q = 0;
//input declaration value as double
double value;
//A do..while will loop forever (or until we hit the break statement)
do
{
//The current value is 0.
cout << "Result :"<<" "<< total << '\n';
//Please enter an operation
cout << "Please enter an operation and number : ";
cin >> sign;
//If the operation is Q, the program will end.
if (sign != 'Q')
cin >> value;
cin.ignore();
// If the value input is <=0, you can't divide anything by zero.
if (value <= 0)
{
cout << "Unknown Operator " << sign <<'\n' ;
}
//Otherwise procede with the calulator program
else
{
//If the operation is equal to '+', then the total is added.
if (sign == '+')
{
total += value;
}
// If the operation is equal to '-', then the value is subtracted from the previous number input.
开发者_如何学JAVA else
{
if (sign == '-')
{
total -= value;
}
// If the operation is equal to '*', then the value is multiplied to the previous number input.
else
{
if (sign == '*')
{
total *= value;
}
// If the operation is equal to '/', then the value is divided by the previous number input.
else
{
if ((sign == '/')&&(value != 0))
{
total /= value;
}
}
}
}
}
}
//While the operation is not equal to 'Q', the program will run.
while (sign != 'Q');
return 0;
}
The coding for the above program has no error but if i press "Q" to quit,it will display the last result non-stop. .Over and over and over and over again. . . Anyway,anyone know how to add square root to the program. .
Replace if (sign != 'Q') ...
by if (sign == 'Q') break;
- This will fix numerous possible errors.
- This will make your code more readable by using less indentation and braces.
EDIT: as someone mentionned, you should probably be checking lowercase too. (if (sign == 'Q' || sign == 'q')).
As a simple exercise I have refactored your program to make it simpler. I don't promise it works, but it should give you a nice foundation to build on:
#include <iostream>
#include <string>
using namespace std;
int main()
{
//input declarations as doubles for total and counter
double total = 0, counter =0;
//input declarations sign and Q as character
char sign = 0;
//input declaration value as double
double value;
//A do..while will loop forever (or until we hit the break statement)
do
{
//The current value is 0.
cout << "Current result: " << total << '\n';
//Please enter an operation
cout << "Please enter an operation (or Q to quit) and number: ";
cin >> sign;
//If the operation is Q, the program will end.
if (sign == 'Q' || sign == 'q') {
cout << "See you!\n";
break;
}
// If the input cannot be transformed into a double
// abort loop and try again
if (!(cin >> value)) {
cerr << "Invalid value entered, try again\n";
continue;
}
switch(sign)
{
case '+': total += value; break;
case '-': total -= value; break;
case '*': total *= value; break;
case '/':
if (value == 0) {
cerr << "Divide by 0 prevented!\n";
} else {
total /= value; break;
}
default:
cerr << "Unknown sign: " << sign << "\n";
}
} while (true);
return 0;
}
The main points:
- avoid indentation when possible, they hamper readability (prefer continue/break)
- use a switch when possible (it's possible for
char
and numbers) - check for errors when necessary: the conversion from the user input to the
double
could fail,0
might be entered
#include <math.h>
double result = sqrt (value);
For square root,
#include <math>
double r=sqrt(e);
Concerning your other issue : please indent your code better ;-)
Are you sure you didn't forget to put a block after this line ?
if (sign != 'Q')
Simplest way, use a debugger in step mode and you'll understand!
精彩评论