Problems with coding a basic calculator

closed account (iT7X92yv)
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 
Well first off, you need some sort of validation with your input. You could use a simple if else for this. You have operand initialized as an empty char, then immediately put toupper() on it. This does nothing at all here, because there is no value there. You say you are new to programming, so if you didn't know, a and A are completely different to a computer. If you use toupper(), your switch will never pass as is.
You're making this more complicated than it has to be:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;
int main () {
int a;
int b;
int c;
int d;
for (d=0, d=d*2,d++)
{
cout<<"This is your friendly neighbourhood calculator!"<<endl<<"Compute: "<<endl;
cin>>a;
cout<<"(D)ivide, (M)ultiply, (A)dd, (S)ubtract, (Q)uit"<<endl;
cin>>b;
cin>>c;
if (b=='d') cout<<a/c<<endl;
if (b=='m') cout<<a*c<<endl;
if (b=='a') cout<<a+c<<endl;
if (b=='s') cout<<a-c<<endl;
if (b=='q') return 0;
if (b!='q','d','m','a','s') cout<<"Run to da choppah!"<<endl;
}
}


But maybe I'm just rambling.
Last edited on
closed account (iT7X92yv)
ResidentBiscuit - I knew that capital letters weren't the same as their lower case equivalents, but that's about the extent of it. *lol* I wasn't sure where to put the toupper() in the coding. I was trying to follow an example that the book had, but obviously that didn't work.

Vortex - I wouldn't be surprised if I were making it more complicated, except I wasn't sure how to un-complicate it. :-P
Well right now you're doing nothing to the input. You just take it and go straight into the switch hoping all is well. That's no good. After input, do any modifications and checking of it, then go into the switch. And think about it, if you toupper() the input, and test it as is with lowercase vales as the testers, you won't achieve anything. You want the input to match up with your cases
closed account (iT7X92yv)
Where would I put the toupper in my code, if it even needs it at all so that I can use an upper case or a lower case letter and still achieve the same end result?
closed account (iT7X92yv)
I was finally able to figure out the problem I was having and finally got it working the way it was supposed to. Thanks guys for the help! I'll keep coming back for more advice since I'm sure I'll run into more problems along the way as I continue to learn C++
Topic archived. No new replies allowed.