Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Temperature Conversion Program - Still a Rookie

Reply
Thread Tools

Temperature Conversion Program - Still a Rookie

 
 
Ian Collins
Guest
Posts: n/a
 
      04-01-2007
wrote:
> Alright guys....
> I agree with you that stdafx.h does not need to be included but it
> won't compile without it.


Nonsense.

> Ok....so,I started playing around with it and I got the program to
> finally compile and it is working fairly ok.
> The program will exit when I enter a value because temp,
> tempFahrenheit, and tempCelsius have not been intialized. When I set
> it to 0 I still get the same problem. Any ideas?


It will exit because it falls out of main, there is nothing after your
last output line.

> Oh by the way....here is the updated code from this afternoon.
>
> // Temperature.cpp : Defines the entry point for the console
> application.
> //
> #include "stdafx.h"
> #include <iostream>
> #include <cstdlib>


You don't use anything from <cstdlib>, so it can go as well.

> using namespace std;
>
> double FahrenheittoCelsius(double); //Function Prototype
> double CelsiustoFahrenheit(double);
>
> int main()
> {
> double tempFahrenheit, tempCelsius;
> double temp;
> char corf;
>
> cout << " Enter the Temperature: "; //Prompt user to enter temp
> cin >> temp;
> cout << " Fahrenheit or Celsius? ";
> cin >> corf;
>
> if (corf == 'f')
> {
> tempCelsius = FahrenheittoCelsius( temp );
> cout << tempFahrenheit << " degrees Fahrenheit is" << tempCelsius
> << " degrees celsius ";


You don't initialise tempFahrenheit, so garbage will be output, same
fortempCelsius in the next block.

> cout << " Continue? ";
>

You could do the above after the if() blocks to avoid duplication.

> }
> if (corf == 'c')
> {
> tempFahrenheit = CelsiustoFahrenheit(temp);
> cout << tempCelsius << " degrees Celsius is " << tempFahrenheit << "
> degrees Fahrenheit";
> cout << " Continue? ";
>
> }
>
> return temp;


This isn't a legal return value from main, 0, EXIT_SUCCESS and
EXIT_FAILURE are the only portable returns.

--
Ian Collins.
 
Reply With Quote
 
 
 
 
AsheeG87@msn.com
Guest
Posts: n/a
 
      04-01-2007
On Apr 1, 4:56 pm, Ian Collins <ian-n...@hotmail.com> wrote:
> Ashee...@msn.com wrote:
> > Alright guys....
> > I agree with you that stdafx.h does not need to be included but it
> > won't compile without it.

>
> Nonsense.
>
> > Ok....so,I started playing around with it and I got the program to
> > finally compile and it is working fairly ok.
> > The program will exit when I enter a value because temp,
> > tempFahrenheit, and tempCelsius have not been intialized. When I set
> > it to 0 I still get the same problem. Any ideas?

>
> It will exit because it falls out of main, there is nothing after your
> last output line.
>
> > Oh by the way....here is the updated code from this afternoon.

>
> > // Temperature.cpp : Defines the entry point for the console
> > application.
> > //
> > #include "stdafx.h"
> > #include <iostream>
> > #include <cstdlib>

>
> You don't use anything from <cstdlib>, so it can go as well.
>
>
>
>
>
> > using namespace std;

>
> > double FahrenheittoCelsius(double); //Function Prototype
> > double CelsiustoFahrenheit(double);

>
> > int main()
> > {
> > double tempFahrenheit, tempCelsius;
> > double temp;
> > char corf;

>
> > cout << " Enter the Temperature: "; //Prompt user to enter temp
> > cin >> temp;
> > cout << " Fahrenheit or Celsius? ";
> > cin >> corf;

>
> > if (corf == 'f')
> > {
> > tempCelsius = FahrenheittoCelsius( temp );
> > cout << tempFahrenheit << " degrees Fahrenheit is" << tempCelsius
> > << " degrees celsius ";

>
> You don't initialise tempFahrenheit, so garbage will be output, same
> fortempCelsius in the next block.
>
> > cout << " Continue? ";

>
> You could do the above after the if() blocks to avoid duplication.
>
> > }
> > if (corf == 'c')
> > {
> > tempFahrenheit = CelsiustoFahrenheit(temp);
> > cout << tempCelsius << " degrees Celsius is " << tempFahrenheit << "
> > degrees Fahrenheit";
> > cout << " Continue? ";

>
> > }

>
> > return temp;

>
> This isn't a legal return value from main, 0, EXIT_SUCCESS and
> EXIT_FAILURE are the only portable returns.
>
> --
> Ian Collins.- Hide quoted text -
>
> - Show quoted text -


I hear ya...but it won't go any further until I intialize that
variable.

 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      04-01-2007
wrote:
>>You don't initialise tempFahrenheit, so garbage will be output, same
>>fortempCelsius in the next block.
>>
>>
>>> cout << " Continue? ";

>>
>>You could do the above after the if() blocks to avoid duplication.
>>
>>
>>> }
>>> if (corf == 'c')
>>> {
>>> tempFahrenheit = CelsiustoFahrenheit(temp);
>>> cout << tempCelsius << " degrees Celsius is " << tempFahrenheit << "
>>>degrees Fahrenheit";
>>> cout << " Continue? ";

>>
>>> }

>>
>>> return temp;

>>
>>This isn't a legal return value from main, 0, EXIT_SUCCESS and
>>EXIT_FAILURE are the only portable returns.
>>
>>--
>>Ian Collins.- Hide quoted text -
>>
>>- Show quoted text -

>

Please don't quote signatures, or that google quoted text crap.
>
> I hear ya...but it won't go any further until I intialize that
> variable.
>

What won't? Looking though what you posted, it will run through once,
then exit.

--
Ian Collins.
 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      04-01-2007
wrote:

> Alright guys....
> I agree with you that stdafx.h does not need to be included but it
> won't compile without it.


That's only because you started with the wrong sort of project
initially. If you can't fix it by changing the project settings, start
over with a new "console" project and include that file in the new
project.




Brian
 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      04-02-2007
On Apr 2, 8:56 am, Ian Collins <ian-n...@hotmail.com> wrote:
> Ashee...@msn.com wrote:
> > Alright guys....
> > I agree with you that stdafx.h does not need to be included but it
> > won't compile without it.

>
> Nonsense.


How do you know whether or not the OP's compiler will compile
code without that line? In fact it seems likely from his
description that the compilation fails when he deletes it.

> > tempCelsius = FahrenheittoCelsius( temp );
> > cout << tempFahrenheit << " degrees Fahrenheit is" << tempCelsius
> > << " degrees celsius ";

>
> You don't initialise tempFahrenheit, so garbage will be output


The behaviour will be undefined -- it could crash the program, etc.

> > return temp;

>
> This isn't a legal return value from main, 0, EXIT_SUCCESS and
> EXIT_FAILURE are the only portable returns.


Other return values are legal. The only 'problem' is that the
operating system may not be able to determine whether the
program succeeded or failed.

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      04-02-2007
Old Wolf wrote:
> On Apr 2, 8:56 am, Ian Collins <ian-n...@hotmail.com> wrote:
>
>>Ashee...@msn.com wrote:
>>
>>>Alright guys....
>>>I agree with you that stdafx.h does not need to be included but it
>>>won't compile without it.

>>
>>Nonsense.

>
> How do you know whether or not the OP's compiler will compile
> code without that line? In fact it seems likely from his
> description that the compilation fails when he deletes it.
>

He made no mention of a complier, just that the code wouldn't compile
without it. On my system, it won't compile with it!

--
Ian Collins.
 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      04-02-2007
<> wrote in message
news: oups.com...
> Alright guys....
> I agree with you that stdafx.h does not need to be included but it
> won't compile without it.


Make sure you remove the stdafx.cpp from the progject and it should. This
code does for me in VC++ .net 2003 without stdafx.h

> Ok....so,I started playing around with it and I got the program to
> finally compile and it is working fairly ok.
> The program will exit when I enter a value because temp,
> tempFahrenheit, and tempCelsius have not been intialized. When I set
> it to 0 I still get the same problem. Any ideas?
> Oh by the way....here is the updated code from this afternoon.


Run the program with ctrl-F5 instead of F5 and you will get a
Press any key to continue...
after the program runs allowing you to see the output. This is Microsoft
specific.

Personally, I just make it wait in code by putting at the end of main
something on the line of:
std::string wait;
std::getline( std::cin, wait );

>
> // Temperature.cpp : Defines the entry point for the console
> application.
> //
> #include "stdafx.h"
> #include <iostream>
> #include <cstdlib>
> using namespace std;
>
> double FahrenheittoCelsius(double); //Function Prototype
> double CelsiustoFahrenheit(double);
>
> int main()
> {
> double tempFahrenheit, tempCelsius;
> double temp;
> char corf;
>
>
>
> cout << " Enter the Temperature: "; //Prompt user to enter temp
> cin >> temp;


Okay, the temperature is entered into a variabled called "temp".

> cout << " Fahrenheit or Celsius? ";
> cin >> corf;
>
> if (corf == 'f')
> {
> tempCelsius = FahrenheittoCelsius( temp );
> cout << tempFahrenheit << " degrees Fahrenheit is" << tempCelsius
> << " degrees celsius ";


The variable "tempFahrenheit" is not being used. The value was stored in
temp, not tempFahrenheit.
cout << temp << " degrees Fahrenheit is" << tempCelsius << " degrees
celsius ";

> }
> if (corf == 'c')
> {
> tempFahrenheit = CelsiustoFahrenheit(temp);
> cout << tempCelsius << " degrees Celsius is " << tempFahrenheit << "
> degrees Fahrenheit";


Same here, output temp, not tempCelsius.

> cout << " Continue? ";
>
> }
>
> return temp;
> }
> double FahrenheittoCelsius (double f)
> {
> return (f - 32) * (5.0/9);
> }
> double CelsiustoFahrenheit (double c)
> {
> return (9/5.0 * c) + 32;
> }



 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      04-02-2007
On Apr 1, 8:40 pm, "Default User" <defaultuse...@yahoo.com> wrote:
> Jim Langston wrote:
> > You send the message to the console for the user to enter the
> > Temperature, but then you have them input if it's celsius or
> > centigrade.


> > This should be:


> > cout << " Enter the Temperature: "; //Prompt user to enter temp
> > cin >> temp;
> > cout << "Celcius or Farenheit? ";
> > cin >> corf;


> > > if (corf == 'f')

>
> This isn't a very robust design. There are all kinds of unused
> characters left hanging around. I'd recommend reading in the entire
> line and examining the first character.


The first non-blank character, and then verifying that there
aren't any other non-blanck characters in the line.

--
James Kanze (GABI Software) email:
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      04-02-2007
On Apr 1, 10:56 pm, Ian Collins <ian-n...@hotmail.com> wrote:
> Ashee...@msn.com wrote:
> > Alright guys....
> > I agree with you that stdafx.h does not need to be included but it
> > won't compile without it.


> Nonsense.


Worse. On my systems, it won't compile with it.

[...]
> > return temp;


> This isn't a legal return value from main, 0, EXIT_SUCCESS and
> EXIT_FAILURE are the only portable returns.


Just a nit: it's legal, temp is a double, and a double converts
implicitly to an int. It doesn't make any sense, and the
results are very much implementation defined, but it is legal.

--
James Kanze (GABI Software) email:
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      04-02-2007
James Kanze wrote:
> On Apr 1, 10:56 pm, Ian Collins <ian-n...@hotmail.com> wrote:
>
>>This isn't a legal return value from main, 0, EXIT_SUCCESS and
>>EXIT_FAILURE are the only portable returns.

>
>
> Just a nit: it's legal, temp is a double, and a double converts
> implicitly to an int. It doesn't make any sense, and the
> results are very much implementation defined, but it is legal.
>

You're right, I should have written "portable" rather than "legal".

> --


*Please* fix this - it's a pain to have to keep manually trimming your
signature.

--
Ian Collins.
 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
a temperature conversion programme arnuld C++ 4 03-04-2007 10:13 AM
question about 290,291,270,228 from a rookie duke MCSE 1 02-26-2006 06:36 AM
Temperature conversion deanfamily11 C++ 1 09-10-2005 12:24 AM
MCSE rookie need help Marko MCSE 0 09-16-2003 05:43 AM
Re: MCSE rookie need help gg77 MCSE 0 09-15-2003 05:34 PM



Advertisments
 



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