473,756 Members | 3,973 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Temperature Conversion Program - Still a Rookie

Hey Everyone~
I'm still a C++ Rookie so please bear with me on this.
I'm doing a temperature conversion program with prototype functions.
Basicly, I was wondering if some of you would take a look at my code
and critique it for me. I'm mostly concerned with how prototype
functions work and if I designed them correctly in my code.
Your participation would be greatly appreciated!

My Code:

#include "stdafx.h"
#include <iostream>
#include <cstdlib>

double FahrenheittoCel sius(double)= 0.0; //Function Prototype
double CelsiustoFahren heit(double)= 0.0;

int _tmain(int argc, _TCHAR* argv[])
{
double tempFahrenheit, tempCelsius;
double temp;
char f;
char c;
cout << " Enter the Temperature: "; //Prompt user to enter temp
cin >temp;
cout << " Fahrenheit or Celsius?"; // If user does not specify f or c
ask them
if (temp == f)
{
c = FahrenheittoCel sius;
cout << f " degrees Fahrenheit is" c " degrees celsius ";
cout << " Continue? ";

else
f = CelsiustoFahren heit;
cout << c " degrees Celsius is " f " degrees Fahrenheit ";
cout << " Continue? ";

}
return temp;
}
double FahrenheitoCels ius (double f)
{
return (f - 32) * (5.0/9);
}
double CelsiustoFahren heit (double c)
{
return (9/5.0 * c) + 32;
}

Mar 31 '07 #1
21 4263
As******@msn.co m wrote:
Hey Everyone~
I'm still a C++ Rookie so please bear with me on this.
I'm doing a temperature conversion program with prototype functions.
Basicly, I was wondering if some of you would take a look at my code
and critique it for me. I'm mostly concerned with how prototype
functions work and if I designed them correctly in my code.
Your participation would be greatly appreciated!

My Code:
I assume you haven't tried to compile this!

Some hints:
#include "stdafx.h"
Non-standard header.
#include <iostream>
#include <cstdlib>

double FahrenheittoCel sius(double)= 0.0; //Function Prototype
double CelsiustoFahren heit(double)= 0.0;
These are syntax errors, not prototypes. Loose the = 0.0.
int _tmain(int argc, _TCHAR* argv[])
That's _TCHAR? Use

int main( int argc, char* argv[] )

{
double tempFahrenheit, tempCelsius;
double temp;
char f;
char c;
cout << " Enter the Temperature: "; //Prompt user to enter temp
cin >temp;
cout << " Fahrenheit or Celsius?"; // If user does not specify f or c
ask them
if (temp == f)
Where is f assigned?
{
c = FahrenheittoCel sius;
Here you attempt to assign a function to a char, which is a very strange
thing to do! Did you mean to write

c = FahrenheittoCel sius( temp );
cout << f " degrees Fahrenheit is" c " degrees celsius ";
cout << " Continue? ";
missing }
else
missing {
f = CelsiustoFahren heit;
cout << c " degrees Celsius is " f " degrees Fahrenheit ";
cout << " Continue? ";

}
return temp;
}
double FahrenheitoCels ius (double f)
{
return (f - 32) * (5.0/9);
}
double CelsiustoFahren heit (double c)
{
return (9/5.0 * c) + 32;
}

--
Ian Collins.
Mar 31 '07 #2
<As******@msn.c omwrote in message
news:11******** **************@ y66g2000hsf.goo glegroups.com.. .
Hey Everyone~
I'm still a C++ Rookie so please bear with me on this.
I'm doing a temperature conversion program with prototype functions.
Basicly, I was wondering if some of you would take a look at my code
and critique it for me. I'm mostly concerned with how prototype
functions work and if I designed them correctly in my code.
Your participation would be greatly appreciated!

My Code:

#include "stdafx.h"
#include <iostream>
#include <cstdlib>

double FahrenheittoCel sius(double)= 0.0; //Function Prototype
This is not a pure virtual method. It is a function prototype. It should
look like this:
double FarenheitToCelc ius(double);
double CelsiustoFahren heit(double)= 0.0;
double CelciusToFarenh eit(double);
int _tmain(int argc, _TCHAR* argv[])
This is non standard, becuase you did a windows project and are using
standard headers. That is frowned upon in this newsgroup.

