Public Group

# hum

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

## Recommended Posts

// temps2.cpp
#include <iostream>
using namespace std;

void do_intro();
void get_temp_data (char scl, int dgrs);
void change_C2F(int C);
void change_F2C(int F);

int main()
{
char scale; // Cels or Fahr
int degrees;
do_intro();
get_temp_data (scale, degrees);
if (scale=='c' || scale=='C')
change_C2F(degrees);
else
change_F2C(degrees);
return 0;
}

void do_intro()
{
cout << "CELSIUS <---> FAHRENHEIT CONVERTER\n";
cout << "THE TWO CONVERSION FORMULAS ARE:\n";
cout << "   F = 1.8 * C + 32" << endl;
cout << "   C + (5.0/9) * (F - 32)" << endl;
}

void get_temp_data (char scl, int dgrs)
{
cout << "Is input Celsius or Fahrenheit? (F/C): ";
cin >> scl;
cout << "Enter number of degrees: ";
cin >> dgrs;
}

void change_C2F(int C)
{
// stub
cout << "In stub for change_C2F" << endl;
}

void change_F2C(int F)
{
int C;
C = int((5./9.) * (F-32));
cout << F << " F = " << C << " C" << endl;
}


Hum.. this is a code from a book example, but the book told me to remove the & from the defenition of the get_temp_data function defenittion and decleration; void get_temp_data (char& scl, int& dgrs); The program worked fine before I removed them.. Ok.. and this is the output I get.. whatever degree I put in it gives me "8 F =" .. What I dont get is where 8 is assigned to F? D:\c++\book code\ch05>temps2 CELSIUS <---> FAHRENHEIT CONVERTER THE TWO CONVERSION FORMULAS ARE: F = 1.8 * C + 32 C + (5.0/9) * (F - 32) Is input Celsius or Fahrenheit? (F/C): f Enter number of degrees: 30 8 F = -13 C

##### Share on other sites
I know how to fix it ofcourse.. just put back the &'s.. but the book told me to remove it and see what happens..

##### Share on other sites
8 is just garbage that was randomly in memory.

F is never assigned any value with the & missing in the function definition. A COPY of F is assigned the value, but it once funciton ends the copy is destroyed and the original is unaltered.

Could be wrong of course, i breifly looked over the code :)

Im sure your book will begin to talk about pass by value/ pass by reference soon and this is probably the intro.

What book is this anyway, because that is some really bad code..

##### Share on other sites
its called using c++ - an introduction to programming.. its from 2002.. maybe I should get a new one... I dont have mutch time to use it these days because of some exames.. so I have time to wait for a new one..

Anyone used or have any experience with it? its from brooks/cole and is writen by Hennerfeld, Baker and Buchard..

the example is an introduction to functions..

##### Share on other sites
Id recommend

Accelerated C++: Practical Programming by Example (Paperback)
by Andrew Koenig, Barbara E. Moo

http://www.amazon.com/gp/product/020170353X/sr=8-1/qid=1147067864/ref=pd_bbs_1/104-8071661-3590350?%5Fencoding=UTF8

##### Share on other sites
char scale;
int degrees;

Both are uninstantiated, you assign no value to them.

void get_temp_data (char scl, int dgrs)
{
cout << "Is input Celsius or Fahrenheit? (F/C): ";
cin >> scl;
cout << "Enter number of degrees: ";
cin >> dgrs;
}

Here is where they should receive values, but scl and dgrs are local variables to void get_temp_data(). Once this function completes it's local variables lose scope and are removed from memory. degrees and scale are not set.

if (scale=='c' || scale=='C')
change_C2F(degrees);
else
change_F2C(degrees);
return 0;

Without the '&' (ByRef) scale will never be 'c' or 'C' because scale == NULL. Hence the reason you always get the change_F2C call. As was previously mentioned 8 was just junk memory.

The reason it works with the '&' (ByRef) is because now you're referencing a spot in memory. You're basically telling VC++ that scale resides in the same spot in memory as your function variable scl, and degrees resides in the same spot as dgrs. When you change scl and dgrs you're actually manipulating your
memory reference and not the variables themself( make sense sorta? ) Hope this helps some.

[Edited by - senelebe on May 8, 2006 8:27:13 AM]

• 15
• 9
• 13
• 41
• 15