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);
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);
Are talking about 'string' or an array of chars ?
// Gliptic
im talking about string
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
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?
sorry I don't know how to format the code so it's indented in the forum
a bit unrelated to this, but what compiler are you using?
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.
I'm using MSVC++ 6.0Originally posted by Hoxu
a bit unrelated to this, but what compiler are you using?
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.