Celsius to Fahrenheit wrong increment

i am new to programming, so bare with me. I know that only C to F works but once i figure that out everything else will be easy. The problem i have is that when i display the table my increment of the conversion is wrong:

==============================================
|********************************************|
|*********** Celcius to Farenheit ***********|
|********************************************|
---------------------------------------------
| Start C || Ending C | Increment |
=============================================
| Celsius || Farenheit |
==============================================
| 1 || 33.8 |
| 2 || 34.8 |
| 3 || 35.8 |
| 4 || 36.8 |
| 5 || 37.8 |
| 6 || 38.8 |
| 7 || 39.8 |
| 8 || 40.8 |
| 9 || 41.8 |
| 10 || 42.8 |
==============================================
==============================================

Here is my code, btw i know do know that my table is not aligned right but in the program it is :P:

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

void displayMenu (void);
char getMenuSelection (void);
void getStartEndAndIncrement(double& start, int& end, int& increment);
void CtoF(double& celsius, double& F,int& increment,double& start);
void displayCtoFTable(double& start, int& end, int& increment,double& F);
int end, increment;
char choice;
double start;
double celsius;
double F;

int main()
{
displayMenu ();
getMenuSelection ();
getStartEndAndIncrement (start,end,increment);
if (choice=='C'){
CtoF(celsius,F,increment,start);
displayCtoFTable(start,end,increment,F);}
cin.ignore(2);
}

void displayMenu ()
{
cout << "Celsius and Fahrenheit Temperation Converter\n" << endl;
cout << "Please choose from the following options\n" << endl;
cout << "C: Convert from CELSIUS to FAHRENHEIT" << endl;
cout << "F: Convert from FAHRENHEIT to CELSIUS" << endl;
cout << "Q: Quit" << endl;
}
char getMenuSelection ()
{

cout << "\nEnter your choice: ";
cin >> choice;


switch (choice)
{
case 'C':
cout <<"You have choosen a CELSIUS to FAHRENHEIT conversion"<<endl;
return choice;
break;

case 'F':
cout <<"You have choosen a FAHRENHEIT to CELSIUS"<<endl;
return choice;
break;

case 'Q':
cout <<"Press ENTER to terminate program"<<endl;
cin.ignore(2);
return choice;
}
do {
cout << "Sorry, invalid choice\n"<<endl;
displayMenu ();
getMenuSelection ();
break;
} while (choice != 'C' && choice != 'F' &&
choice != 'Q');
}
void getStartEndAndIncrement(double& start, int& end, int& increment)
{
cout << "Enter start:";
cin >> start;
cout << "Enter End:";
cin >> end;
cout << "Enter Increment:";
cin >> increment;
}
void CtoF(double& celsius, double& F,int& increment,double&start)
{
celsius = start;
F = (9.0 * celsius/5.0) + 32.0;
cout.precision(3);
}
void displayCtoFTable(double& start, int& end, int& increment,double&F)
{
cout << "==============================================" << endl;
cout << "|********************************************|" << endl;
cout << "|*********** Celcius to Farenheit ***********|" << endl;
cout << "|********************************************|" << endl;
cout << "---------------------------------------------" << endl;
cout << "| Start C || Ending C | Increment |" << endl;
cout << "============================================= " << endl;
cout << "| Celsius || Farenheit |" << endl;
cout << "==============================================" << endl;
for (celsius = start; celsius <= end; celsius+=increment){
cout <<"|"<< setw(10)<< celsius<<setw(11)<<"||"
<<setw(13)<<F++<<setw(11)<<"|"<<"\n";}

cout << "==============================================" << endl;
cout << "==============================================" << endl;
}
<<setw(13)<<F++<<setw(11)<<"|"<<"\n";}

You problem is here. Each iteration you are simply adding 1 to the previous farenheit value. In actuality you need to plug each celcius value into the formula to obtain each farenheit value. The CtoF function is much more useful if you change the return type to double, and call it in the above line. The following code demonstrates this, I have commented the lines I have changed.

Additionally, I'm not sure what you are doing with your variables. First you declared all of them globally, bad habit to get into. Then you proceed to pass the variables to functions, unnecessary if they are global. Also, you only need to pass to each function the variables it will actually use. Also, you made all your variables reference variables, also unnecessary. Pass only what you need then return what the calling function needs.