project->properties->c/C++->Precompiled Headers
Change Create/Use Precompiled Header to Not Using Precompiled Header.
Then remove stdafx.cpp from your project. Change your main to:
int main()
{
double tempFahrenheit, tempCelsius;
Better to call these Farenheit and Celcius so you don't confuse yourself.

double Farenheit, Celcious;
double temp;
char f;
char c;
These are not the characters 'f' and 'c' but variables of type char that are
called f and c. You can get rid of these.
cout << " Enter the Temperature: "; //Prompt user to enter temp
cin >temp;
cout << " Fahrenheit or Celsius?"; // If user does not specify f or c
ask them
if (temp == f)
You are asking if temp == f. temp is a double. f is a char. I think you
actually wanted to ask the user but forgot to do it.

char torf;
cin >torf;

if ( torf == 'f' )
{
c = FahrenheittoCel sius;
You need to call a function there. Your prototype is:
double FarenheitToCelc ius(double);
so it would be:

Celcius = FarenheitToCelc ius( temp );
cout << f " degrees Fahrenheit is" c " degrees celsius ";
That ain't going to work. It should be:
cout << Farenheit << " degrees Fahrenheit is" << Celcius << " degrees
celsius ";
cout << " Continue? ";

else
f = CelsiustoFahren heit;
cout << c " degrees Celsius is " f " degrees Fahrenheit ";
See comments for converting from FarenheitToCelc ius and fix this to reflect.
cout << " Continue? ";

}
return temp;
}
double FahrenheitoCels ius (double f)
{
return (f - 32) * (5.0/9);
}
double CelsiustoFahren heit (double c)
{
return (9/5.0 * c) + 32;
}
Okay, try all tis, then actually try to compile it. You'll get compilation
errors variables not found, etc.. Fix the errors. If you still get stuck
post again with your new fixed code.
Apr 1 '07 #3
You are a lot of help! And...I feel dumb about some of the mistakes
that I made. I have no clue where I came up with that. But, I'm still
learning. So, here we go. I am having a problem debugging. It won't
execute the program. So, I can build and see the errors that come up
but it won't execute. What am I missing?

Here is my *NEW* code:

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

double FahrenheittoCel sius(double); //Function Prototype
double CelsiustoFahren heit(double);

int main()
{
double tempFahrenheit, tempCelsius;
double temp;
char corf;
cout << " Enter the Temperature: "; //Prompt user to enter temp
cin >corf;

if (corf == 'f')
{
tempCelsius = FahrenheittoCel sius( temp );
cout << tempFahrenheit << " degrees Fahrenheit is" << tempCelsius
<< " degrees celsius ";
cout << " Continue? ";
if
tempFahrenheit = CelsiustoFahren heit(temp);
cout << tempCelsius << " degrees Celsius is " << tempFahrenheit << "
degrees Fahrenheit";
cout << " Continue? ";
}

return 0;
}
double FahrenheittoCel sius (double f)
{
return (f - 32) * (5.0/9);
}
double CelsiustoFahren heit (double c)
{
return (9/5.0 * c) + 32;
}


Apr 1 '07 #4
As******@msn.co m wrote:
You are a lot of help! And...I feel dumb about some of the mistakes
that I made. I have no clue where I came up with that. But, I'm still
learning. So, here we go. I am having a problem debugging. It won't
execute the program. So, I can build and see the errors that come up
but it won't execute. What am I missing?

Here is my *NEW* code:
Which *still* shouldn't compile!
// Temperature.cpp : Defines the entry point for the console
application.
//
#include "stdafx.h"
Loose this.
#include <iostream>
#include <cstdlib>
using namespace std;

double FahrenheittoCel sius(double); //Function Prototype
double CelsiustoFahren heit(double);

