Hi guys, I am having a bit of trouble. I've come to realize that my book does a poor job to explain a lot of concepts in detail.

I am trying to use functions in the folowing code.

#include <iostream>	//For cin and cout
using namespace std;

int main(void)
{
	int 	number1, 					//INPUT
number2;
					
	cout << "Enter first number: " << endl;	        
	cin >> number1;
	cout << "Enter second number: " << endl;
	cin >> number2;

	int	sum,                                         //CALCULATIONS
difference, 
product, 
quotient;

	sum = number1 + number2;
	difference = number1 - number2;
	product = number1 * number2;
	quotient = number1 / number2;

	cout	<< "\n\nNumber 1: " << number1 << endl	//OUTPUT
		<< "Number 2: " << number2 << endl
		<< "\n\nAnswers: "
		<< "\n  sum = " << sum
		<< "\n  difference = " << difference
		<< "\n  product = " << product
<< "\n  quotient = " << quotient << "\n" << endl;

	
cout	<< "Press any key to exit." << endl;
	cin.ignore(2);
   
	return 0;				
}

What i wanna do, is to have a function say, for the menu.

I know i have to list the function and the bottom of the code, outside the bracket.
But, how do you call it? I don't undesrtand the parameters as well.

Basically, instead of having the whole menu up there, I wanna have a function like "getmenu ()" to display the menu.

I realize that it is the same (with more work), but I'm trying to understand this here because as homework, I will be using functions for calculations.

I know that the variables stated and out put would stay within the body.
But the equations?
If I understand, you would use this part for the function,

int	sum,                                         //CALCULATIONS
difference, 
product, 
quotient;

	sum = number1 + number2;
	difference = number1 - number2;
	product = number1 * number2;
	quotient = number1 / number2;

correct?


how would you make the calculations in this example a function?

int getmenu()
// returns 1 for addition, 2 for sub traction,
//         3 for mult., 4 for division, -1 for
{
    // display the menu
    // ask user for input
    // return 1 through 4 based on user's selection
}

Call it from main, then do a mathematical operation based on the return value.

That would be the menu part. If you decide to write separate functions for each operation, something like this...

int Add(int x, int y)
{
    int sum = x + y;
    return sum;
}

Edited 5 Years Ago by VernonDozier: n/a

int getmenu()
// returns 1 for addition, 2 for sub traction,
//         3 for mult., 4 for division, -1 for
{
    // display the menu
    // ask user for input
    // return 1 through 4 based on user's selection
}

Call it from main, then do a mathematical operation based on the return value.

That would be the menu part. If you decide to write separate functions for each operation, something like this...

int Add(int x, int y)
{
    int sum = x + y;
    return sum;
}

oops, i missed that.

For the menu, I want to do something like :

"Press a for addition, s for substraction"

and so on.
how would I make that into a function?

>> For the menu, I want to do something like :

"Press a for addition, s for substraction"

and so on.
how would I make that into a function?

So change the 1 through4 in my outline to 'a', 's', 'm', 'd'. Have the function return a char instead of an int.

Comments
Great poster and willing to help!

>> For the menu, I want to do something like :

"Press a for addition, s for substraction"

and so on.
how would I make that into a function?

So change the 1 through4 in my outline to 'a', 's', 'm', 'd'. Have the function return a char instead of an int.

I'm having a lot of trouble here.

Ok, so this is my new code:

#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
using namespace std;

int main ()
{
	char op= ' ';
	int cstart, cend, increment, fstart, fend; 

	cout << "This program converts and calculates the increment between" << endl; 
	cout <<"celsius and fahrenheit temperatures"<< endl;
	
	cout << endl << endl; 

	char getmenu (char menu); // I TO CALL THE FUNCTION HERE

	switch (op)
	{

		case 'F':
		case 'f':
			cout << "Please enter the starting Fahrenheit temperature" <<endl; 
			cin >> fstart;

			cout << "Please enter the ending fahrenheit temperature" << endl; 
			cin >> fend; 

			cout << "Please enter the increment" << endl; 
			cin >> increment;



				break;

		case 'C':
		case 'c':
			cout << "Please enter the starting Celsius temperature" <<endl; 
			cin >> cstart;

			cout << "Please enter the ending Celsius temperature" << endl; 
			cin >> cend;

			cout << "Please enter the increment" << endl; 
			cin >> increment;


				break; 

	}
	 
	
system ("pause");
return 0;
}
char getmenu (char menu); // THIS WOULD BE THE FUNCTION
{ 
	cout << "1. Press C to convert celsius to fahrenheit " <<endl; 
	cout << "2. Press F to convert fahrenheit to celsius " <<endl;
	cout << "3. Press Q to quit the program " <<endl; 
	cin >> op;
	return getmenu;
}

but the compiler gives me an error on line 58 "error C2447: '{' : missing function header (old-style formal list?)"

Help?

Edited 5 Years Ago by juangalvez4: n/a

Corrected function:

char getmenu () // THIS WOULD BE THE FUNCTION
{
        char op; 
	cout << "1. Press C to convert celsius to fahrenheit " <<endl; 
	cout << "2. Press F to convert fahrenheit to celsius " <<endl;
	cout << "3. Press Q to quit the program " <<endl; 
	cin >> op;
	return op;
}

1) No semicolon in line 57.
2) No parameters passed to the function.
3) "op" variable is declared as alocal variable in the function.

Line 17 would be this:

op = getmenu();

Edited 5 Years Ago by VernonDozier: n/a

Awesome, I'll try it out soon.
Ok
Now I need to make the switch statement into a function and now im really stuck because I dont know how to even start!

This article has been dead for over six months. Start a new discussion instead.