Finally, when posting code to the forum, please use the code brackets by pressing the # key to the right of the text box. This formats the code for easy reading.

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
#include <iostream>
#include <iomanip>
using namespace std;

void displayMenu (void);
char getMenuSelection (void);

double CtoF(double celsius); //return type changed to double,removed unnecessary variables
void displayCtoFTable(double, int, int);//removed F, variable names are unnecessary in prototypes


int main()
{
int end, increment;
char choice;
double start;
double celsius;
double F;
displayMenu ();
choice=getMenuSelection (); //added choice = to make use of the function's return

//this is what used to be your getstartendandincrement function.
cout << "Enter start:";
cin >> start;
cout << "Enter End:";
cin >> end;
cout << "Enter Increment:";
cin >> increment;


if (choice=='C'){
//you didn't need to call CtoF here, it will be called in the table function
displayCtoFTable(start,end,increment); //you didn't need the F variable here, in fact you didn't need it at all
}                                            //except locally in the CtoF function
cin.ignore(2);
}

void displayMenu ()
{
cout << "Celsius and Fahrenheit Temperation Converter\n" << endl;
cout << "Please choose from the following options\n" << endl;
cout << "C: Convert from CELSIUS to FAHRENHEIT" << endl;
cout << "F: Convert from FAHRENHEIT to CELSIUS" << endl;
cout << "Q: Quit" << endl;
}
char getMenuSelection ()
{
char choice;  //added local declaration
cout << "\nEnter your choice: ";
cin >> choice;


switch (choice)
{
case 'C':
cout <<"You have choosen a CELSIUS to FAHRENHEIT conversion"<<endl;
return choice;
break;

case 'F':
cout <<"You have choosen a FAHRENHEIT to CELSIUS"<<endl;
return choice;
break;

case 'Q':
cout <<"Press ENTER to terminate program"<<endl;
cin.ignore(2);
return choice;
}
do {
cout << "Sorry, invalid choice\n"<<endl;
displayMenu ();
getMenuSelection ();
break;
} while (choice != 'C' && choice != 'F' &&
choice != 'Q');
}

double CtoF(double celsius) //changed return type to double, removed unnecessary variables
{
double F;  //added local declaration
//celsius = start; //removed, this would reset celcius every time.
F = (9.0 * celsius/5.0) + 32.0;
cout.precision(3);
return F;  //F is returned to display function
}
void displayCtoFTable(double start, int end, int increment) //removed f and references
{
cout << "==============================================" << endl;
cout << "|********************************************|" << endl;
cout << "|*********** Celcius to Farenheit ***********|" << endl;
cout << "|********************************************|" << endl;
cout << "---------------------------------------------" << endl;
cout << "| Start C || Ending C | Increment |" << endl;
cout << "============================================= " << endl;
cout << "| Celsius || Farenheit |" << endl;
cout << "==============================================" << endl;
for (double celsius = start; celsius <= end; celsius+=increment){ //added local declaration of celsius
cout <<"|"<< setw(10)<< celsius<<setw(11)<<"||"
<<setw(13)<<CtoF(celsius)<<setw(11)<<"|"<<"\n";  //CtoF function is called
} 

cout << "==============================================" << endl;
cout << "==============================================" << endl;
}
thanks for your tips, but i already figured out how to display everything properly. I still need to remove the global variables though lol , i knew it was simple i was not returning my calculations, eg. return F.

thanks!

i will post my result later...
Last edited on
Here is my code thanks for you help. I actually have to keep the getstartendandincrement function.

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
144
145
146
#include <iostream>
#include <iomanip>
using namespace std;
#define DEGREE ((char)248)

void displayMenu (void);
char getMenuSelection (void);
void getStartEndAndIncrement(float& start, float& end, float& increment);
float CtoF(float);
void displayCtoFTable(float, float, float);
float FtoC(float);
void displayFtoCTable(float,float,float);
char choice;

