C++ and data references help
Results 1 to 8 of 8

Thread: C++ and data references help

  1. #1

    C++ and data references help

    I was wondering if anyone could help me with this program.

    I am taking a course and have to make this program use references. I attempted at this but as you can see I did not get very far.

    The program should do this:
    1) run first function
    2) get the degrees in celsius from the user
    3) use the celsius data to calculate the farenheit data
    4) then finally cout the answer to the user

    Any Help would be Greatly Appreciated

    #include <iostream.h>

    double celsius_in;

    double celsius_to_fahrenheit(double);
    double get_celsius(double &celsius_in);



    int main()
    {
    double fahrenheit;
    double celsius;

    celsius = get_celsius(double &celsius_in);

    fahrenheit = celsius_to_fahrenheit(celsius);

    cout << celsius << " C = " << fahrenheit << " F\n";

    return 0;
    }

    double celsius_to_fahrenheit(double celsius)
    {
    return(celsius * (9.0/5.0) + 32.0);
    }

    double get_celsius(double celsius_in)
    {


    cout << "Enter the temperature in celsius: ";
    cin >> celsius_in;


    }
    We all search for a reason to live. It is in that reasoning we discover who we truly are.

  2. #2
    Senior Member
    Join Date
    May 2002
    Posts
    344
    well i am not going to give you the exact answer, but from the code you posted, you seem very confused. Anyways, i have to study for a chemistry test tomarrow so i dont have enough time to explain everything today, but tomarrow or as soon as i can, i will come back and explain to you why your code doesnt work and why mine does Also, in this code i typed up, i didnt use references or pointers because then i would just be giving you the answer. Anyways, the code compiles and runs fine i think...i have no clue how to convert celcius to farienheit (cant even spell the damn word), because i is a american and we dont need to know that stuff cause me prezident dunt make me cuase hi dunt fund my educasion Anyways here is the code:

    Code:
    #include <iostream>
    using namespace std;
    
    double celsiusToFahrenheit(double celsius);
    double getCelsius();
    
    
    
    int main()
    {
    	double fahrenheit;
    	double celsius;
    
    	celsius = getCelsius();
    
    	fahrenheit = celsiusToFahrenheit(celsius);
    
    	cout << celsius << " C = " << fahrenheit << " F\n";
    
    	return 0;
    }
    
    double celsiusToFahrenheit(double celsius)
    {
    	return(celsius * (9.0/5.0) + 32.0);
    }
    
    double getCelsius()
    {
    	double celsiusInput;
    	cout << "Please enter the temperature in celsius: ";
    	cin >> celsiusInput;
    	
    	return celsiusInput;//returns what the user just inputed
    }
    Once again i will come back and edit this post or make a new one and explain why i did everything this way and what everything does. Also, i would highly recommend that you go vist your teacher and ask him for help or ask a tutor or whatever....you seem pretty confused.
    Support your right to arm bears.


    ^^This was the first video game which i played on an old win3.1 box

  3. #3
    Suprisingly you almost exactly reproduced the original code.

    I have attempted to ask my teacher why or even how. What he does is simply for a lesson get up in the middle of class and read straight out of the book and assign projects. I tried working on this for at least 45 minutes yesterday. The problem was I had to using calling (i think its called) by reference. I have to use '&' infront of the variable im passing to the function to ask the user for the input. The problem was I could not figure out what part of the program was continously giving me errors. So After I would solve one of them, I would get another 4 ridiculous errors.

    Maybe I have to go on my own, because this just seems to get more confusing to me. The code I follow from the material is always incomplete, and I have yet to see a fully functional program even use this. All I got was the void function which sends the variable back to the main function, so I have no idea how to declare these, call them, or pretty much do anything with them. Any help would be appreciated.
    We all search for a reason to live. It is in that reasoning we discover who we truly are.

  4. #4
    Senior Member
    Join Date
    May 2002
    Posts
    344
    Suprisingly you almost exactly reproduced the original code.
    No, not exactly, my code actually compiled and the functions did what they were suppose to do. I personally think that the assignment is stupid because there is no need to pass a reference variable into one of your functions. I mean sure you could do it, but it isnt needed and i think it makes the program a lot more confusing. From the code you wrote above, it seems like you dont even really know how to call functions or how to make them return anything, because originally your get_celcius function didnt return anything. If you dont understand how functions work, then references will be hard. Do you know what the '&' does? you gotta know a little bit about pointers to understand what it actually does and how to use it in one of your programs....if you want i can give you a link to one of my tutorials that i wrote about pointers or you can search on AO for Intro to pointers and i am sure you will find it.

    All I got was the void function which sends the variable back to the main function,
    lol you didnt even get that! void does the oppisite, it makes it so that your functions can return anything. int get_celcius tells the function to return an integer, so lets say if the user inputted 3.4 as the value for celcius_in, then it would return 3. double get_celcius tells the function to return a double, meaning up to 15 digits (dont quote me on that, but a lot of digits, maybe 17 rusty memory), so if the user set celcius_in to 3.14567899324, it would return 3.14567899324. I would defiently suggest re reading the chapter and if you teacher is a dumbshit, drop the class...
    Support your right to arm bears.


    ^^This was the first video game which i played on an old win3.1 box

  5. #5
    Sure, Any Help would be appreciated. I know my teacher is probably one of the worst in the world. A link would be greatly appreciated.
    We all search for a reason to live. It is in that reasoning we discover who we truly are.

  6. #6
    Jaded Network Admin nebulus200's Avatar
    Join Date
    Jun 2002
    Posts
    1,356
    Hey White_Eskimo, pretty good answer, but on a purely style based suggestion, I don't think I would have the getCelsius ask for input, but rather just return the value. From what I have seen, usually the input would either be handled in main, or if you just really wanted to, handled in a setCelsius function (the reason for this is once they go onto OOP, properly using get and set functions will make their code easier for others to use).

    I agree though with the fact that passing the variable by reference isn't really necessary. Pjd9000, the easiest way for now, for you to think about passing a variable by reference, is this way:

    If you call a function, passing a variable to it, the variable that you pass into the function will not change, it will hold its value. So celsiusToFarenheit(x) when used in a normal fashion, the value of x would not change and you would have to setup another variable to take on the value returned by the function, ie, y = celsiusToFarenheit(x) (since you return a double, y will then hold the double calculated by the function).

    What is different about passing the variable by reference? The variable that you pass in the function, can be changed in the function you called. So if you called celsiusToFarenheit(x) and passed x as a reference, if you modified the value of x in celsiusToFarenheit(), when the program returns to main(), the value you set for x in celsiusToFarenheit will now be the value of x in main().

    So, passing variables normally means that the variables passed can not be changed outside of the scope of the function you called. Passing the variables by reference means they can (and what you essentially are doing is passing a pointer, but hey, you probably haven't gotten there yet ).

    To illustrate, compile and run this: (It is very redundant and a oversolving of your problem, but it clearly illustrates the difference). The first function when it is called, the output results in two distinct values. The second function (the one that takes arguments passsed as reference), the output results in the same value...


    Code:
    #include <iostream.h>
    using namespace std;
    
    double celsiusToFahrenheit(double celsius);
    double celsiusToFahrenheit2(double &);
    
    
    
    int main(int argc, char **argv)
    {
    double temperature;
    double tempFarenheit;
    
            cout << "Please enter the temperature in celsius: \n";
            cin >> temperature;
            cout << "\nCalling celsiusToFahrenheit (normal)...\n";
    
            tempFarenheit = celsiusToFahrenheit(temperature);
    
            cout << "tempFarenheit " << tempFarenheit << " and temperature " << temperature << "\n";
    
            cout << "\nCalling celsiusToFahrenheit2 (reference)...\n";
    
            tempFarenheit = celsiusToFahrenheit2(temperature);
    
            cout << "tempFarenheit " << tempFarenheit << " and temperature " << temperature << "\n";
    
            return 0;
    }
    
    double celsiusToFahrenheit(double celsius)
    {
            return(celsius * (9.0/5.0) + 32.0);
    }
    
    double celsiusToFahrenheit2(double &temp)
    {
            temp = temp * (9.0/5.0) + 32.0;
            return temp;
    }
    ./a.out
    Please enter the temperature in celsius:
    32

    Calling celsiusToFahrenheit normally...
    tempFarenheit 89.6 and temperature 32

    Calling celsiusToFahrenheit passing reference...
    tempFarenheit 89.6 and temperature 89.6
    Make sense?

    /nebulus
    There is only one constant, one universal, it is the only real truth: causality. Action. Reaction. Cause and effect...There is no escape from it, we are forever slaves to it. Our only hope, our only peace is to understand it, to understand the 'why'. 'Why' is what separates us from them, you from me. 'Why' is the only real social power, without it you are powerless.

    (Merovingian - Matrix Reloaded)

  7. #7
    Thanks for the help, I appreciate it.
    We all search for a reason to live. It is in that reasoning we discover who we truly are.

  8. #8
    Senior Member
    Join Date
    May 2002
    Posts
    344
    Hey White_Eskimo, pretty good answer, but on a purely style based suggestion, I don't think I would have the getCelsius ask for input, but rather just return the value. From what I have seen, usually the input would either be handled in main, or if you just really wanted to, handled in a setCelsius function (the reason for this is once they go onto OOP, properly using get and set functions will make their code easier for others to use).
    Sure, but i was on a time crunch there and i just wanted to write the program and get no errors so i just changed a couple things around in his original code
    Support your right to arm bears.


    ^^This was the first video game which i played on an old win3.1 box

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •