GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 07-Mar-2004, 18:06
Skampy Skampy is offline
New Member
 
Join Date: Mar 2004
Posts: 4
Skampy is on a distinguished road
Question

Issues converting Fahrenheit to celcius [was: ok another one blowing my mind ...]


/*The code below was taken straight from Sam's book for demonstrating the use of local variables and parameters. Is supposed to convert Fahrenheit to celsius. Doesn't work though, keeps bailing out after first number is entered in program. I wrote it using my own method in code as seen below this this and seems to work fine converting the temperature both ways using the correct math. I'm glad it works and is more useful using my method but, I think I'm not learning the point by getting creative here. Any ideas why the book style doesn't work*/

CPP / C++ / C Code:
 
/*Sam's code*/
#include <iostream>
using namespace std;
float Convert(float);

int main()
{
float TempFer;
float TempCel;

cout<<"Please enter the temperature in Fahrenheit: ";
cin>>TempFer;
TempCel=Convert(TempFer);
cout<<"\nHere's the temperature in Celsius: ";
cout<<TempCel<<endl;

return 0;
}
float Convert(float TempFer)
{
float TempCel;
TempCel=((TempFer-32)*5)/9;

return TempCel;
}
/*Accomplished the correct and I think better approach using my method below*/
CPP / C++ / C Code:
#include <iostream>
using namespace std;
float Fahrenheit, Celsius;
int main()
{
float Fahrenheit;
float Celsius;

cout<<"Enter the temperature in Fahrenheit: ";
cin>>Fahrenheit;

Celsius=(Fahrenheit-32)*5/9;

cout<<"The temperature in Celsius is: "<<Celsius;
cout<<endl;
system ("pause");

cout<<"Enter the temperature in Celsius: ";
cin>>Celsius;

Fahrenheit=Celsius*9/5+32;

cout<<"The temperature in Fahrenheit is: "<<Fahrenheit;
cout<<endl;
system ("pause");
return 0;
}
Last edited by JdS : 08-Mar-2004 at 05:56. Reason: Please use a better title in your thread
  #2  
Old 07-Mar-2004, 19:03
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
Skampy, the reason the first version bails out after the number is entered is not an error. The program successfully completes its job, then exits. Try adding your system("pause"); at the end of the first version, before the return 0; part.
  #3  
Old 07-Mar-2004, 19:19
Skampy Skampy is offline
New Member
 
Join Date: Mar 2004
Posts: 4
Skampy is on a distinguished road

many thanks that is partially working


the program now gives the celsius temp then the press to continue option and bails. Based on the textbook this is supposed to be the output:
Please enter the temperature in Fahrenheit: 212
Here's the temperature in Celsius: 100
*now i'm getting the press to continue and it exists.
*the rest of these i'm not seeing even after I tried adding the system pause in the second function as well
Please enter the temperature in Fahrenheit: 32
Here's the termperature in Celsius: 0

Please enter the temperature in fahrenheit: 85
Here's the temperature in Celsius: 29.4444
*maybe could be my compiler i guess

Quote:
Originally Posted by aaroncohn
Skampy, the reason the first version bails out after the number is entered is not an error. The program successfully completes its job, then exits. Try adding your system("pause"); at the end of the first version, before the return 0; part.
  #4  
Old 07-Mar-2004, 20:05
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hi Skampy.

One thing real quick that helps a ton when you post code. If you place [c] before your code and [/c] after your code, it will be highlighted as I have editted above.

I think that it is great that you are experimenting with different ways to do things in C/C++. The book was probably trying to show the use of functions. Functions are the key to good programming (IMHO). Anyway, it appears that you got it to work both ways.
  #5  
Old 08-Mar-2004, 18:00
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by aaroncohn
Skampy, the reason the first version bails out after the number is entered is not an error. The program successfully completes its job, then exits. Try adding your system("pause"); at the end of the first version, before the return 0; part.
Haven't we had this discussion before? Don not use system("pause");, it's not portable and is an expensive call to the OS. Use:
CPP / C++ / C Code:
cin.get();
  #6  
Old 24-Feb-2006, 16:00
mrgrim333 mrgrim333 is offline
New Member
 
Join Date: Feb 2006
Location: Colorado
Posts: 1
mrgrim333 is on a distinguished road

Re: Issues converting Fahrenheit to celsius


Hey, I was having the same problem... But there is a solution. Its Simple too.
all you have to do is open you're command prompt (start/run/cmd) and click and drag your compiled file into it then press enter... It will run it and not kill it's self after you press a key.

Hope I helped.
  #7  
Old 24-Feb-2006, 18:13
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Issues converting Fahrenheit to celsius


Quote:
Originally Posted by mrgrim333
Hey, I was having the same problem... But there is a solution. Its Simple too.
all you have to do is open you're command prompt (start/run/cmd) and click and drag your compiled file into it then press enter... It will run it and not kill it's self after you press a key.

Hope I helped.
Probably not, since this thread was 2 years old!!!. And the responses that came before fixed it easier than this solution...
__________________

During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence?
  #8  
Old 25-Feb-2006, 09:04
admin's Avatar
admin admin is offline
Administrator
 
Join Date: Sep 2002
Posts: 841
admin will become famous soon enough

Re: Issues converting Fahrenheit to celsius


Really, it doesn't matter how old this (or any other) thread is.

What matters is that any new posts / replies appended to it are relevant, up to date, and/or useful.

If it's not, just say so or better yet, use the "Report Bad Post" button to have them (useless posts) deleted.
__________________
Custom BB codes you can use here:
[HTML] | [C++] | [CSS] | [JAVA] | [PY] | [VB]
  #9  
Old 07-Feb-2009, 20:35
XodoX XodoX is offline
New Member
 
Join Date: Feb 2009
Posts: 4
XodoX is on a distinguished road

Re: Issues converting Fahrenheit to celsius


I need to do the same.

I'm almost finished, but I got another problem. Additionally I want to round up or down the Celsius answer by adding 0.5 to the calculation expression.
I'm new to this, so I am not quite sure how to do this. Can someone help me?

Also ,I don't understand why I am supposed to add 0.5... how is that gonna round it up or down??
  #10  
Old 08-Feb-2009, 00:59
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Issues converting Fahrenheit to celsius


The reason you add 0.5 is because converting a floating point number to an integer will truncate the value. 1.2 becomes 1, 3.6 becomes 3, 5.999 becomes 5.

So if you have 4.7, truncating gives you 4 but you clearly want 5 when rounding. Therefore add 0.5 first. You now have 5.2 which becomes 5.

And why 0.5? Because any number below x.5 rounds to x. Any number equal to or greater than x.5 rounds up to x+1.
__________________

During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence?
 
 

Recent GIDBlogProblems with the Navy (Chiefs) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
newbie using Sam's series Skampy C++ Forum 3 06-Mar-2004 19:51
(read/write file) newbie need help plz momotx C Programming Language 6 28-Jan-2004 14:40
Newbie help with unary ops Tony C Programming Language 2 04-Jan-2004 20:10
newbie help milwalt C++ Forum 1 27-Dec-2003 12:52
total newbie need help grunt123 Apache Web Server Forum 2 04-Dec-2003 12:14

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 23:48.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.