int main()
{
float end, increment,start;


	do{
displayMenu ();
choice=getMenuSelection ();
getStartEndAndIncrement (start,end,increment);
if (choice=='C' || choice=='c'){
displayCtoFTable(start,end,increment);}
else if (choice =='F' || choice=='f'){
displayFtoCTable(start,end,increment);}
	}
	while (choice !='Q'|| choice !='q');
}

void displayMenu ()
{
  	cout << "Celsius and Fahrenheit Temperation Converter\n" << endl;
	cout << "Please choose from the following options\n" << endl;
	cout << "C: Convert from CELSIUS to FAHRENHEIT" << endl;
	cout << "F: Convert from FAHRENHEIT to CELSIUS" << endl;
	cout << "Q: Quit" << endl;
}
char getMenuSelection ()
{
	
	cout << "\nEnter your choice: ";
	cin >> choice;
	

	  	switch (choice)
	{
		case 'c':
		case 'C':
			cout <<"\nYou have choosen a CELSIUS to FAHRENHEIT conversion"<<endl;
			return choice;
			break;
		case 'f':
		case 'F':
			cout <<"\nYou have choosen a FAHRENHEIT to CELSIUS"<<endl;
			return choice;
			break;
		case 'q':
		case 'Q':
			cin.get();
			cout <<"\nPress ENTER to terminate program.."<<endl;
			cin.get();
            exit(0);
			return choice;
			break;
	}
	do {
		cout << endl;
		cout << "Invalid choice, press ENTER\n"<<endl;
		cin.ignore(2);
	    displayMenu ();
		getMenuSelection ();
		break;
} while (choice != 'C' && choice != 'F' &&
		choice != 'Q');
}
void getStartEndAndIncrement(float& start, float& end, float& increment)
{
  cout << "\nEnter a temperature to start from:";
  cin >> start;
  cout << "Enter a temperature to end at:";
  cin >> end;
  cout << "Enter the temperature increment:";
  cin >> increment;
 }
float CtoF(float celsius)
{
	float F;
	F = 1.8 * celsius + 32.0;
	cout << fixed;
	cout.precision(1);
	return F;
}
float FtoC(float farh)
{
	float C;
	C = (5.0*(farh - 32.0))/9.0;
	cout << fixed;
	cout.precision(1);
	return C;
}
void displayCtoFTable(float start, float end, float increment)
{
	cout << endl;
	cout << setw(10) <<"Increment: " << increment<<endl;
	cout << "==============================================" << endl;
	cout << "|********************************************|" << endl;
	cout << "|*********** Celcius to Farenheit ***********|" << endl;
    cout << "|********************************************|" << endl;
	cout << "============================================= " << endl;	
	cout << "|         C"<<DEGREE<<"         ||          F"<<DEGREE<<"          |" << endl;
	cout << "==============================================" << endl;

	for (float celsius = start; celsius <= end; celsius+=increment){
        cout <<"|"<< setw(10)<<celsius<<setw(11)<<"||"
		<<setw(13)<<CtoF(celsius)<<setw(11)<<"|"<<"\n";}

	cout << "==============================================" << endl;	
	cout << "==============================================" << endl;
	cout << "\nPress the ENTER to continue...";
    cin.ignore(2);

}
void displayFtoCTable(float start, float end, float increment)
{
	cout << endl;
	cout << setw(10) <<"Increment: " << increment<<endl;
	cout << "==============================================" << endl;
	cout << "|********************************************|" << endl;
	cout << "|*********** Farenheit to Celsius ***********|" << endl;
    cout << "|********************************************|" << endl;
	cout << "==============================================" << endl;	
	cout << "|         F"<<DEGREE<<"         ||          C"<<DEGREE<<"          |" << endl;
	cout << "==============================================" << endl;

	for (float farh = start; farh <= end; farh+=increment){
        cout <<"|"<< setw(10)<<farh<<DEGREE<<setw(11)<<"||"
		<<setw(13)<<FtoC(farh)<<DEGREE<<setw(9)<<"|"<<"\n";}

	cout << "==============================================" << endl;	
	cout << "==============================================" << endl;
	cout << "\nPress the ENTER to continue...";
    cin.ignore(2);
}
Last edited on
Much better, good job.
thanks for your tips now i really understand how functions work.
Last edited on
Topic archived. No new replies allowed.