Structures and File input questions

So basically, I have no idea. I can't get getline to work for the structure and I've been trying for hours upon hours. I'm also using a function. Any help would be appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  void studentRegistration(Values info[MAX_STUDENT], int size)
{
	ifstream inFile; //needed to open file and read
	inFile.open("student.txt");
	if (!inFile.fail())
	{
		for (int i = 0; i < 11; i++)
		{
			getline(inFile, info[i].ID);
			
                 }
		
                 inFile.close();
		cout << "Done.\n";
	}
}


My structure looks like this:
1
2
3
4
5
6
struct Values 
{
	int ID;
	char name;
	char answer_choice; 
};




My text file looks like this:

ID number, name, answer
Last edited on
You're using getline on an int. You can only use getline on strings. Also you need to read info by info... Your for loop should have at least 3 states, depending on how you program it.

1
2
3
4
5
6
for(int i = 0; i << 1; i++)
{
    // read in type_int memberID
    // read in name
    // read in answer_choice
}
Last edited on
Well I changed the name to a string but i'm still getting an error

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
ifstream inFile; //needed to open file and read
	inFile.open("student.txt");
	if (!inFile.fail())
	{
		
                for (int i = 0; i < 11; i++)
		{
		    inFile >> info[i].ID;	
                    inFile.getline(info[i].name, 50, '\n');
                    inFile.getline(info[i].answer_choice, 1, 'n');
			
                 }
		
                 inFile.close();
		cout << "Done.\n";
	}


I'm also using char for answer_choice. I keep getting "no instance of overload function"
ID number, name, answer

That still leaves room for uncertainty, I can't be sure whether the ID is an integer or maybe contains other characters. name must be a string, not a single character. answer could be anything, an integer, a character, a string of several words.

Given that your struct had char name; which doesn't make sense, it is quite likely that the other parts may be wrong or less than optimal too.


Assuming that only the name is a string, maybe do it like this. Here I read the file one line at a time, then extract the details from that line. A stringstream is used to do that. Also I passed the size by reference, to hold the actual number of records read from the file.

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
55
56
57
58
59
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <iomanip>

using namespace std;

struct Values 
{
	int ID;
	string name;
	char answer_choice; 
};

const int MAX_STUDENT = 100;

void studentRegistration(Values info[MAX_STUDENT], int & size)
{
    ifstream inFile("student.txt");
    if (!inFile)
    {
        cout << "file could not be opened\n";
        return;
    }
    
    size = 0;
    string line;

    while (size < MAX_STUDENT && getline(inFile, line))
    {
        // use a stringstream to parse the contents of each line
        istringstream ss(line);
        char comma; 
        ss >> info[size].ID >> comma;
        getline(ss, info[size].name, ',');
        ss >> info[size].answer_choice;

        if (ss) // if stream input did not fail
            ++size;
    }
    
}

int main()
{

    Values student_data[MAX_STUDENT];
    int count = 0;
    studentRegistration(student_data, count);  
    
    for (int i=0; i<count; ++i)
    {
        cout << setw(10) << student_data[i].ID
             << setw(20) << student_data[i].name
             << setw(10) << student_data[i].answer_choice << '\n';
    }
    
}

Topic archived. No new replies allowed.