program crashes when i try to enter the first operation. any help is great. Here's the code:
#include <iostream>
using namespace std; // so we dont have to use std
int main () // start function
{
double num1; // first number
double num2; // second number
double answer; // answer
char op1; // first operation
char op2; // second operation
char quit; // quit option
char op;
do // start do loop
{
cout << " welcome to my calculator\n"; // welcom screen
cout << " enter first number\n"; // user enters first number
cin >> num1; // input users answer
cout << "enter an operation ( + - * or / ) \n";
cin >> op1;
do // second loop
{
switch (op) // start of switch
{
cout << "enter another number\n";
cin >> num2;
case '+' : (answer = num1 + num2);
cout << "total is "; cout << answer;
cout << "enter another operation ( + - * / or =) \n";
cin >> op2;
if (op2 != '=') (answer=num1);
break;
case '-' : (answer = num1 - num2);
cout << "total is "; cout << answer;
cout << "enter another operation ( + - * / or =) \n";
cin >> op2;
if (op2 == '=')
break;
case '*' : (answer = num1 * num2);
cout << "total is "; cout << answer;
cout << "enter another operation ( + - * / or =) \n";
cin >> op2;
if (op2 != '=') (answer=num1);
break;
case '/' : (answer = num1 / num2);
cout << "total is "; cout << answer;
cout << "enter another operation ( + - * / or =) \n";
cin >> op2;
if (op2 != '=') (answer=num1);
break;
}
} while (op2 != '=');
I believe this line is o.k., since op2 is supposed to be a char. The user could enter '=' at the command prompt. If all of the variables were initialized and/or set, I believe the code will compile and run as is.
CPPMATT,
I was not talking about the '=', I was referring to the statement after condition check. Isn't answer=num1 supposed to be in curly braces rather than ()?