Letting the user choose what file to read from.

So the assignment I was given was to have the user type in the name of a text file and then read out the text of the file converting it into Pig Latin, if no file is specified it's supposed to read from test.txt. I was able to get it to read from test.txt without issue and the pig latin wasn't an issue, but I just can't seem to get the "user indicates what file to read from down pat. I've tried having the user input it as a string but that didn't work, and everywhere I look online I get so much advanced code that I can't understand it. This is the basic code I've manage to make so far that just reads from test.txt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
 #include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <cmath>
#include <ctime>
#include <fstream>
#include <ctype.h>
#include <stdio.h>
using namespace std;

string word;

string newWord;
string fileName;

void readFile();
ifstream fin("test.txt");
int main()
{
	
	readFile(); 
	fin >> word;
	bool notDone = true;
	while( fin >> word ) {
	
	
   

    
    char fLetter = word[0];
    newWord = word.substr(1) + fLetter + "ay ";
    //newWord= tolower(newWord);
    cout << newWord;}
    
    //cout << word;
    
    
    return 0;
}

void readFile()
{

ifstream fin();
ofstream fout;
   
   
}
1
2
3
4
5
std::string strFile;
std::getline(std::cin, strFile);
if (strFile.empty()) { // user pressed enter key
    std::cout << "You did not enter a valid filename...";
}
Great, that allows me to get the filename but it doesn't answer the question of how to make the now strFile into the name of the file the information gets pulled from. I put strFile into the fin parenthesis but the program remains empty... What I'm having trouble with is making it so that the input text is the file that is pulled.
Last edited on
If you were not able to read anything from the file the user entered, then either the file does not exist or the file is indeed empty.
I cannot reproduce the results you get otherwise
So I took what you gave me and moved that into the read file process but like I said it's not working. I'm sorry if I'm not understanding right, this is one of my first times working with files. this is what I have now.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
string word;
string strFile;
string newWord;
string fileName;

void readFile();

int main()
{
	
	readFile(); 
	ifstream fin("strFile");
	fin >> word;
	bool notDone = true;
	while( fin >> word ) {
	
	
   

    
    char fLetter = word[0];
    newWord = word.substr(1) + fLetter + "ay ";
    //newWord= tolower(newWord);
    cout << newWord;}
    
    //cout << word;
    
    
    return 0;
}

void readFile()
{

	string strFile;
getline(cin, strFile);
if (strFile.empty()) { // user pressed enter key
   cout << "You did not enter a valid filename...";
}
   
   
}
Last edited on
Create a function that returns a string. The function will first ask the user for a file to read from. If the user did not supply this file, the function will then return the string test.txt otherwise the function returns the string that the user entered.
Then in main, you will have an ifstream object that takes whatever this function returned as a filename and try to read from it.

1
2
3
4
5
6
7
8
9
10
11
12
13
int main() {
    std::ifstream ifs();

    std::string filename = getFileName(); // this is the function to return desired file
    ifs.open(filename);

    for (std::string word; ifs >> word; ) {
        // do piglatin stuff
    }

    ifs.close();
    return 0;
}
Last edited on
It keeps returning a crap ton of errors when I try using any of this code or trying to alter it to make it work with what I have... Thanks for your help my friend, I guess I just don't understand ifstream yet as well as I should. I'll have to ask a few of my classmates in class tomorrow.
Here is the full skeleton of what I posted above (not complete, but should compile):
http://cpp.sh/4l3s
It works! Alright thank you, I finally see what I did wrong. The way it was taught in class i thought that fin was an operation similar to cin, rather than a variable. Thank you so much.
Topic archived. No new replies allowed.