Temp conversion excersize for beginners.

Pages: 12
I am currently taking my first C++ class. At the moment I am taking it online. I was supposed to have this in by last night but, asked for an extension because I couldn't get anything out of it. We were given specific parameters wanted in the program to learn about building, calling and basically trying to declutter the main by using functions. We where given a list of functions to build. I have no access to a lab, or community type thing, to ask for help. My professor is trying to help without giving me the answers, which is good, but his help and the tutorial material that I have been given just isn't doing it for me. I know most of my issue now is in the main. I need some help please.

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
  #include<iostream>
#include<iomanip>
#include<cmath>


using namespace std;
inline void keep_window_open(){ char ch; cin >> ch; }

                         //Function Prototypes;

void displayMenu();
double getMenuSelection(char var, double FtoC, double CtoF);
double getStartEndAndIncrement(double a, double b, double c);
double FtoC(double);
double CtoF(double);
void displayTable();

//  Program Main*****************************************************;

int main()
{
	
	
	cout << fixed << setprecision(1) << left;
	void displayMenu();
	double getStartEndAndIncrement(double a, double b, double c);
	double getMenuSelection(char var, double FtoC, double CtoF);
	void displayTable();

	


	cin.ignore();
	return 0;      // End of Main**********************************;
}

// Instruction display;

void displayMenu()
{
	cout << "Please enter the starting temperature, the increments you wish to see from the\n";
	cout << "starting temp. to the ending temp. and the ending temperature and, if you wish\n";
	cout << "to convert from Celsius to Fahrenheit type 'C' or, Fahrenheit to Celsius type 'F'.\n";
	cout << "Type 'Q' to Quit.\n";
}

// Selecting conversion units;

double getMenuSelection(char var, double FtoC, double CtoF)
{
	switch (var)
	{
	case 'f': case 'F':
		return FtoC;
		break;
	case 'c': case 'C':
		return CtoF;
		break;
	case 'q': case 'Q':
		cout << "Good Bye!" << endl;
		break;
	default:
		cout << "Invalid selection: try again.\n";
		break;
	}
}
	// Getting starting temp, ending temp, and increments desired for table;

	void getStartEndAndIncrement(double& a, double& b, double& c)
	{
		double startingTemp = a;
		double endingTemp = b;
		double incrementTemp = c;
	}

	// Fahrenheit to celsius conversion;

	double FtoC(double& startingTemp)
	{
			startingTemp= (9 * startingTemp) / 5 + 32;
			return startingTemp;
	}

	//  Celsius to Fahrenheit conversion;

	double CtoF(double& startingTemp)
	{
		startingTemp = 5 * (startingTemp - 32) / 9;
		return startingTemp;
	}

	//  Displays the temperatures in a table;

	void displayTable(double i, double startingTemp, double incrementTemp, double endingTemp, char FtoC, char CtoF)
	{
		if (FtoC)
		{
			i = startingTemp;
			cout << '\t' << "From " << (char)248 << "F" << '\t';
				cout << '\t' << "To " << (char)248 << "C" << endl;
			while (startingTemp <= endingTemp)
			{
				cout << '\t' << right << startingTemp << (char)248 << "F" << '\t';
					cout << '\t' << right << startingTemp << (char)248 << "C" << endl;
				i += incrementTemp;
			}

		}

		if (CtoF)
		{
			i = startingTemp;
			cout << '\t' << "From " << (char)248 << "C" << '\t';
				cout << '\t' << "To " << (char)248 << "F" << endl;
			while (startingTemp <= endingTemp)
			{
				cout << '\t' << right << startingTemp << (char)248 << "C" << '\t';
					cout << '\t' << right << startingTemp << (char)248 << "F" << endl;
				i += incrementTemp;
			}

		}

	}
When you actually call a function, you don't include the data types. So for example -
line 26
double getStartEndAndIncrement(double a, double b, double c);

should be

getStartEndAndIncrement(a, b, c);


