Trying to get input from file and send to a class

Let say I have this file "inputfile.txt" and the file contains the text below...

First Name: Ron
Age: 40


And I want get the txt after the ": ". For example I would want the input "Ron" and "40" and put it into a class to be stored. How exactly would I do that? This is what I have so far...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <fstream>
#include <stdlib.h>

using namespace std;

int main()
{
    string inputFile;
    
    cout << "Please enter the file you would like to input: ";
    cin >> inputFile;
    
    ifstream fin(inputFile.c_str());
}


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
#ifndef INPUT_H
#define INPUT_H

#include <iostream>

using namespace std;

class input
{
    public:

        void setFirstName(string firstName);
        void setAge(int age);

        string getFirstName();
        int getAge();
        
    private:

        string fn;
        int a;
};

#endif // INPUT_H


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "input.h"

void input::setFirstName(string firstName)
{
    fn = firstName;
}

string input::getFirstName()
{
    return fn;
}

void input::setAge(int age)
{
    a = age;
}

int input::getAge()
{
    return a;
}


I don't know quite how to skip the first part of the text ("First Name: ") and only input the text after that ("Ron")? Also is there any way to do the input stuff in the classes and not in the main function? Basically I want to ask, in the main function, if the user would like to create a person with a first name and age? If the user says yes, then there is a function call to the input class, and the input class takes in the input file, and stores all the input, to be retrieved later.
Last edited on
Something like this?

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
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <vector>

using namespace std;

int main()
{
    string inputFile;

    vector<string> inputs;

    cout << "Please enter the file you would like to input: ";
    cin >> inputFile;

    ifstream fin(inputFile.c_str());

    string word;

    while(fin >> word)
    {
        fin.ignore(256,': ');
        inputs.push_back(word);
    }

    cout << inputs[0] << endl;
    cout << inputs[1] << endl;
}


It compiles fine, but I'm trying to test if it works properly, but when I run it, it doesn't print out the cout statements, it just freezes.
Last edited on
Does anyone know what I'm doing wrong here?
on line 23: ': ' is not a valid char. Remove the space

if you want to ignore from the beginning you shouldn't read word before that. It' contain "First" in your example
So I took the spaces out, after the semicolon, of the text file, so it looks like this now...

First Name:Ron
Age:40

And changed the code like you said, but it still doesn't seem to be working.

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
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <vector>

using namespace std;

int main()
{
    string inputFile;

    vector<string> inputs;

    cout << "Please enter the file you would like to input: ";
    cin >> inputFile;

    ifstream fin(inputFile.c_str());

    string word;

    fin.ignore(256,':');

    while(fin >> word)
    {
        inputs.push_back(word);
    }

    cout << inputs[0]<< endl;
    cout << inputs[1]<< endl;
}
Topic archived. No new replies allowed.