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

Started by
22 comments, last by RogueZero 20 years, 8 months ago
Ok i just started learning C++ and its my first language ive ever learned other than some simple things in Qbasic a long time ago. I''m learning C++ out of a online ebook called teach yourself C++ in 21 days. I''m currently on the third day and I learned a little about functions. I learn better if I ask questions and have other more knowledgable people help me out than if i just study a book. I can use a simple function to make a program add multiple divide or subtract two numbers but I cant let the person running the program decide what simple math function to do with those numbers. This is what i got so far:

#include <iostream>
int multiply (int x, int y)
{
cout << " Multiplied " << x << " and " << y << "\n";
return (x*y);
}
int main()
{
int a, b, c;
cout << " Enter 2 numbers: ";
cin >>a;
cin >>b;
c=multiply(a,b);
cout << "Heres the answer to your multiplication question: " << c;
cin >> c;
return 0;
}
can someone help me make this program ask the user what to do with the numbers then do it to the numbers using this simple type of source code. I''m a newb so keep it as simple as possible.
Advertisement
Something like:

#include <iostream>int Multiply(...){    ...}int Divide(...){    ...}int Add(...){    ....}int main(){    int a, b, c, d;    std::cout << " Enter 2 numbers: ";    std::cin >> a;    std::cin >> b;    std::cout << " Multiply(1)? Divide(2)? Add(3)? " << std::endl;    std::cin >> d;    if(d == 1) { c = Add(...) };    if(d == 2) { c = Divide(...) };    if(d == 3) { c = Add(...) };    std::cout << "Heres the answer to your question: " << c;    std::cin >> c;    return 0;} 


[edited by - Orb on July 27, 2003 9:57:21 PM]

Try something like this:

#include <iostream>/************************************* *                                   * *************************************/int multiply (int x, int y){	cout << " Multiplied " << x << " and " << y << "\n";	return (x*y);}/************************************* *                                   * *************************************/char simpleMenu(void){	char choice;	cout<<"1: Multiply\n";	cout<<"2: Add\n";	cout<<"3: Divide\n";	cin>>choice;	return choice;};/************************************* *                                   * *************************************/int main(void){	int a, b, c;	cout << " Enter 2 numbers: ";	cin >>a;	cin >>b;	char men = simpleMenu();		switch(men)	{	case ''1'':{c=multiply(a,b);};		break;	case ''2'':{cout<<"do another function\n";};		break;	case ''3'':{cout<<"do another function\n";};		break;	default: exit(1);	};		cout << "Heres the answer to your multiplication question: " << c;	return 0;}
Could you guys explain these new commands (I dont know any of these commands ive never seen them before)and what the source code is doing? Cause im kind of a real newb. Thanks.
Look at Day 7 of your book (I think it's called 'More Program Flow') to learn about control statements like if, elseif, else, and switch. You should be able to figure out how to tie in the other stuff you need once you've looked into them.

[edited by - SysOp_1101 on July 27, 2003 9:58:26 PM]
SysOp_1101
quote:Original post by RogueZero
Could you guys explain these new commands (I dont know any of these commands ive never seen them before)and what the source code is doing? Cause im kind of a real newb. Thanks.


Sure, it''s nothing magic. The "switch" statement is simply a built-in identifier that allows you to pass in variable, and then choose multiple cases based on that variable. Here I passed in the "men" variable (for menu option), and then below, based on the value of the "men" variable we execute the proper function. If the user choses a "1", then the numbers are multiplied, if the user chooses a "2" then nothing happens but some text, but you could replace that with an addition function if you wanted.
Alright guys youve been a great help so far but now im stuck this is what ive got as the program:
#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;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";cin >> d;if(d = 1) { c = multiply(a, b); };if(d = 2) { c = divide(a, b); };if(d = 3) { c = add(a, b); };if(d = 4) { c = subtract(a, b); };cout << " Enter 2 numbers:\n ";cin >>a;cin >>b;cout << "Heres the answer: " << c;cin >> c;return 0;}// the error signals that its here

and the error its giving me is on line 42 and says there is a parse error at the end of input. Oh yeah and what do you guys think of my first program?
That probably means that you have an opening brace ({) that is not followed by a closing brace. Looks like it is in the divide funtion:

int divide (int x, int y)
{
cout << " Divided " << x << " and " << y << "\n";
return (x/y);
{

Make that:

int divide (int x, int y)
{
cout << " Divided " << x << " and " << y << "\n";
return (x/y);
}

Notice the change on that last line. That brace needs to be closing, not opening.
Provided that your snippet there is a copy/paste job, your problem is in the divide() function. You wrote it like this:

int divide (int x, int y){  ...{ // note that it's a second opening brace!  


Just change the { to a } where I marked it and it should work.

Usually when you get an error like that, it means you have missed or misplaced a brace or semi-colon or something. Don't feel bad, it a really easy mistake to make and the error spewed out by the compiler is useless for telling what's really wrong.

EDIT: Dammit! Beaten by mere seconds!

-Auron

[edited by - Auron on July 27, 2003 11:09:44 PM]
if(d = 1)...if(d = 2)...if(d = 3)...if(d = 4)...


Should be:

if(d == 1)...if(d == 2)...if(d == 3)...if(d == 4)...


''='' means assignment
''=='' means comparison

SysOp_1101

This topic is closed to new replies.

Advertisement