Function prototypes and definitions need to agree. For example - the prototype at line 13 and the definition at line 69 don't match up in terms of return type and whether the function takes parameters by value or by reference.

double getStartEndAndIncrement(double a, double b, double c);

void getStartEndAndIncrement(double& a, double& b, double& c)

Since it looks like you're using this function to get input for three variables, it might make more sense to keep it a void function and pass the variables by reference, but you need to make sure a, b and c are actually declared in the main function before calling this function.

Do you want the person to input the values for a, b and c? Right now you're just creating new local variables in this function and assigning whatever value is in a, b and c to those local variables. Nothing gets input by the user and nothing changes with the original a, b and c variables.
1
2
3
4
5
6
void getStartEndAndIncrement(double& a, double& b, double& c)
	{
		double startingTemp = a;
		double endingTemp = b;
		double incrementTemp = c;
	}
Last edited on
Thank you for the help.

Ok, I made some of the corrections and yes I am trying to pull three variables from the user and a one of three decisions.

I have opened Codeblocks to basically rebuild this one function at a time. The 'displayMenu' function is working great. I have moved on to the next function 'getMenuSelection'

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
#include<iostream>
#include<iomanip>
#include<cmath>


using namespace std;
inline void keep_window_open(){ char ch; cin >> ch; }

                         //Functions to be called;
void displayMenu();
void getMenuSelection(char var, char FtoC, char CtoF);

int main()
{

    char var, FtoC, CtoF;


    displayMenu();
    getMenuSelection(var, FtoC, CtoF);

	return 0;
}

void displayMenu()
{
	cout << "Please enter the starting temperature, the increments you wish to see from the\n";
	cout << "starting temp. to the ending temp. and the ending temperature and, if you wish\n";
	cout << "to convert from Celsius to Fahrenheit type 'C' or, Fahrenheit to Celsius type \n";
	cout << "'F'.  Type 'Q' to Quit.\n";


}
void getMenuSelection(char var, char FtoC, char CtoF)
{
	switch (var)
	{
	case 'f': case 'F':
		return FtoC;
		break;
	case 'c': case 'C':
		return CtoF;
		break;
	case 'q': case 'Q':
		cout << "Good Bye!" << endl;
		break;
	default:
		cout << "Invalid selection: try again.\n";
		break;
	}
}


Codeblocks is giving me this error message.

||=== Build: Debug in Project4 (compiler: GNU GCC Compiler) ===|
C:\Users\Scotts\Documents\Codeblocks file\Project4\main.cpp||In function 'void getMenuSelection(char, char, char)':|
C:\Users\Scotts\Documents\Codeblocks file\Project4\main.cpp|39|error: return-statement with a value, in function returning 'void' [-fpermissive]|
C:\Users\Scotts\Documents\Codeblocks file\Project4\main.cpp|42|error: return-statement with a value, in function returning 'void' [-fpermissive]|
||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
If you have a void function - that means it doesn't return any value. In some of the switch cases you're trying to return a value from the getMenuSelection function.
Alright, so how does it know that I am trying to return a value? I don't even have a FtoC or CtoF function that is set to a value created in the Codeblocks compiler. What do I have to do to fix it? Do I have to overload the function? Which, if I am understanding overloading right I would make two separate switch functions that are basically the same but, one would switch for the void values and one for the values that return something right?

It knows you're trying to return a value because there's a return statement in the function.

You can overload functions, but I'm not sure that you have to. This function is just trying to determine whether the user types C, F or Q?
Yes, that is what it is trying to do. It is supposed allow the user to make the decision of which scale that is going to be used or, if they want to quit. I guess I don't understand how an answer is gotten if there is no return value. How exactly is a choice made if there is no return?
If you want it to return a value - maybe char type if it's just one letter? - you need to change the return type so it's not void. You wouldn't really need to send any variables to the function. But you'd want a variable in main to hold the value returned so you could continue processing.

void getMenuSelection(char var, char FtoC, char CtoF)

OR

It can remain void and you can pass the choice variable from main by reference, so any update to the choice variable in the function is made to original variable back in main.

