# Functions Within Menu Driven Programs

Page 1 of 1

## 1 Replies - 7940 Views - Last Post: 03 November 2008 - 12:44 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=70187&amp;s=79bf735257aef4383d1af989bdec5c7a&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 jap2010

Reputation: 0
• Posts: 4
• Joined: 29-October 08

# Functions Within Menu Driven Programs

Posted 03 November 2008 - 12:21 AM

I do not understand how to use Functions in a Menu Driven Program I am trying to do this program, however, I'm not sure on where to go from here. I'm already getting lots of error messages. Any help will be appreciated.

Write a program to convert Celsius to Fahrenheit or Fahrenheit to Celsius depending on the user’s choice.
a. Use main( ) as the driver function. main( ) should allow the user to run the program as many times as desired.
b. Write the function getConvertChoice( ) to prompt the user for the desired conversion:
1. Celsius to Fahrenheit
2. Fahrenheit to Celsius.
Return this value back to main( ).
c. Write the function getTemperature( ) to prompt the user for the temperature in degrees to be converted.
d. Depending on the user’s conversion choice: 1) call the function convertToFahren( ), if the user chose to convert from Celsius to Fahrenheit, or 2) call the function convertToCelsius( ) if the user chose to convert from Fahrenheit to Celsius. The formulas are:
Fahrenheit = (9.0/5.0) * Celsius + 32.0
Celsius = (5.0/9.0) * (Fahrenheit – 32.0)
e. Write the function displayConversion( ) to display the original temperature entered and the converted temperature with appropriate messages. Display the values to a tenth of a decimal.

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

//Symbolic Constants
const double TEMPERATURE_CONVERSION_FACTOR = 32.0;

//Function Prototypes
int getConvertChoice(int);
double getTemperature(int );
void convertToFahren(double);
void convertToCelsius(double);
void display(double, double);

int main (void)
{
int x=0;

char repeat;

getConvertChoice(x);
getTemperature(x);

do
{
//Builds a menu for user to choose desired conversion
cout << "Please choose the desired conversion from the menu." << endl;
cout << "\n\n	  1.			Convert from Celsius to Fahrenheit" << endl;
cout << "\n	  2.			Convert from Fahrenheit to Celsius" << endl;
cout << "\nEnter choice: ? ";
cin >> choice;
cout << "\n\nWould you like to convert another temperature? (Y or N): ";
cin >> repeat;
}while(repeat == 'y' || repeat == 'Y');

return 0;
}

int getConvertChoice(int choice)
{

if (choice ==1)
convertToFahren(c);
else
convertToCelsius(f);

return choice;
}

double getTemperature (int x)
{
cout << "Please enter the temperature to be converted: ?";
;
}

```

Is This A Good Question/Topic? 0

## Replies To: Functions Within Menu Driven Programs

### #2 OliveOyl3471

• Everybody's crazy but me!

Reputation: 135
• Posts: 6,581
• Joined: 11-July 07

## Re: Functions Within Menu Driven Programs

Posted 03 November 2008 - 12:44 AM

Here are some errors I found:
1. you did not declare the variable choice
2. you did not declare variables c and f (not sure you even need to use them in your getConvertChoice() function)
3. you did not define your other functions, convertToFahren() and convertToCelsius()
4. If I'm reading this right, it looks as if you're trying to call your function getConvertChoice(x); which uses the variable choice, before you even get the user input for choice.
5. I wouldn't call your conversion functions within your other function. You are returning a value, so just call them after the place your value gets returned to...if that makes sense. Your instructions say return this value back to main. So after you get it back to main, then use it to determine which function to call next.

I have fixed it so that your code will at least compile now. You'll have to work on it some more to finish it up, though. I hope this gives you a nudge in the right direction. Happy Coding!

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

//Symbolic Constants
const double TEMPERATURE_CONVERSION_FACTOR = 32.0;

//Function Prototypes
int getConvertChoice();
double getTemperature();
void convertToFahren();
void convertToCelsius();
void display(double, double);

int main (void)
{
int x=0;
int choice;
char repeat;

//getConvertChoice(x);
//getTemperature(x);

do
{

choice=getConvertChoice(); //here's where you call your function to get the choice and assign it to a variable

if (choice == 1)
{
convertToFahren();
}
else
{
convertToCelsius();
}

cout << "\n\nWould you like to convert another temperature? (Y or N): ";
cin >> repeat;
}while(repeat == 'y' || repeat == 'Y');

return 0;
}

int getConvertChoice()	//Builds a menu for user to choose desired conversion
{
int choice;
cout << "Please choose the desired conversion from the menu." << endl;
cout << "\n\n1.Convert from Celsius to Fahrenheit" << endl;
cout << "\n2.Convert from Fahrenheit to Celsius" << endl;
cout << "\nEnter choice: ? ";
cin >> choice;
return choice;
}

double getTemperature ()
{   int temp;
cout << "Please enter the temperature to be converted: ?";
cin>>temp;
return temp;
}

void convertToFahren()
{
cout<<"TEST"; //put conversion code here instead of test
}

void convertToCelsius()
{
cout<<"test"; //put conversion code here instead of test
}

```

This post has been edited by OliveOyl3471: 03 November 2008 - 01:33 AM

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }