Function failing?

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

My code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <iostream>
#include <iomanip>
#include <cmath>
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.