Newbie needs help

This is a discussion on Newbie needs help within the C++ Programming forums, part of the General Programming Boards category; Is there a way to keep an output in fraction form in stead of decimal form? and What does pass ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    18

    Newbie needs help

    Is there a way to keep an output in fraction form in stead of decimal form? and What does pass by reference mean? Also when you are using functions, when do you put void in front of the function and when do you not do that?

    Also is there a way to let someone cin>> a formula?
    Last edited by john5754; 12-12-2008 at 09:31 AM.

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    1) Yes. If I understand correctly you want something like
    Code:
    struct fraction
    {
        int big;    //dunno the actual name
        int small; //dunno the actual name
    }
    So instead of having 15.23 you will store 15 to fraction.big and 23 to fraction small. That is all. Now, I don't know if there is a standard method for getting the "small" from a number. You get the "gib" by fraction.big = 15.23, of course. To get the small you will have probably to specify how many digits you want.

    2) Pass by reference means that you pass the actual object/variable to the function, not a copy. So whatever changes you make to the object/variable are reflected to it. Google it for more information. You use the & symbol to do so

    3) If you put void in front of a function it means that it returns nothing. If you want a function lets say to print "hello" on the screen you would do this:
    Code:
    void fun()
    {
        std::cout << "Hello";
    }
    If you want to add two number and return the result:
    Code:
    int add(int a, int b)
    {
        return a + b;
    }
    You can also use return; with a void function but it just terminates the function without returning anything.

    These are kind of basic stuff. I recommend studying a book to learn C++

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Is there a way to keep an output in fraction form in stead of decimal form?
    Not in a trivial way when the result is a floating point (float, double or long double).

    and What does pass by reference mean?
    That you are passing a value as a reference - you are basically passing the ACTUAL location of the variable, rather than passing a by value, which means that a copy of the original variable is made. A reference can be modfiied, but a value copy is just that - a copy of the original variable, so you can modify it to your hearts content, but it will not change the original variable - just like scribbling on a copy of a page of a book will not change the original page in the book.

    The fact that we are not making a copy comes in handy when the object (variable) is large - in this case, copying the location of the variable is a small effort, whilst copying the content (say of a vector with 10000 elements) can be a substantial effort for the computer.

    Also when you are using functions, when do you put void in front of the function and when do you not do that?
    void is used to indicate that a function doesn't return a value (void means "of no value"). All functions have a return type - if the function doesn't have a natural return value (it's not calculating something and there is no other form of "result" to return - e.g. no "success or fail" to return).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    What does pass by reference mean?
    Maybe it will help if I give you an example of why you would use references...

    Usually references are introduced with the Swap(x, y) function. The purpose of that function is to swap the x & y values. So, if x=4 and y=5 before you run the function, you will have x=5 and y=4 after you run the function (example*).

    If you simply pass x and y into a function "normally", the actual variables (x & y) don't really get passed-in. Only the values (4 & 5) get passed in. You can re-use the variable names x & y inside the function (with the values 4 & 5 assigned), but these will be different x & y variables with the same names (but different scope). So, you can go-ahead and swap the x & y values inside the function, and it will work fine inside the function (this requres a temporary variable). But, when you exit the function, your original x & y values will not be changed!

    Your function could return one swapped value, but a function can only return one value. So, you could return the new x-value, or the new y-value, but not both!

    References (and pointers) allow your function to "get to" the original x & y variables, and get-around these limitations.

    Since functions very-frequently need to modify more than one variable, references (and pointers) are very common.


    * The example shows 4 different version of swap(), using references, pointers, templates, and the STL. (Don't worry about the other methods/concepts 'till you study them.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getting to grips with allegro and ms vc++ (newbie)
    By jimjamjahaa in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2005, 06:49 PM
  2. Newbie in problem with looping
    By nrain in forum C Programming
    Replies: 6
    Last Post: 11-05-2005, 11:53 AM
  3. Newbie Programmer
    By Extropian in forum C++ Programming
    Replies: 3
    Last Post: 05-18-2004, 01:17 PM
  4. C++ newbie / linux not so newbie question
    By goldmonkey in forum C++ Programming
    Replies: 7
    Last Post: 12-13-2003, 11:27 AM
  5. Newbie Game Develpoers Unite!
    By Telenosis in forum Game Programming
    Replies: 10
    Last Post: 06-22-2002, 02:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21