reading input text problems

I was doing fine in my C++ class until we hit functions. Maybe we are covering material too fast for me. I have three different books that I am using with a fourth ordered. My issue with this program is that I have to read in a text file with last name, first name and/or middle name. The output is to be first name, last name. I go from 3 errors to up to 29 at one point. The compiler mostly complains about my getline and my cin. If someone could point me to a specific line I should focus on and/or a good reference book with examples, I would greatly appreciate it.

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
#include <iostream>
#include <string>
#include <fstream>



using namespace std;

void PrintingNames (string firstName, string lastName);
void Length (string firstName, string lastName);

int main()
{

ifstream inFile;
ofstream outFile;
string firstName;
string lastName;

inFile.open("Chapter8Input");


if (!inFile)
   {
    cout << "File could not be opened." << endl;
    return 1;
    }

PrintingNames (firstName, lastName);

inFile.close();
outFile.close();

return 0;
}


void PrintingNames (string firstName, string lastName)

{
// read the data from the text file
getline(lastName, ',', firstName);
cin.ignore (',');
cin.get("lastName");
cin.get("firstName");

	outFile.open("Chapter8Output");
	cout << firstName << "" << lastName << endl;

}

void Length (string firstName, string lastName)
{
	string len;
	len = inFile.length();	
	cout << "Length is: " <<      endl;

}
The first problem I see is that you're using a getline function that doesn't exist.

To use getline to read from a file, the prototype is one of the following two option:

1
2
istream& getline ( istream& is, string& str, char delim );
istream& getline ( istream& is, string& str );


http://www.cplusplus.com/reference/string/getline/

There is another getline function, http://www.cplusplus.com/reference/iostream/istream/getline/ , which does something similar with C style stings.

You need to pass the getline function the input stream you wish to read from, and the name of the string to read the data into, and an optional delimiter.

getline(inFile, lastName, ','); for example.

cin fetches from the standard input stream, which is usually the keyboard. You can change it to be somehting else, but I wouldn't recommend that for the moment.

Last edited on
Thank you so much. I wished I found this site earlier in my C++ class. Very informative. I am only now having an issue with an error C2065, outFile undeclared identifier. I don't understand as the outFile and inFile were declared in main. Also should I not open the outfile until I am in the function writing to it?
Topic archived. No new replies allowed.