### Function failing?

I'm supposed to write a program that converts temperatures using
 ``123`` ``````void displayMenu() int getChoice() void generateTable(int startTemp, int endTemp, int stepSize, int choice)``````

My code:
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143`` ``````#include #include #include using namespace std; void displayMenu(); int getChoice(int); void generateTable(double, double, int, int); int main() { int stepSize = 0, choice = 0; double startTemp = 0, endTemp = 0; displayMenu(); getChoice(choice); generateTable(startTemp, endTemp, stepSize, choice); return 0; } void displayMenu(){ cout << "Program that creates a table of the follwing...\n" << " Menu\n"; cout << "1. Convert Farenheit to Celsius\n" << "2. Convert Celsius to Farenheit\n" << "3. Farenheit to Kelvin\n" << "4. Kelvin to Farenheit\n" << "5. Convert Celsius to Kelvin\n" << "6. Convert Kelvin to Celsius\n" << "7. Quit the program\n"; } int getChoice(int fchoice){ cout << "Enter menu choice: "; cin >> fchoice; while (fchoice<1 || fchoice>7){ cout << "Invalid menu choice. Choose 1, 2, 3, 4, 5, 6, or 7. "; cout << endl << "Enter menu choice: "; cin >> fchoice; } return fchoice; } void generateTable(double fstartTemp, double fendTemp, int fstepSize, int fchoice){ switch (fchoice){ case 1: //Farenheit to Celsius cout << "Enter starting temp: "; cin >> fstartTemp; cout << "Enter ending temp: "; cin >> fendTemp; cout << "Enter step size: "; cin >> fstepSize; cout << setprecision(2); for (double i = fstartTemp; i < fendTemp; i += fstepSize){ cout << setw(7) << i << setw(2) << "F" << setw(5) << "===>" << setw(8) << (i - 32)*5.0 / 9 << setw(2) << "C" << endl; } break; case 2: //Celsius to Farenheit cout << "Enter starting temp: "; cin >> fstartTemp; cout << "Enter ending temp: "; cin >> fendTemp; cout << "Enter step size: "; cin >> fstepSize; cout << setprecision(2); for (double i = fstartTemp; i < fendTemp; i += fstepSize){ cout << setw(7) << i << setw(2) << "C" << setw(5) << "===>" << setw(8) << i*9.0 / 5 + 32 << setw(2) << "F" << endl; } break; case 3: //Farenheit to Kelvin cout << "Enter starting temp: "; cin >> fstartTemp; cout << "Enter ending temp: "; cin >> fendTemp; cout << "Enter step size: "; cin >> fstepSize; cout << setprecision(2); for (double i = fstartTemp; i < fendTemp; i += fstepSize){ cout << setw(7) << i << setw(2) << "F" << setw(5) << "===>" << setw(8) << ((i + 459.67)*5.0 / 9) + 273 << setw(2) << "K" << endl; } break; case 4: //Kelvin to Farenheit cout << "Enter starting temp: "; cin >> fstartTemp; cout << "Enter ending temp: "; cin >> fendTemp; cout << "Enter step size: "; cin >> fstepSize; cout << setprecision(2); for (double i = fstartTemp; i < fendTemp; i += fstepSize){ cout << setw(7) << i << setw(2) << "K" << setw(5) << "===>" << setw(8) << i * 9 / 5 - 459.67 << setw(2) << "F" << endl; } break; case 5: //Celsius to Kelvin cout << "Enter starting temp: "; cin >> fstartTemp; cout << "Enter ending temp: "; cin >> fendTemp; cout << "Enter step size: "; cin >> fstepSize; cout << setprecision(2); for (double i = fstartTemp; i < fendTemp; i += fstepSize){ cout << setw(7) << i << setw(2) << "C" << setw(5) << "===>" << setw(8) << i + 273.0 << setw(2) << "K" << endl; } break; case 6: //Kelvin to Celsius cout << "Enter starting temp: "; cin >> fstartTemp; cout << "Enter ending temp: "; cin >> fendTemp; cout << "Enter step size: "; cin >> fstepSize; cout << setprecision(2); for (double i = fstartTemp; i < fendTemp; i += fstepSize){ cout << setw(7) << i << setw(2) << "K" << setw(5) << "===>" << setw(8) << i - 273.0 << setw(2) << "C" << endl; } break; case 7: break; } }``````

The first two functions seem to work fine but when i select 1,2,3,4,5,6,7 as a menu choice nothing happens except "Press any key to continue".
The function "getChoice()" should be taking it's argument by reference.
Thanks, that seemed to fix the problem.
Now I just have to clean the table up.
You do understand why that change needed to be made right? As in the difference between passing by reference and passing by value?
I understand the difference, one makes a copy of the variable and changes it and one actually uses the variable's location. Is the failure something related to the fact that the other functions are void or am I mistaken?
Edit: Actually thinking about it, since I didn't say that choice=getChoice(choice); I figure that was the main issue and thus pass by reference does that for me?
Last edited on
It's the same end result.
Topic archived. No new replies allowed.