In either case - you need to add in code to accept input from the user.
Before I continue presenting my problem to you I think I should make sure that you know I don't have to use a switch statement here. I can use something else if it would work better. I don't know. All the functions that I have listed I just have to use the name, the parameters are all based on what I think will work. If you know something better that would work I am all ears. I just don't want you banging your head against the wall on my problem if there is a better way. We have covered a number of things:

if, if...else, while, do while, switch, for loops, we just started working with vector statements.

As, I said I am in a beginner class so, I don't know all the options available. Thanks again.

I have tried making the changes you suggest but, I am still having an issue with errors.
I guess I would do something like this. Then use the menu selection in the next function to determine what or if a conversion needs to be done. I might use a switch there.

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
//function prototypes
char getMenuSelection();
void getStartEndAndIncrement(double&, double&, double&);

int main()
{
    char menuSelection;
    double startTemp, endTemp, increment;

    getStartEndAndIncrement(startTemp,endTemp,increment);
    displayMenu();
    menuSelection = getMenuSelection();
	//additional functions

    return 0;
}

void getStartEndAndIncrement(double& a, double& b, double& c)
{
	cout << "Enter the starting temperature: ";
	cin >> a; //passed by reference so updating startTemp in main
	cout << "\nEnter the ending temperature: ";
	cin >> b; //passed by reference so updating endTemp in main
	cout << "\nEnter the increment from start to end: ";
	cin >> c; //passed by reference so updating increment in main
}

void displayMenu()
{
	cout << "C - Celsius to Fahrenheit" << endl;
	cout << "F - Fahrenheit to Celsius" << endl;
	cout << "Q - No conversion" << endl;
	cout << "Enter selection (C,F or Q): ";
}

char getMenuSelection()
{
	char selection;
	cin >> selection;
	selection = toupper(selection); //include <cctype> to use toupper (converts to upper case)
	while (!(selection == 'C' || selection == 'F' || selection || 'Q'))
	{   
		cout << "Invalid entry - please re-enter (C,F or Q)";
		cin >> selection;
	}
	return selection;//return back to menuSelection variable in main
}
Last edited on
Oh wow, that is much smoother. I like the way you did the while (!(selection== )) and set it up for three logic applications. That is nice. I am sure I will get better at this stuff as I go but, to begin with I sure need more tutorial stuff then I have access to.

Now, to continue to the displayTable function I just have to do something like this then right?

displayTable = getMenuSelection() && getStartEndIncrement();

I am sure I will have to clean it up some.

Thank you so much for your help with this.

So, you don't really have to have a return in a function to get a value you can pull or call the value needed out of a function. That is sure something that was tripping me up. I thought you always had to have the function somehow return something for it to be used. Basically a function can be used just as a information storage unit. And, get called when ever that information is needed.
Last edited on
Hi Ruthgar,

I know you asked if there was any other way than using a switch (not having a go at wildblue, who provided an answer), but IMO a switch is much better for menu type stuff:

1. They are easily scalable - what if you had 20 options in the menu? Would you have a huge long if statement?

2. Bad input is easily caught with the default case, just like you had at the start.

3. It prevents nasty looking conditionals to weed out or prevent bad input.

A switch is good has to have constant value to work with, and this exactly what you have with a menu.

One can use an if, else if, else chain for example if you have ranges of double values, say.

Don't be under the misconception that shorter code is more smooth or elegant, often it is better to have more code (in a switch say) because it aids clarity, understanding, maintainability, is less error prone, harder to use wrongly, possibly better for the compiler to optimise.

Any way hope my 2 cents worth helped a bit, back to the very capable hands of wildblue : +
Yes, switches are handy and easy to read if you have a lot of options to handle.

2. Bad input is easily caught with the default case, just like you had at the start.


It catches the bad input with an error message, but you'd still need some sort of loop to reprompt and re-evaluate the new input in the switch, so that the function can return a valid value.
Yes, I put my switches in a bool controlled while loop, with a quit case.

