I need some help on a simple C++ math program

Started by
22 comments, last by RogueZero 20 years, 8 months ago
also, you are using

if(choice 1)
do thing 1
if(choice 2)
do thing 2
if(choice 3)
do thing 3

... this type of situation is exactly what the "else if" is for ... and also the switch statement ...

basically ... your if lines are testing each condition, and doing the one''s that match ..

here''s it rewritten with else ifs

if(choice 1)  do thing 1else if(choice 2)  do thing 2else if(choice 3)  do thing 3else  tell the user you did not understand their choice


this is jut like your code ... with 2 differences ...

1 - it''s more efficient, because once the computer sees that it has matched a choice, it quits trying to match other choices as well (the else if mean ONLY check if the previous ifs have failed ...)

2 - the else case can catch every situation that you haven''t handled, all at once ... so say they can enter any letter or number ... and you only handle 4 choices ... using else, you can write ONE block of code to handle all wrong choices the same ... easy ... perhaps a message like:

cout << "Command: " << d << " is unknown, please try again" << endl;
Advertisement
next recommendation ...

name your variables with things that MEAN something ... obvious ... to you ...

why is your input variable "d" ???

I''m sure you have some reason ... but wouldn''t something like:

"usersChoice", or "menuChoice", or "command" mean a little more to you?

the a and b names for the operands are pretty fine (although something like "operandA" and "operandB" or even "val1" "val2" would be ok too ...

the name c could really stand to be something like "result"

so basically, in the simplest form, I''d make your code more readable by making your variables something like:

int a, b, result, command;

which will really make obvious which lines of code do your math, which part they''re dealing with, and which lines have to do with your menu choices ...
ok guys ive looked over and looked over my code but i cant find what i left out. It says there is a parse error at the end of input on the last line. Does anyone see any thing i missed? Here's the code:
#include <iostream>int multiply (int x, int y){cout << " Multiplied " << x << " and " << y << "\n";return (x*y);}int add (int x, int y){cout << " Added " << x << " and " << y << "\n";return (x+y);}int subtract (int x, int y){cout << " Subtracted " << x << " and " << y << "\n";return (x-y);}int divide (int x, int y){cout << " Divided " << x << " and " << y << "\n";return (x/y);}int main(){int a, b, c, d;while(true){cout << " What math fuction do you wish to perform?\n ";cout << " 1. Multiply\n ";cout << " 2. Divide\n ";cout << " 3. Add\n ";cout << " 4. Subtract\n";cout << " 5. Exit\n";cin >> d;if(d == 5) {break;}cout << " Enter  the 2 numbers you wish to calculate:\n ";cin >>a;cin >>b;if(d == 1) { c = multiply(a, b); }else if(d == 2) { c = divide(a, b); }else if(d == 3) { c = add(a, b); }else if(d == 4) { c = subtract(a, b); }else {cout << " Not a valid choice!\n";continue;}cout << "Heres the answer: " << c;cin >> c;return 0;}



[edited by - RogueZero on July 28, 2003 8:22:05 PM]
Code indentation is a Good Thing™. I''ve added some tabs:
#include <iostream>int multiply (int x, int y){	cout << " Multiplied " << x << " and " << y << "\n";	return (x*y);}int add (int x, int y){	cout << " Added " << x << " and " << y << "\n";	return (x+y);}int subtract (int x, int y){	cout << " Subtracted " << x << " and " << y << "\n";	return (x-y);}int divide (int x, int y){	cout << " Divided " << x << " and " << y << "\n";	return (x/y);}int main(){	int a, b, c, d;	while(true){		cout << " What math fuction do you wish to perform?\n ";		cout << " 1. Multiply\n ";		cout << " 2. Divide\n ";		cout << " 3. Add\n ";		cout << " 4. Subtract\n";		cout << " 5. Exit\n";		cin >> d;		if(d == 5) {			break;		}		cout << " Enter  the 2 numbers you wish to calculate:\n ";		cin >>a;		cin >>b;		if(d == 1) { c = multiply(a, b); }		else if(d == 2) { c = divide(a, b); }		else if(d == 3) { c = add(a, b); }		else if(d == 4) { c = subtract(a, b); }		else {			cout << " Not a valid choice!\n";			continue;		}		cout << "Heres the answer: " << c;		cin >> c;		return 0;	}
From this, it''s easier to see that there''s a missing closing brace. I think you want one just before the ''cin >> c;'' line.

This topic is closed to new replies.

Advertisement