Yet another person asking for help with code

First time post. I've been trying to figure this out for the last hour. I can't get the following to compile correctly:
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
struct studentInfo
{
	string name;
	int idNumber;
	double score;
};

//MAIN OMITTED

void getData(studentInfo x[])
{
	string filename;
	
	cout << "Enter file name: ";
	cin >> filename;
	
	ifstream fin(filename.c_str());
	
	int count = 0;
	while(!fin.eof() && count < 20)
	{
		char temp[15];
		fin.get(temp);  //My problem
		x[count].name = temp;
		fin >> x[count].idNumber >> x[count].score;
		count++;
		fin.ignore();
	}
}


On line 23, I keep getting the following error:

no matching function for call to 'std::basic_ifstream<char,std::char_traits<char> > :: get(char[15])'

I've looked around and I can't figure out what's going on. My professor used the same example in class, but it still doesn't work. Can anyone help? Thanks ahead of time.

edwin.
Last edited on
Try this:
1
2
std::string temp;
fin.getline(temp);
Thanks for your help, but I'm still getting the same error.

These are the header files I'm using:
1
2
3
4
5
#include<iostream>
#include<iomanip>
#include<fstream>
#include <string>
 using namespace std;


Also, using getline probably would not work because my data looks like this:

Tom Wilson 1432 93.7
Kylie Jones 2006 85.1

where the ints and doubles go into separate parts of the record.
Last edited on
Sorry, I meant this:
 
getline(fin, temp);
Closer, thanks! The only problem is that I can't get the getline to stop after collecting "Tom Wilson" and it gets the line (as it should). Anyone?
You can either use getline to get a single line at a time, or fin >> temp to get a single word at a time. If you want to read a "name" each time, C++ don't know what a "name" is, so you have to implement it using the basic functions.

I don't know exactly what you want though. Does the line contain more after "Tom Wilson", since you want it to stop at that point? In this case, if you always have exactly one first and one last name, you could do something like this:
1
2
string first, last;
fin >> first >> last;


However, if you want to support more advanced stuff like optional middle names, you can't expect a single standard function to extract one "name" and stop. You have to do some programming yourself. The easiest way is probably to have the file format as simple as possible, like one name on each line.
Last edited on
Topic archived. No new replies allowed.