Regards
Thank you guy's, it all helps. Thank you TheIdeasMan for the suggesting of using it in a loop. That makes perfect sense.

I have restarted and am working from function to function now. Obviously the displayMenu function should work with out a hitch since I am just printing out a an instruction and, it does. I have taken wildblue's suggestion and ran with it since I was having as much trouble as I was. I am still getting an error. I am not understanding what it needs.

1>c:\users\scotts\documents\visual studio 2013\projects\project6\project6\source.cpp(25): error C2660: 'getMenuSelection' : function does not take 0 arguments
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
#include<iostream>
#include<iomanip>
#include<cmath>


using namespace std;
inline void keep_window_open(){ char ch; cin >> ch; }

                         //Function Prototypes;

void displayMenu();
char getMenuSelection(char);


//  Program Main*****************************************************;

int main()
{

	
	char menuSelection, F, C, Q;

	cout << fixed << setprecision(1) << left;
	displayMenu();
	menuSelection = getMenuSelection();
	
	
	


	cin.ignore();
	return 0;      // End of Main**********************************;
}

// Instruction display;

void displayMenu()
{
	cout << "Please enter the starting temperature, the increments you wish to see from the\n";
	cout << "starting temp. to the ending temp. and the ending temperature and, if you wish\n";
	cout << "to convert from Celsius to Fahrenheit type 'C' or, Fahrenheit to Celsius type 'F'.\n";
	cout << "Type 'Q' to Quit.\n";
}

// Selecting conversion units;

char getMenuSelection(char menuSelection)
{
	cout << "Please enter 'F' to convert from F to C, 'C' to convert from C to F or, 'Q' to quit.\n";
	cin >> menuSelection;
	menuSelection = toupper(menuSelection);
	while (!(menuSelection == 'F' || menuSelection == 'C' || menuSelection == 'Q'))
	{
		cout << "This is an invalid entry.  Please try again.\n";
		cin >> menuSelection;
	}
	return menuSelection;
	
}




The getMenuSelection function doesn't need to receive a parameter.

You're getting the error because at line 25 the function is called with no parameters, but line 12 and line 47 say it is getting a char parameter.
Do I need parameters set on any of those lines? I have no parameters set in any of them now. I am receiving this error code.

1>c:\users\scotts\documents\visual studio 2013\projects\project6\project6\source.cpp(20): warning C4101: 'menuSelection' : unreferenced local variable
1>c:\users\scotts\documents\visual studio 2013\projects\project6\project6\source.cpp(52): error C2065: 'menuSelection' : undeclared identifier
1>c:\users\scotts\documents\visual studio 2013\projects\project6\project6\source.cpp(53): error C2065: 'menuSelection' : undeclared identifier
1>c:\users\scotts\documents\visual studio 2013\projects\project6\project6\source.cpp(54): error C2065: 'menuSelection' : undeclared identifier
1>c:\users\scotts\documents\visual studio 2013\projects\project6\project6\source.cpp(54): fatal error C1903: unable to recover from previous error(s); stopping compilation
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

I see there is a problem with line 20 but, it doesn't help to remove it. I still get an error.
You need to declare a local variable to hold the input you're getting in the function - the compiler doesn't know what menuSelection is. I think I had just called it selection in the post I have above. That holds the input and is returned to the menuSelection variable in main.

You don't need to declare F,C and Q as variables in the main function.

So, you have to declare a variable in the function then, you can't just declare it in the main and use it? Do I need to declare any variables in the main?
Variables are known only in the function or block in which they are declared. So a variable declared in main is not visible to a separate function, and vice versa. So if you're going to use a variable in main, you need to declare it there. If you need variables to do calculations or other processing in a function, you need to declare the variables there.

You can pass a variable by reference (e.g. void myFunction (char & myVariable);) which allows a function to modify the variable back in the calling function. Or you can have the function return a value back to the variable in main without passing the variable to the function at all.

There are many different ways to make this work :)

Pages: 12