I'm taking a class in "Introduction to Programming with C++". It is the very first experience I have ever had with programming and so far I'm less than thrilled about it.
Right now, I'm having some issues with coding a basic calculator. The homework project I'm expected to do calls for me to have a user pick a mathematical operation and if it's an invalid operation, to display the error without asking the user for the integers. When the person chooses a valid mathematical operation, it should allow them to use a lower case or upper case character and still achieve the same result.
The issues I'm running into are this:
* I can not get the error to display without asking the user for the integers that need calculating without breaking the rest of the program. Meaning that even if I get the error to display before asking for the numbers to calculate, it'll treat everything as an error condition as well even when a person chooses a valid operation.
* I do not know how to code a program so that a person can use an upper case or a lower case letter to pick the mathematical operation and achieve the same result. The book used in the class isn't very helpful when it comes to explaining it. The closest it's come is talking about the toupper and tolower statements, but using either of those hasn't helped me figure out my problem.
Trying to figure out my problem has been incredibly frustrating and I'm pulling out what little hair I have left trying to figure it out. I'm hoping someone here can take a look at my code and give me some pointers on how to get the program to function as the homework problem intends it.
Here's my current code for the program:
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
|
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
//declare variables
int integer1 = 0;
int integer2 = 0;
double Result = 0.0;
char operand = ' ';
operand = toupper(operand);
//Choose mathematical operation
cout << "Choose mathematical operation (A/S/M/D): ";
cin >> operand;
//Choose integers to calculate
cout << "Enter first integer: ";
cin >> integer1;
cout << "Enter second integer: ";
cin >> integer2;
//Determine whether operation is valid
switch (operand)
{
case 'a':
Result = integer1 + integer2;
break;
case 's':
Result = integer1 - integer2;
break;
case 'm':
Result = integer1 * integer2;
break;
case 'd':
Result = integer1 / integer2;
break;
default:
cout << "Invalid operation" << endl;
system ("pause");
return 0;
} //end switch
//Display result
cout << "Result: " << Result << endl;
system ("pause");
return 0;
} //end of main function
| |