Thread: Switches and functions

  1. #16
    Registered User
    Join Date
    Jun 2005
    Posts
    75
    Quote Originally Posted by Epo
    Remember, when using pointers:
    Code:
    void MyFunc(char *MyChar)
    {
    //Notice the *MyChar, which means that MyChar is a pointer (*) to a character.
    //Not char&...
    }
    
    //Then, when calling the function
    MyFunc(&operation);
    //The "&" symbolizes that you are passing the address of operation as your parameter.
    I don't follow. What do you mean?

  2. #17
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    Quote Originally Posted by MyntiFresh
    I don't follow. What do you mean?
    He is saying that if you use pointer in the function you have to pass the adress of the variable you are passing to the function or it will give you an error, because pointers only store the adress of the value.

  3. #18
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    Your function prototype for menu is:
    Code:
    void menu(char&, int&, int&, int&, int&); //I don't know what this does...
    However, it is:
    Code:
    void menu(char*, int*, int*, int*, int*); //Here you are taking pointers
    That means you are taking a pointer as a parameter.

    Then, when you call the menu function:
    Code:
    menu(operation, numeratorOne, denominatorOne, numeratorTwo, denominatorTwo);
    Must be:
    Code:
    menu(&operation, &numeratorOne, &denominatorOne, &numeratorTwo, &denominatorTwo);
    Which will pass the address of each variable into your function.

    Then, inside your menu() function, you can use:
    Code:
    cin >> (*operation); //Brackets not necessary
    Which will alter the value located at the address which operation is pointing to.

    Code:
    cin >> (operation); //Brackets not necessary
    Will change the address which operation points to, which will not only lead to crazy things happening probably, but definitely not your desired outcome.

    Pointers are a tricky topic. Give it a read over. www.eternallyconfuzzled.com (Prelude's website) has a great tutorial on pointers, as does the FAQ here on the boards. If you haven't been using pointers long, then they're a great resource. If you have, perhaps a quick refresher could help out.
    Pentium 4 - 2.0GHz, 512MB RAM
    NVIDIA GeForce4 MX 440
    WinXP
    Visual Studio .Net 2003
    DX9 October 2004 Update (R.I.P. VC++ 6.0 Compatability)

  4. #19
    Registered User
    Join Date
    Jun 2005
    Posts
    75
    I haven't learned pointers yet, so I don't know what to do. Everything in the program runs fine... I just can't figure out now that I have the menu looping, how do I make it so that the program exits if the user inputs 'x' or 'X'???

  5. #20
    *this
    Join Date
    Mar 2005
    Posts
    498
    Have the x case either exit(0); or return 0;

    Just a quick suggestion.

  6. #21
    Registered User
    Join Date
    Jun 2005
    Posts
    75
    Quote Originally Posted by JoshR
    Have the x case either exit(0); or return 0;

    Just a quick suggestion.
    Thanks... I've done that and it works.... but I don't want the program to continue getting numbers. What the program is doing now is when i input 'x' it still asks me for the numbers, then once I've entered them the program terminates. Suggestions?

  7. #22
    *this
    Join Date
    Mar 2005
    Posts
    498
    That will exit the program and it won't continue.

    Hmm so I suspect the problem you speak of resides in your menu function...Have the menu function check for x, if x is present then quit.

  8. #23
    Registered User
    Join Date
    Jun 2005
    Posts
    75
    Thanks. I got it now!

  9. #24
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Epo
    Your function prototype for menu is:
    Code:
    void menu(char&, int&, int&, int&, int&); //I don't know what this does...
    These are what're known as references. It's C++'s attempt at a "better" pointer. Personally I don't like them. At any rate, this is how it works:
    Code:
    void foo( int &bar )
    {
        bar = 10;
    }
    
    int main ( )
    {
        int baz = 5;
    
        foo( baz );
    
        ...baz now equals 10...
    
        return 0;
    }
    It's in effect as if you had used a pointer, without the "hassle" of indirection. There's a few other points, such as, you can never have a null reference. Thus, if you have something that could potentially be null, you can't use a reference for it.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #25
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    Oh...well...pretty much everything I've suggested is wrong then...my bad...and I was wondering how the rest of the program was working...I guess that explains it.

    Personally I don't like them.
    I think I'm gonna be agreeing with you on this. It seems like an attempt to re-work something that already works. I dunno, maybe I'm just too used to pointers.

    I've never seen references before (I just assumed "by reference" meant pointers), thanks for the info Quzah.
    Pentium 4 - 2.0GHz, 512MB RAM
    NVIDIA GeForce4 MX 440
    WinXP
    Visual Studio .Net 2003
    DX9 October 2004 Update (R.I.P. VC++ 6.0 Compatability)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. benchmarking?
    By cs32 in forum C Programming
    Replies: 5
    Last Post: 02-14-2008, 07:37 AM
  2. Mutiple switches
    By quiet_forever in forum C++ Programming
    Replies: 2
    Last Post: 04-26-2007, 03:19 PM
  3. Unicode file I/O functions
    By MiamiCuse in forum C Programming
    Replies: 8
    Last Post: 11-04-2005, 01:09 PM
  4. error LNK2001: unresolved external symbol
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 07-12-2002, 08:45 PM