Thread: Passing string to function

  1. #1
    Unregistered
    Guest

    Passing string to function

    When you pass a string to a function, do you need to pass by reference or does it already do that automatically like arrays? I.E.

    sendString(string1);

    or:

    sendString(&string1);

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    164
    Are talking about 'string' or an array of chars ?
    // Gliptic

  3. #3
    Unregistered
    Guest
    im talking about string

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    164
    String is a class so if your function has declared the string parameter as a pointer, you have to pass it using '&string1'. But if it's declared as a reference you just have to pass it using 'string1'. Arrays aren't passed automatically as pointers, they are pointers.
    // Gliptic

  5. #5
    Unregistered
    Guest
    thanx, but I still can't get my program to work if you could just look really quick at this function:

    void formatDollar(string currency)
    {
    int dp; // integer to store decimal position
    dp = currency.find('.'); // find decimal position
    if(dp > 3)
    {
    for(int x = dp - 3; x > 0; x -= 3)
    currency.insert(x, ",");
    }

    currency.insert(0, "$");
    }

    I can't get the function to insert a comma or dollar sign in the string. do u know what might be causing it?

  6. #6
    Unregistered
    Guest
    sorry I don't know how to format the code so it's indented in the forum

  7. #7
    Registered User Hoxu's Avatar
    Join Date
    Nov 2001
    Posts
    25
    a bit unrelated to this, but what compiler are you using?

  8. #8
    Registered User
    Join Date
    Aug 2001
    Posts
    155
    how do you decide where to put commas in a currency value if you don't use the computer? You probably find the decimal point and count backwards, inserting a comma every three digits. This sounds like an ideal use for reverse iterators if you are truly using the STL string class (although I don't have much experience using them). Use find to find() find the pointer pointing to the decimal point in the string. Then use a reverse iterator to go backward in the string three steps and insert a comma every three steps (times through a loop) if you have that many times through the loop. When you get to the end of the reverse iteration insert a dollar sign at the beginning of the loop, or else use the function to find the beginning of the string and insert the dollar sign at that spot. I don't have my STL book here to give you the proper function names, etc., but they should be readily available in yours.

    If you do this without string class and reverse iterators, I would first copy the string to a different string long enough to hold the new string once all the inserting has been done. I would then reverse the string. Then I would find which index has the decimal point and use a loop to move to the right by three. Once there I would shift everything else to the right of where I am by one to create an empty spot to insert the comma. To shift to the right by one start at the end of the string and shift the null char to the right by one first. then use a loop to work your way backward until you have the open index where you want to insert the comma.

    When you are all done insert a dollar sign just before the null terminator and re reverse the string.

  9. #9
    Unregistered
    Guest
    Originally posted by Hoxu
    a bit unrelated to this, but what compiler are you using?
    I'm using MSVC++ 6.0

    Thanks for all the help peoples, I finally got it to work, I wasn't passing it right, so when I went to print it out in main() it was still the original string.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing string to function
    By R.Stiltskin in forum C++ Programming
    Replies: 5
    Last Post: 03-04-2009, 12:56 PM
  2. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  4. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM