Can you help fix this?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
// Simple math script to help me get use
// to using funcltions!
#include <cstdlib>
#include <cstdio>
#include <iostream>
using namespace std; // Should I stop using
// in future scripts?
char operation; // (+ - * /)
double sum, num1, num2;
void explainCal(void) //Begining message.
{
cout << "This is a very basic calculater"
<< " able to add, subtract, multiply"
<< ", and divide small numbers quicly."
<< "\nEnter Problem:\t";
}
void calculateProb(void) // Calculates are
{ // Problem.
case( operation == '+') //Add
{
sum = num1 + num2;
break;
}
case( operation == '-') //Subtract
{
sum = num1 - num2;
break;
}
case( operation == '*') // Multiply
{
sum = num1 * num2;
break;
}
case( operation == '/') // Divide
{
sum = num1 / num2;
break;
}
default //If all else goes wrong!
{
cout << "Error: Unable to complete!";
}
return ;
}
int main ()
{
explainCal();
cin >> num1 >> operation
>> num2; // Grabs problem.
calculateProb();
cout << "\n The answer:\t"
<< sum; //Prints answer.
return 0;
}
|
Last edited on
Your calculateProb function doesn't implement the
switch
statement correctly.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
void calculateProb()
{
switch(operation)
{
case '+':
//do addition stuff
break;
case '-':
//do subtraction stuff
break;
//etc...
default:
//print error message
break;
}
}
|
Last edited on
You are using case
statements as if
statements.
http://www.cplusplus.com/doc/tutorial/control/
Last edited on