int main()
{
double tempFahrenheit, tempCelsius;
double temp;
char corf;
cout << " Enter the Temperature: "; //Prompt user to enter temp
cin >corf;

if (corf == 'f')
{
tempCelsius = FahrenheittoCel sius( temp );
cout << tempFahrenheit << " degrees Fahrenheit is" << tempCelsius
<< " degrees celsius ";
cout << " Continue? ";
missing }
if
missing condition and {

--
Ian Collins.
Apr 1 '07 #5
ajk
On 31 Mar 2007 14:22:44 -0700, As******@msn.co m wrote:
>#include "stdafx.h"
#include <iostream>
#include <cstdlib>

double FahrenheittoCel sius(double)= 0.0; //Function Prototype
double CelsiustoFahren heit(double)= 0.0;
double FahrenheittoCel sius(double arg);
double CelsiustoFahren heit(double arg);
>int _tmain(int argc, _TCHAR* argv[])
{
fine, visual studio will set correct character type depending on
unicode or multibyte but for now use char instead, since you are
learning.
> double tempFahrenheit, tempCelsius;
double temp;
char f;
char c;
initialize all variables always, this can help you in the future,
preferably initialize them to some invalid - for your purposes -
value.
>
cout << " Enter the Temperature: "; //Prompt user to enter temp
cin >temp;
ok
> cout << " Fahrenheit or Celsius?"; // If user does not specify f or c
ask them
if (temp == f)
{
temp is of type double and f is of type char - what are you doing
here? looks like you have misstakenly taken then variable name f as
the variable value 'f'.

instead use

const char celsius = 'c';
const char fahrenheit = 'f';
char choice;

cin >choice;
if ( choice == celsius )
{
}

> c = FahrenheittoCel sius;
cout << f " degrees Fahrenheit is" c " degrees celsius ";
cout << " Continue? ";

else
f = CelsiustoFahren heit;
cout << c " degrees Celsius is " f " degrees Fahrenheit ";
cout << " Continue? ";

}
return temp;
}
double FahrenheitoCels ius (double f)
{
return (f - 32) * (5.0/9);
}
double CelsiustoFahren heit (double c)
{
return (9/5.0 * c) + 32;
}
hth/ajk
Apr 1 '07 #6
ajk
On 31 Mar 2007 22:22:48 -0700, As******@msn.co m wrote:
>// Temperature.cpp : Defines the entry point for the console
application.
//
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
using namespace std;

double FahrenheittoCel sius(double); //Function Prototype
double CelsiustoFahren heit(double);

int main()
{
double tempFahrenheit, tempCelsius;
double temp;
char corf;
cout << " Enter the Temperature: "; //Prompt user to enter temp
cin >corf;

if (corf == 'f')
{
tempCelsius = FahrenheittoCel sius( temp );
temp is undefined, it hasn't been set to a value
> cout << tempFahrenheit << " degrees Fahrenheit is" << tempCelsius
<< " degrees celsius ";
cout << " Continue? ";
if
tempFahrenheit = CelsiustoFahren heit(temp);
cout << tempCelsius << " degrees Celsius is " << tempFahrenheit << "
degrees Fahrenheit";
cout << " Continue? ";
}

return 0;
}
double FahrenheittoCel sius (double f)
{
return (f - 32) * (5.0/9);
}
double CelsiustoFahren heit (double c)
{
return (9/5.0 * c) + 32;
}
Apr 1 '07 #7
<As******@msn.c omwrote in message
news:11******** *************@o 5g2000hsb.googl egroups.com...
You are a lot of help! And...I feel dumb about some of the mistakes
that I made. I have no clue where I came up with that. But, I'm still
learning. So, here we go. I am having a problem debugging. It won't
execute the program. So, I can build and see the errors that come up
but it won't execute. What am I missing?

Here is my *NEW* code:

// Temperature.cpp : Defines the entry point for the console
application.
//
#include "stdafx.h"
You don't need to include stdafx.h anymore, that was part of precompiled
headers. Delete that line.
#include <iostream>
#include <cstdlib>
using namespace std;

double FahrenheittoCel sius(double); //Function Prototype
double CelsiustoFahren heit(double);

int main()
{
double tempFahrenheit, tempCelsius;
double temp;
char corf;
cout << " Enter the Temperature: "; //Prompt user to enter temp
cin >corf;
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')
{
tempCelsius = FahrenheittoCel sius( temp );
cout << tempFahrenheit << " degrees Fahrenheit is" << tempCelsius
<< " degrees celsius ";
cout << " Continue? ";
if
Somehow you cut off this line. This should probably be:
if ( corf == 'c' )
{
tempFahrenheit = CelsiustoFahren heit(temp);
cout << tempCelsius << " degrees Celsius is " << tempFahrenheit << "
degrees Fahrenheit";
cout << " Continue? ";
}

return 0;
}
double FahrenheittoCel sius (double f)
{
return (f - 32) * (5.0/9);
}
double CelsiustoFahren heit (double c)
{
return (9/5.0 * c) + 32;
}

Apr 1 '07 #8
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.


Brian
Apr 1 '07 #9
Alright guys....
I agree with you that stdafx.h does not need to be included but it
won't compile without it.
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.

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

double FahrenheittoCel sius(double); //Function Prototype
double CelsiustoFahren heit(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 = FahrenheittoCel sius( temp );
cout << tempFahrenheit << " degrees Fahrenheit is" << tempCelsius
<< " degrees celsius ";
cout << " Continue? ";

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

}

return temp;
}
double FahrenheittoCel sius (double f)
{
return (f - 32) * (5.0/9);
}
double CelsiustoFahren heit (double c)
{
return (9/5.0 * c) + 32;
}



Apr 1 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
7848
by: Derek Ross | last post by:
Hello, Say I have a server that's saving the CPU temperature to 'temperature.js' once a second. The contents of the file is one single line: var temperature = "35.5"; And it changes as the temperature changes.
1
12134
by: deanfamily11 | last post by:
I'm trying to have this program do a simple temperature conversion from Fahrenheit to Celsius. I have confirmed that the other variable is receiving and calculating the conversion, but it is just outputting as "0". Any thoughts? (Code is below) #include <iostream> #include <iomanip> using namespace std;
5
32497
by: anthony | last post by:
One the computer I am programmig I could see the CPU temperature in the BIOS, is there a system DLL in VB.NET that I can call to display the temperature in my software? Thanks!
7
4867
by: alexandre_irrthum | last post by:
Hi there, I am trying to use pyserial to read data from a temperature logger device (T-logger). T-logger is based on the DS1615 temperature recorder chip (Dallas Semiconductor). According to the DS1615 docs, writing to the chip is performed one byte at a time. To read from the chip, one must issue the "read page" command (33h), followed by the two-byte address of the requested page (pages are 32 bytes long). After receiving this, the...
1
3200
by: pollardw | last post by:
i have written a small program to convert Fahrenheit to Celsius and I have a minor problem. Here is the code what do i need to change to get it to work? /* Convert Fahrenheit to Celsius */ #include <iostream> #include <iomanip> #include <string>
4
2341
by: arnuld | last post by:
this is my final code. Can you folks advise some improvements for making this programme better? BTW, i aways compile my programme with this cmmand on Linux: g++ -ansi -pedantic -Wall -Wextra file.cpp // a programme that converts Fahrenheit temperature // to Celcius (in range 0 -
16
4427
by: Brigitte Behrmann | last post by:
I am absolutely stuck with this one. I have to create a temperature conversion calculator that rounds the resulting temperature to the nearest whole number & vice versa. The result must be displayed in a window alert. The given formula is (Fahrenheit_temp - 32) * .55 The only tip/clue I have is to use var tempInCelcius = (document.Converter.fahrenheit.value - 32) * .55; var tempInFahrenheit = (document.Converter.celcius.value * 1.8) + 32 ...
1
3335
by: vbfrnd | last post by:
I need to write a program where I have to convert a temp provided by user to celcius or farenheit and vice versa. I am using text boxes to get the temp in degrees but when I am trying to convert the string to double by CDbl it is showing error while debugging.. I will be thankful If anyone can provide answer..
25
4899
by: kid joe | last post by:
Hi, Is it normal for the temperature of your CPU to increase as time goes on? I've got an Athlon XP 2100+ and I see that the temp is now at 51C where it used to be around 42 to 44. Anyone have any ideas?
0
10031
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9838
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9708
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8709
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7242
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6534
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5140
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3354
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2665
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.