Page 5 of 6 FirstFirst 123456 LastLast
Results 61 to 75 of 88

Thread: C++ Help

  1. #61
    Old-Ones Studios Cruise Control's Avatar
    Join Date
    Apr 2005
    Location
    Seattle, WA
    Posts
    3,799

    Default

    I need help, how in blaspheme do I pass data? Can I get an example
    Code:
      float c(float fa, float ce);
      float f(float fa, float ce);
    
     int main()
      {
       float choice; //Stored for intial choice.
    
       cout << "This will convert fahrenheit to celsius.";
       cout << "What do you want to start with, enter your number than print enter.\n";
       cout << " 1. Fahrenheit\n";
       cout << " 2. Celsius\n";
       cin >> choice;
    
       if (choice==1)
        f();
       else
        c();
     return 0;
    }
    
    float c(float fa, float ce)
    {
     float fa; //Fahrenheit variable.
     float ce; //Celsius variable.
    
     cout << "Input your fahrenheit vaulue and press\nENTER.\n";
     cin >> ce;
     fa=1.8*ce+32;
    }
    
    float f(float fa, float ce)
    {
     float fa; //Fahrenheit variable.
     float ce; //Celsius variable.
     cout << "Input your celsius vaulue and press\nENTER.\n";
     cin >> fa;
     ce=(fa-32)/1.8;
    
    }
    Last edited by Cruise Control; 03-23-2006 at 04:46 PM.
    Leave some shards under the belly
    Lay some grease inside my hand
    It's a sentimental jury
    And the makings of a good plan

  2. #62
    Old school, like an old fool. Flying Mullet's Avatar
    Join Date
    Apr 2003
    Location
    Napping in a peach tree.
    Posts
    19,185
    Articles
    6
    Blog Entries
    7
    Contributions
    • Former Administrator
    • Former Cid's Knight
    • Former Senior Site Staff

    Default

    Do you mean use parameters/arguments in your functions?
    Figaro Castle

  3. #63
    Old-Ones Studios Cruise Control's Avatar
    Join Date
    Apr 2005
    Location
    Seattle, WA
    Posts
    3,799

    Default

    I guess *updates code*
    Leave some shards under the belly
    Lay some grease inside my hand
    It's a sentimental jury
    And the makings of a good plan

  4. #64
    Old school, like an old fool. Flying Mullet's Avatar
    Join Date
    Apr 2003
    Location
    Napping in a peach tree.
    Posts
    19,185
    Articles
    6
    Blog Entries
    7
    Contributions
    • Former Administrator
    • Former Cid's Knight
    • Former Senior Site Staff

    Default

    There are two ways of passing arguments to a function, by value or by reference.

    To pass by value, you simply name the variables in the function definition:
    Code:
    void functionByValue(int a, float b)
    {
       // yada yada yada
    }
    What this does is make a copy of the variables passed to the function and uses them locally, hence, by value.

    The other way to pass variables is by reference, which looks like this:
    Code:
    void functionByValue(int a&, float b&)
    {
       // yada yada yada
    }
    What this does is pass a reference (pointer) to the "calling" variable, rather than copy it's value. This means that if you change the value of a to 2 in the function, the original a will be changed to 2. This is useful when you want to return more than one value from a function.

    You can also mix and match by reference and by value:
    Code:
    void functionByValue(int a&, float b)
    {
       // yada yada yada
    }
    In this case a local copy of b will be made and a will point to the "calling" a.

    Hope this helps.

    <b>EDIT:</b> Also, I don't know what book(s) you are using in class, but I used <a href="http://www.amazon.com/gp/product/0672323087/qid=1143132976/sr=2-1/ref=pd_bbs_b_2_1/104-9506222-3431117?s=books&v=glance&n=283155">Object-Oriented Programming in C++ by Robert Lafore</a> and it makes learning C++ a lot easier. Granted, I have the 2nd edition (was the current one when I was in school) and the current one is the 4th edition. But regardless, it's a great book and I highly reccomend it.
    Figaro Castle

  5. #65
    Prinny God Recognized Member Endless's Avatar
    Join Date
    Aug 2000
    Location
    Prinny Moon
    Posts
    2,641
    Contributions
    • Former Cid's Knight

    Default

    Quote Originally Posted by bbomber72000
    Code:
      float c(float fa, float ce);
      float f(float fa, float ce);
    
     int main()
      {
       float choice; //Stored for intial choice.
    
       cout << "This will convert fahrenheit to celsius.";
       cout << "What do you want to start with, enter your number than print enter.\n";
       cout << " 1. Fahrenheit\n";
       cout << " 2. Celsius\n";
       cin >> choice;
    
       if (choice==1)
        f();
       else
        c();
     return 0;
    }
    
    float c(float fa, float ce)
    {
     float fa; //Fahrenheit variable.
     float ce; //Celsius variable.
    
     cout << "Input your fahrenheit vaulue and press\nENTER.\n";
     cin >> ce;
     fa=1.8*ce+32;
    }
    
    float f(float fa, float ce)
    {
     float fa; //Fahrenheit variable.
     float ce; //Celsius variable.
     cout << "Input your celsius vaulue and press\nENTER.\n";
     cin >> fa;
     ce=(fa-32)/1.8;
    
    }
    Your code makes baby Jesus cry. And I still don't get why some teachers make students use C++ for C programming. Srsly. There's no object in your code, ever.
    Anyway.
    Why is f defined twice?
    Why is c defined twice?
    Why did you define f and c to use two parameters, yet redefined fa and
    ce inside?
    Why did you define f and c to use two parameters and never passed any?
    Why define a function with two arguments that only uses one and should return one that you don't even use?
    Did I mention the awful naming of variables? Do you really need both fa and ce?

    And then there is Death

  6. #66
    i n v i s i b l e Tech Admin o_O's Avatar
    Join Date
    Jun 2001
    Location
    New Zealand
    Posts
    2,957
    Blog Entries
    1

    FFXIV Character

    Humphrey Squibbles (Sargatanas)

    Default

    Code:
    int main() {
        float choice;
        cout << "This will convert fahrenheit to celsius.";
        cout << "What do you want to start with, enter your number than print enter.\n";
        cout << " 1. Fahrenheit\n";
        cout << " 2. Celsius\n";
        cin >> choice;
        
        if (choice == 1) {
            float fTemp;
            cout << "Input temperature in fahrenheit: ";
            cin >> fTemp;
            cout << toCelcius(fTemp);
        else {
            float cTemp;
            cout << "Input temperature in celcius: ";
            cin >> cTemp;
            cout << toFahrenheit(cTemp);
        }
    }
    
    float toCelcius(float fTemp)
    {
        return (fTemp-32)/1.8
    }
    
    float toFahrenheit(float cTemp);
    {
        return (1.8*cTemp)+32;
    }
    This could be one way to do it by passing variables. You need to define the variable outside of the function in order to pass it too the function.
    Last edited by o_O; 03-25-2006 at 09:58 PM.

  7. #67
    Prinny God Recognized Member Endless's Avatar
    Join Date
    Aug 2000
    Location
    Prinny Moon
    Posts
    2,641
    Contributions
    • Former Cid's Knight

    Default

    You also need to define functions <i>before</i> calling them.
    Also, one function is enough to do the conversion.

    And then there is Death

  8. #68
    i n v i s i b l e Tech Admin o_O's Avatar
    Join Date
    Jun 2001
    Location
    New Zealand
    Posts
    2,957
    Blog Entries
    1

    FFXIV Character

    Humphrey Squibbles (Sargatanas)

    Default

    Shh, I'm a Java programmer!
    >_>
    <_<
    I was trying to preserve the original layout too.

    I guess this would be a better program then.

    Code:
    float convert(float temp, bool toFahrenheit);
    
    int main() {
        int choice;
        cout << "Choose your conversion:\n1. To fahrenheit\n2. To celcius";
        cin >> choice;
        bool toFahrenheit = (choice == 1);
        cout << "Enter your temperature: ";
        cin >> temp;
        cout << convert(temp, toFahrenheit);
    }
    
    float convert(float temp, bool toFahrenheit)
    {
        if (toFahrenheit) return (1.8*temp)+32;
        else return (temp-32)/1.8;
    }

  9. #69
    Prinny God Recognized Member Endless's Avatar
    Join Date
    Aug 2000
    Location
    Prinny Moon
    Posts
    2,641
    Contributions
    • Former Cid's Knight

    Default

    Close. It won't compile because you forgot to declare a variable
    Why make a boolean when you can pass choice directly? Also, you need a couple \n here and there so that it displays better.
    I mean:
    <font color=lime>lopez@pc9-142</font> <font color=yellow>aks $></font> ./conv2
    Choose your conversion:
    1. To fahrenheit
    2. To celcius1
    Enter your temperature: 12
    53.6<font color=lime>lopez@pc9-142</font> <font color=yellow>aks $></font> ./conv2
    Choose your conversion:
    1. To fahrenheit
    2. To celcius2
    Enter your temperature: 53.6
    12<font color=lime>lopez@pc9-142</font> <font color=yellow>aks $></font>

    And then there is Death

  10. #70
    i n v i s i b l e Tech Admin o_O's Avatar
    Join Date
    Jun 2001
    Location
    New Zealand
    Posts
    2,957
    Blog Entries
    1

    FFXIV Character

    Humphrey Squibbles (Sargatanas)

    Default

    xD
    So I did. I guess that's what I get for not testing it before I post it.

  11. #71
    Old-Ones Studios Cruise Control's Avatar
    Join Date
    Apr 2005
    Location
    Seattle, WA
    Posts
    3,799

    Default

    Alrightm I finished but it looks totally different. I need to write a function that will round any given number to the 10th decimal place.

    Code:
    //Chris Taylor
    //March 15th, 2006
    //Intermediate C++, Terminal 6.
    //Cube Thingy
    
    #include <iostream.h>
    #include <fstream.h>
    #include <conio.h>
    
      //Prototype statements
      void pdescription();
      int choice (int choose);
      float c(float fa);
      float f(float ce);
      void output(float fa, float ce);
    
     int main()
      {
       //Variable declaration
       float fa=32; //Fahrenheit variable.
       float ce=0; //Celsius variable.
       int choose=0; //Stored for intial choice.
    
       pdescription();//Calls the function "description"
       choice (choose);//Calls the function "choice"
    
       if (choose==0)//Determines what is being converted to what.
       {
        f(ce);
       }
    
       else
       {
        c(fa);
       }
    
       output(fa, ce);
    
       getch();
       return 0;
      }
    
    void pdescription()//Prints out program description
    {
       cout << "What do you want to start with, enter your number than print enter.\n";
    }
    
    
    int choice(int choose)//Allows user to choose what is being converted to.
    {
       cout << " 0. Fahrenheit\n";
       cout << " 1. Celsius\n";
       cin >> choose;
       return choose;
    }
    
    float c(float fa)//For converting into fahrenheit.
    {
     float ce;
     cout << "Input your celsius vaulue and press\nENTER.\n";
     cin >> ce;
     fa=(1.8*ce)+32; //Formula to convert c to f.
     return fa;
    }
    
    float f(float ce)//For converting into celsius.
    {
     float fa;
     cout << "Input your fahrenheit vaulue and press\nENTER.\n";
     cin >> fa;
     ce=(fa-32)*(5.0/9.0);//Formula to convert f to c.
     return ce;
    }
    
    void output(float fa, float ce)//Prints fahrenheit and celsius value to the screen.
    {
     clrscr();
     cout << "Fahrenheit value is: " << fa << endl;
     cout << "Celsius value is: " << ce;
    }
    Leave some shards under the belly
    Lay some grease inside my hand
    It's a sentimental jury
    And the makings of a good plan

  12. #72
    Prinny God Recognized Member Endless's Avatar
    Join Date
    Aug 2000
    Location
    Prinny Moon
    Posts
    2,641
    Contributions
    • Former Cid's Knight

    Default

    I prefer my version that's half as long and does the same xD

    And for displaying a fixed number of decimals:
    #include &lt;iomanip>

    Then just before the cout (only the first time):
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(number_of_decimals_here);


    Or for rounding (warning, this comes from the web, in case your teacher checks):
    Code:
    float Round(float Value, int NumPlaces)
       {
       int k, Temp;
       float Factor;
       
       Factor = 1;
       for (k = 0; k < NumPlaces; k++)
          Factor = Factor * 10;
          
       Temp = Value * Factor + 0.5;
       return Temp / Factor;
       }

    And then there is Death

  13. #73
    ORANGE Dr Unne's Avatar
    Join Date
    Dec 1999
    Posts
    7,394
    Articles
    1
    Contributions
    • Former Administrator
    • Former Developer
    • Former Tech Admin

    Default

    Quote Originally Posted by bbomber72000
    Alrightm I finished but it looks totally different. I need to write a function that will round any given number to the 10th decimal place.
    Is <strong>printf("%.10f", var);</strong> cheating?

  14. #74
    Old-Ones Studios Cruise Control's Avatar
    Join Date
    Apr 2005
    Location
    Seattle, WA
    Posts
    3,799

    Default

    My code is lengthy due to the program description.

    Code:
    //Chris Taylor
    //March 15th, 2006
    //Intermediate C++, Terminal 6.
    //Cube Thingy
    
    #include <iostream.h>
    #include <fstream.h>
    #include <conio.h>
    
    
      //Prototype dstatements
      void pdescription();
      int choice (int choose);
      float c(float fa);
      float f(float ce);
      void output(float fa, float ce);
    
     int main()
      {
       //Variable declaration
       float fa; //Fahrenheit variable.
       float ce; //Celsius variable.
       int choose=0; //Stored for intial choice.
    
       pdescription();//Calls the function "description"
       choice (choose);//Calls the function "choice"
    
       if (choose==0)//Determines what is being converted to what.
       {
        f(ce);
       }
    
       else
       {
        c(fa);
       }
    
       output(fa, ce);
    
       getch();
       return 0;
      }
    
    void pdescription()//Prints out program description
    {
       cout << "This program will print out two random value for triangle legs, determine the hypotenuse, and quiz the user on the answer.\n";
    }
    
    
    int choice(int choose)//Allows user to choose what is being converted to.
    {
       cout << " 0. Fahrenheit\n";
       cout << " 1. Celsius\n";
       cin >> choose;
       return choose;
    }
    
    float c(float fa)//For converting into fahrenheit.
    {
     float ce=0;
     cout << "Input your celsius vaulue and press\nENTER.\n";
     cin >> ce;
     fa=(1.8*ce)+32; //Formula to convert c to f.
     return fa;
    }
    
    float f(float ce)//For converting into celsius.
    {
     float fa=32;
     cout << "Input your fahrenheit vaulue and press\nENTER.\n";
     cin >> fa;
     ce=(fa-32)*(5.0/9.0);//Formula to convert f to c.
     return ce;
    }
    
    void output(float fa, float ce)//Prints fahrenheit and celsius value to the screen.
    {
     clrscr();
     cout << "Fahrenheit value is: " << fa << endl;
     cout << "Celsius value is: " << ce;
    
    }
    Last edited by Cruise Control; 03-28-2006 at 04:21 PM.
    Leave some shards under the belly
    Lay some grease inside my hand
    It's a sentimental jury
    And the makings of a good plan

  15. #75
    Prinny God Recognized Member Endless's Avatar
    Join Date
    Aug 2000
    Location
    Prinny Moon
    Posts
    2,641
    Contributions
    • Former Cid's Knight

    Default

    Did you actually test your code?

    And then there is Death

Posting Permissions

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