I'm having problems getting a complete string from the datafile. Whenever I use cin.getline, it always asks me to enter in values in order to get information from the file. I just want to display the name, the last name and than a description about themselves. Here is the coding to my file:
include <fstream.h> // Header file used to open/close files
#include <stdlib.h> // Header file that includes the 'exit' function
#include <cstring.h> // Header file that is used to declare variables as strings
#include <io.h> // Header file that includes the 'EOF' function
string name, lname; // Declares the variables as strings
string namearray [10]; // Declares an array
string lnamearray [10];
string agearray [10];
char age [256];
int a =-1;
void main ()
{
ifstream new_name; // Declares the input stream
new_name.open ("c:/names.txt", ios::in); // Opens the textfile
if (!new_name) // Checks to see if file is in the disk
{
cout << "Error opening file.\n"; // Displays information on to the screen
exit(1); // Exits the program
}
while (new_name)
{
a++; // Counter
new_name >> name >> lname; // Gets the data from the file
cin.getline (infob, '\n');
namearray [a] = name; // Stores it into an array
lnamearray [a] = lname;
agearray [a] = infob;
eof(0); // Ends the loop when it is at end of file
}
for (int v=0; v < a; v++)
cout << namearray [v] << " " << lnamearray [v] << " " << agearray [v] << endl; // Displays values from the file
new_name.close(); // Closes the file
}