-
March 30th, 2014, 05:37 PM
#1
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
Here is my code.
Code:
//Create a file, append to it, and read it.
#include <iostream>
#include <fstream>
#include <string.h>
#include <stdio.h>
using namespace std;
char filename[256];
string n;
string filelist;
void CreateNew(ofstream & FileNew);
void ReadNew(ifstream & FileGet);
void AppendNew(ofstream & FileApp);
int response;
char trash[2];
int fileyes=0;
//Main
int main()
{ ofstream FileCreate;
ifstream FileRead;
ofstream fileAppend;
cout<<"Do you want to find information about an employee? (0 for yes, 1 for no) ";
cin>>response;
cin.getline(trash, 2);
while(response == 0)
{
ReadNew(FileRead);
if(fileyes==1)
{
CreateNew(FileCreate);
}
cout<<"fileyes = "<<fileyes<<endl;
cout<<"enter another name (0 for yes, 1 for no) ";
cin>>response;
}
return 0;
}
//functions
void CreateNew(ofstream & FileNew)//Create file
{
char filename[256];
string name;
FileNew.open(filename);
name = filename;
if(FileNew.is_open())
{
cout<<"\n";
cout<<name<<".txt file is successfully created!\n";
cout<<"\n";
fileyes=0;
}
else
cout<<"can not open a file...\n"<<endl;
}
void ReadNew(ifstream & FileGet)// Read file
{ char filename[256];
string name;
char newname;
string line;
fileyes=0;
cout<<"Enter file name to extract info.: ";
cin.getline(filename, 256);
name = filename;
FileGet.open(filename);
if(FileGet.is_open())
{
while(1)
{
getline(FileGet, line);
cout<<line<<endl;
if(FileGet.eof())
break;
}
}
else
fileyes=1;
cout<<"read1\n ";
}
-
March 31st, 2014, 04:59 AM
#2
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. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
March 31st, 2014, 08:59 AM
#3
Re: Pass value from one function to another
Code:
#include <string.h>
I guess you wanted the <string> header.
Code:
char filename[256];
string n;
string filelist;
void CreateNew(ofstream & FileNew);
void ReadNew(ifstream & FileGet);
void AppendNew(ofstream & FileApp);
int response;
char trash[2];
int fileyes=0;
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
Code:
std::string fileName = getFileName();
std::ifstream ifs(fileName);
if (!ifs.is_open()) {
createFile(fileName);
ifs.open(fileName);
}
displayContents(ifs);
Code:
cout<<"Do you want to find information about an employee? (0 for yes, 1 for no) ";
cin>>response;
What happens when the user inputs a letter? See Why does my program go into an infinite loop when someone enters an invalid input character?
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
April 4th, 2014, 09:10 AM
#4
Re: Pass value from one function to another
Note that <string> and <cstring> are not the same.
<cstring> is C++'s name for the old C <string.h>. It contains functions to operate on char*-style strings, like strcat, strcpy, etc.
<string> is the C++ string class. It's much safer, easier, and all around better to use than char*s in most cases.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|