-
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 ";
}
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
|