This is the code to my calculator. It is supposed to be set up like an accountants in that you start at 0 and can keep performing operations on it. Currently I need help developing a running total, as well as being able to press 'x' to exit the program. Any help is useful!
--------------------------------------------------------------------------------
#include "stdafx.h"
#include <iostream>
using namespace std; //allows compiler to recognize terms from library such as endl, cout, etc.
int main() //declares the main function
{
double num = 0; //to clarify the variable name, num is short for number
double num2;
char operation; //stores the addresses of +, -, *, and /
char quit;
do
{
cout << "Hello! My name is Jeff Gallimore. Welcome to my calculator :)" << endl << endl; //friendly greeting to the user
cout << "To exit the program, press 'x'" << endl;
cout << "--------------------------------------------------------------------------------" << endl;
cout << "Total: 0" << endl;
cout << " ";
cin >> operation >> num2;
switch (operation){ //replaces if statement and covers all mathematical operations
case '+':
cout << "----------" << endl << "Total: " << num + num2 << endl;
if (num == 0)
num2 += num;
break;
case '-':
cout << "----------" << endl << "Total: " << num - num2 << endl;
break;
case '*':
cout << "----------" << endl << "Total: " << num * num2 << endl;
break;
case '/':
if (num2 == 0)
cout << "Improper operation, not dividable by 0" << endl; //clears calculator and sends the user back to make another calculation
else
cout << "----------" << endl << "Total: " << num / num2 << endl;
break;
}
cin >> quit;
} while (quit == 'x' || quit == 'X'); //gives option to clear all calculations
system("pause");
return 0;
}
well the first problem is that it closes after i type a second operation. you need an infinite loop to make sure it doesn't stop after one. the easiest way to do this is to use
for(;;){
//this stuff will loop forever
}
a loop can be broken with
break;
so you can put that in with return 0;
There are currently no compiling errors. I tried putting in the for loop just above return 0 in my code, and I can still only perform one operation. Do I need to change something with break? When I do that the calculator performs 4 different operations at once.
It is supposed to, but right now it is not. I tried playing around and putting the exit in my operation with +, -, *, and / so the user could press x instead of an arithmetic operation but nothing happened
There are still plenty of flaws here, but i simplified some of the code - there's no need to have so much duplicated code, such as multiple almost identical cout statements, when just one will do:
#include <iostream>
usingnamespace std;
int main()
{
double num1, num2;
char operation;
char quit;
double result = 0;
do
{
cout << "Enter number operator number:" << endl;
cin >> num1 >> operation >> num2;
switch (operation)
{
case'+':
result = num1 + num2;
break;
case'-':
result = num1 - num2;
break;
case'*':
result = num1 * num2;
break;
case'/':
if (num2 == 0)
{
cout << "Improper operation, not dividable by 0" << endl;
continue;
}
else
result = num1 / num2;
break;
}
cout << "----------\nTotal: " << result << endl;
cout << "To exit the program, press 'x'" << endl;
cin >> quit;
} while (quit != 'x' && quit != 'X');
return 0;
}
Also, though you are to be commended for commenting your code, make them meaningful, for example rather than adding a long comment to explain a short variable name, it might be better to simply choose a better variable name.