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.

Pass value from one function to another

In this program, I have to ask the user for an employee, then the program will check to see if the file for that employee exist, if it doesnt then it will automatically create the file.

ReadNew function reads the file....check to see if it exist

CreateNew function creates a new file.

In my code I have no problem with the first part of reading file.. and my createnew function works in other programs where I am asking for input of file name to create the file name. However in this code I cannot figure how to automatically pass the input filename from the ReadNew function to the CreateNew function. I can't ask the user to enter the name a second time, so I have to pass the input filename into both functions

Re: Pass value from one function to another

If you want to request the name of the file in ReadNew, then the name can either be passed back to the calling routine as a return value from the function, or as a reference parameter to the function. Alternatively, but not recommended, name could be another global variable.

A couple of points to note. For c++, the include names are cstring and cstdio, not string.h and stdio.h. Also, it is not considered good practice to have your variables defined as global. Good practice is to define them near to where they are first used with as small a scope as possible. Rather than have the file functions set a global variable, it would be more usual for these functions to return a type bool indicating whether success or not.

eg

Code:

bool ReadNew(ifstream& FileGet, string& name)

All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

As 2kaud mentioned, using global variables is bad practice. Especially because you are using them to pass values between functions, which will prevent you from learning the proper way to pass values between functions and how to design your program. IMO it's also bad design to mix input/output with application logic. It would be better to split these things into separate functions. So, the outline of your program could be something like