Reading from a text file into strings

I'm pretty new to c++ and come from a bash background, I am writing a chat bot that on the first time running it will assign a random alignment to the bot, ask your name, birth month and birth date and write these to a file on their own lines. This part works fine, however I'm having issues trying to use these as strings for reference later on either after creating the file or if it is already created.
basically I want the bot to reference the text file ".udfile" and pull user information from there to say hello username, etc.

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
51
52
53
54
  #include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <map>

using namespace std;
using std::cout;
using std::ifstream;
using std::string;

int main ()
{

                srand (time(NULL)); //initialize the random seed
                string userdatafile = ".udfile";
                ifstream fin(userdatafile.c_str());
                    if(!fin) {
                    cout << "no userdata file present" << ".\n";
                    cout << "initialzing first time setup" << ".n";
                    string introinput;
                    int persChart = rand() % 8;
                    string chartArray[9] = {"lg", "ln", "le", "ng", "tn", "ne", "cg", "cn", "ce"};
                    ofstream outputfile(".udfile");
                    outputfile << chartArray[persChart] << endl;
                    outputfile.flush();
                    cout << "What's your name? ";
                    getline (cin, introinput);
                    outputfile << introinput << endl;
                    cout << "What is the month you were born? (Please enter in format: MM)";
                    getline (cin, introinput);
                    outputfile << introinput << endl;
                    cout << "What is the day you were born? (Please enter in format: DD)";
                    getline (cin, introinput);
                    outputfile << introinput << endl;
                    outputfile.close();
                    
}
else {
        }
ifstream infile;
string alignment, username, usermonth, userday,;
	    infile.open(".udfile");
            getline(infile, alignment, \.n);
	    getline(infile, username, \.n);
	    getline(infile, usermonth, \.n);
            getline(infile, userday, \.n);
            cout << "my alignment is" << alignment <<endl;
            cout << "hello " << username <<endl;
            cout << " your entered month and day are " << usermonth << userday <<endl;
            
             }
infile.close();
}
Last edited on
edit

actually once i fixed a number of small issues, it ran fine and did what you wanted it to do.
you had an extra }, the close file was out of scope, an extra comma on your variable list, and stuff like that breaking compile. Is there any chance you are failing to compile and running an old executable (happens to all of us once in a while?)

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
51
52
53
54
  #include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <map>

using namespace std;
using std::cout;
using std::ifstream;
using std::string;

int main ()
{

                srand (time(NULL)); //initialize the random seed
                string userdatafile = ".udfile";
                ifstream fin(userdatafile.c_str());
                    if(!fin) {
                    cout << "no userdata file present" << ".\n";
                    cout << "initialzing first time setup" << ".n";
                    string introinput;
                    int persChart = rand() % 8;
                    string chartArray[9] = {"lg", "ln", "le", "ng", "tn", "ne", "cg", "cn", "ce"};
                    ofstream outputfile(".udfile");
                    outputfile << chartArray[persChart] << endl;
                    outputfile.flush();
                    cout << "What's your name? ";
                    getline (cin, introinput);
                    outputfile << introinput << endl;
                    cout << "What is the month you were born? (Please enter in format: MM)";
                    getline (cin, introinput);
                    outputfile << introinput << endl;
                    cout << "What is the day you were born? (Please enter in format: DD)";
                    getline (cin, introinput);
                    outputfile << introinput << endl;
                    outputfile.close();
                    
}
else {
        }
ifstream infile;
string alignment, username, usermonth, userday;
	    infile.open(".udfile");
            getline(infile, alignment, '\n');
	    getline(infile, username, '\n');
	    getline(infile, usermonth, '\n');
            getline(infile, userday, '\n');
            cout << "my alignment is" << alignment <<endl;
            cout << "hello " << username <<endl;
            cout << " your entered month and day are " << usermonth << userday <<endl;
infile.close();            
             }

Last edited on
Thanks Jonin!!! that worked awesome :)
glad to help!
you are going to want to be more careful about fixing compiler errors and understanding the error messages so you can fix them, though. I would make it an exercise to take your original code and try to compile it, and fix the errors it gives you one by one by understanding the message and finding the problems. It will help you long term.
Topic archived. No new replies allowed.