Hi, I'm working on a menu where option 1 is ask user for a file name, and option 2 is read from a file.
How can I make it to functions?
I
This is what I have for option 1:
1 2 3 4 5 6 7 8 9 10 11
void inputfile();
void inputfile()
{
ifstream masterfile;
string filename;
cout << "Please enter the name and extension of your file " << endl;
cin >> filename;
masterfile.open(filename);
}
My question is, how can I call the file name from the function inputfile so I can read the document in another function?
I have tried without functions, but when I want to call the file in case 2, it won't show any data.
Maybe I misunderstand, but I feel like there's some information here you're leaving out.
If your code currently doesn't have any functions, what makes you think it will magically start working once you introduce more functions? That being said, using functions is definitely recommended for organization and proper logic, so you certainly should use them!
it won't show any data.
How are you trying to make it show data? The code you posted does not make any attempt to actually read the file contents. So I'm kinda shooting in the dark as to what the actual problem is.
So you want to input the filename in inputfile(), and then actually read the document in another function, let's call it read_file_contents().
I would pass the file stream into the read function.
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
string get_filename()
{
string fname;
// your code
cout << __FUNCTION__ << "\n";
return fname;
}
void read_file(const string& filename)
{
// your code
cout << __FUNCTION__ << "\n";
}
int main()
{
int choice;
string filename;
for (;;)
{
cout << "\n1 - Enter filename";
cout << "\n2 - read from file";
cout << "\n3 - Exit";
cout << "\nEnter your choice: ";
cin >> choice;
if (choice == 1) // maybe use switch if covered
filename = get_filename();
elseif (choice ==2)
read_file(filename);
elseif (choice == 3)
break;
}
}
CAUTION: This code is just an example of how things can be done.
It is neither the only way nor the best way.
It is not properly tested and might not be correct.
what I'm trying to do is,
case 1: ask user for file
case 2: show user's file content.
how can I user the return value of the function 1, so function 2 can read the file?
The way I modify how Ganado did, it asks again for the user input.
I thought that declaring file = inputfile(); it would take the return value of function 1.
Yep Chervil's is definitely the way to go. Using references for fstreams is definitely the better way, to prevent any weirdness in compiler-version differences, and just for clarity if nothing else (so you know you're only looking at the file with one stream).
And just to clarify my previous post: If you compile with C++11/14/17, my code will work, but if you compile with C++98 (and change filename to filename.c_str() as needed), you will get errors related to the constructor of the ios_base/basic_ifstream when returning masterfile. I suppose it has to do with move semantics? C++11 also deletes the copy constructor.