Not getting same answer in c++

Started by
4 comments, last by Scet 18 years, 8 months ago
Ive decided to write a program in c++ to convert celsius to fahrenheit and visa versa. I got everthing good so far except that when i convert far. to cel. and then i convert the same temp reversered, im not getting the same answer. Any1 know y this is caused? heres my code: #include <iostream.h> main() { /*Starting Point!*/ start: system("cls"); float ftemp,ctemp; int endans, choice; cout<<"This program converts Fahrenheit to Celsius or visa versa.\n\n"; cout<<"Enter '1' to convert Fahrenheit to Celsius\n"; cout<<"Enter '2' to convert Celsius to Fahrenheit: \n"; cin>>choice; /*Read the users input and goes to the correct conversion*/ if(choice==1){ goto fc; } else if(choice==2){ goto cf; } else { goto start; } /*Fahrenheit to Celsius!*/ fc: cout<<"Enter the Fahrenheit temperature: "; cin>>ftemp; ctemp=(ftemp-32)*0.6; cout<<ftemp<<" Fahrenheit = "<<ctemp<<" Celsius!\n\n"; goto end; /*Celsius to Fahrenheit!*/ cf: cout<<"Enter the Celsius temperature: "; cin>>ctemp; ftemp=(ctemp*0.6)+32; cout<<ctemp<<" Celsius = "<<ftemp<<" Fahrenheit!\n\n"; goto end; /*The ending sequence!*/ end: cout<<"Do you want to go again?\n"; cout<<"'1' for YES\n"; cout<<"Anything other NUMBER for NO!: "; cin>>endans; /*Sends user to beginning if answered 1*/ if(endans==1){ goto start; } else { exit; } }
Advertisement
uaaaah, source tags please :)
[ source ]

blabla

[ / source ]

NO FIX! JUST IN SOURCE TAGS:

#include <iostream.h>main(){	/*Starting Point!*/start:	system("cls");	float ftemp,ctemp;	int endans, choice;	cout<<"This program converts Fahrenheit to Celsius or visa versa.\n\n";	cout<<"Enter '1' to convert Fahrenheit to Celsius\n";	cout<<"Enter '2' to convert Celsius to Fahrenheit: \n";	cin>>choice;	/*Read the users input and goes to the correct conversion*/	if(choice==1){		goto fc;	}	else if(choice==2){		goto cf;	}	else {		goto start;	}	/*Fahrenheit to Celsius!*/fc:	cout<<"Enter the Fahrenheit temperature: ";	cin>>ftemp;	ctemp=(ftemp-32)*0.6;	cout<<ftemp<<" Fahrenheit = "<<ctemp<<" Celsius!\n\n";	goto end;	/*Celsius to Fahrenheit!*/cf:	cout<<"Enter the Celsius temperature: ";	cin>>ctemp;	ftemp=(ctemp*0.6)+32;	cout<<ctemp<<" Celsius = "<<ftemp<<" Fahrenheit!\n\n";	goto end;	/*The ending sequence!*/end:	cout<<"Do you want to go again?\n";	cout<<"'1' for YES\n";	cout<<"Anything other NUMBER for NO!: ";	cin>>endans;	/*Sends user to beginning if answered 1*/	if(endans==1){		goto start;	}	else {		exit;	}}
Now get down on your hands and knees and start repeating "Open Source Good, M$ Evil", smacking your head against the pavement after each repetition. Once you have completed your training you may change your first name to GNU/, to show that you are free from the slavery of the closed source world. -Michalson
Your formula for converting from celsius to fahrenheit is wrong. It should be "/0.6", not *0.6".

Generally, programmers test their code by trying known values and making sure the results are correct. For example, if you try your code, converting 100 celsius to fahrenheit, you will get the answer 92. That is wrong so you know that something is wrong with that code. If you test your code with 212 fahrenheit, you will get the answer 108 celsius. If that is not close enough to 100 for you, you know that you need to make it more accurate.

One final comment, try rewriting your program without using goto.

Edit: oops, fixed typo

[Edited by - JohnBolton on July 24, 2005 2:11:20 PM]
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Your sums are both wrong for starters.
ftemp=(ctemp* 9) / 5)+ 32;<- thats what it should be to get Fahrenheit.
ctemp=(ftemp- 32) * 5) / 9; <-- and thats for celcius.

To check out if it correct do boiling point of water (212 F and 100 C)
then freezing point(32 F and 0 C).
Quote:Original post by JohnBolton
One final comment, try rewriting your program with using goto.


I think he means not using goto. try using a do....while loop

Please use functions along with a main loop instead of goto .

#include <iostream> // no .hvoid Fahrenheit(){ float f,c;cout << "Enter the Fahrenheit temperature: ";cin >> f;c=(f-32)*5)/9;cout<<f<<" Fahrenheit = "<<c<<" Celsius!\n\n";return;}void Celsius(){ float f,c;cout<<"Enter the Celsius temperature: ";cin>>c;f=(c*9)/5)+32cout<<c<<" Celsius = "<<f<<" Fahrenheit!\n\n";return;}void End(int &result){cout<<"Do you want to go again?\n";cout<<"'1' for YES\n";cout<<"Anything other NUMBER for NO!: ";cin>>result;return;}int main(){int endans, choice;		/*Starting Point!*/		while(1){			cout<<"This program converts Fahrenheit to Celsius or visa versa.\n\n";	cout<<"Enter '1' to convert Fahrenheit to Celsius\n";	cout<<"Enter '2' to convert Celsius to Fahrenheit: \n";	cin>>choice;	/*Read the users input and goes to the correct conversion*/		switch(choice){	case 1: Fahrenheit(); End(endans); break;	case 2: Celsius(); End(endans); break;	default: endans=1; break;};	/*Exits if other than 1. Loops continues if otherwise*/	if(endans!=1){break;}		}return 0;}

This topic is closed to new replies.

Advertisement