If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Re: Passing String to a Function

Hey, there. For starters, always make sure that your function prototype and the function definition have the same data type in their parameters (i.e. both are char*-types.) You had the right idea in the prototype, but in the function definition things got a little scrambled.

For starters, when you make the ofstream "myFile", the syntax should be:

ofstream myFile;

not

ofstream.myFile;

Also, remember that the myFile.open() function takes a pointer to the string as a parameter. It looks like you were trying to pass the function the whole array, but that's overkill and it's improper syntax. Since your c-string is named "fn", the pointer to the c-string is simply "fn", so your myfilemaker() function should read:

Also, just to get in the habit of good programmin', try to make your main() function return a value of 0, that's what generally symbolizes a successful execution. If you want to save some system resources, as will, use cin.get() instead of pausing the system, it's substantially less memory-intensive and performs the same function.

Re: Passing String to a Function

These two are not the same. I hope you can spot the difference.

Code:

void myfilemaker(char *fn);
void myfilemaker(char &fn[0])

I don't want to reiterate what was already said. Keep in mind that C-like strings, i.e. char*, are only a sequence of characters that ends with a null-termination character, or 0 (or '\0'). Though they are actually arrays or characters, they are called null-terminated strings. To work with such strings, you pass the address of the first character in the array. A function that takes a null-terminated string as input, actually takes a pointer to char*, because that is the type for the address of the first character in the array.