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, 17: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 04:56. Reason: Please use a better title in your thread
  #2  
Old 07-Mar-2004, 18: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, 18: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, 19: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, 17:00
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,245
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, 15: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, 17:13
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,245
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...
__________________

Age is unimportant -- except in cheese
  #8  
Old 25-Feb-2006, 08:04
admin's Avatar
admin admin is offline
Administrator
 
Join Date: Sep 2002
Posts: 761
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]
 
 

Recent GIDBlogPython ebook 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 18:51
(read/write file) newbie need help plz momotx C Programming Language 6 28-Jan-2004 13:40
Newbie help with unary ops Tony C Programming Language 2 04-Jan-2004 19:10
newbie help milwalt C++ Forum 1 27-Dec-2003 11:52
total newbie need help grunt123 Apache Web Server Forum 2 04-Dec-2003 11:14

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

All times are GMT -6. The time now is 05:45.


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