Jump to content
  • Advertisement
Sign in to follow this  
SlippyPulse

Not getting same answer in c++

This topic is 4775 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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; } }

Share this post


Link to post
Share on other sites
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;
}
}

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Please use functions along with a main loop instead of goto .



#include <iostream> // no .h

void 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)+32
cout<<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;
}



Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!