Thread: Files & String ARrays

  1. #1
    Unregistered
    Guest

    Files & String ARrays

    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
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >cin.getline (infob, '\n');
    sould be
    new_name.getline(infob, '\n');

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    155
    instead of

    cin.getline(infob, '\n) ;//what is infob?? you never declare it.

    it should be

    new_name.getline(age, 255, '\n')

    or

    new_name.getline(age, 255)

    which uses the default terminating char for getline() which is the newline char, assuming lname, fname, and age are all on the same line, or at least age is not on a line by itself. If age is on a line by itself, then you need to call new_line.ignore() after the last use of new_line >> before the call to new_line.getline() or you will not get the results you want as >> doesn't clear the terminating char from the input stream and if that char is newline then the call to getline() will just see the "left over" newline char and put nothing in age and the data for age will end up in the next lname, etc.

    Also, eof() is an ios class method inherited by instances of ifstreams and should be used as such--maybe something like this:

    while(!new_name.eof())
    {
    //etc.

  4. #4
    Unregistered
    Guest
    Hi! I changed the line to new_name.getline ! But now it only gets the last lines of the datafile. Why is that?

    #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

    char name [256];
    char lname [256];
    char *namearray [50]; // Declares an array
    char *lnamearray [50];
    char infob [200];
    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.getline (name, 255, '\n');
    new_name.ignore();
    namearray [a] = name; // Stores it into an array
    eof(0); // Ends the loop when it is at end of file
    }

    for (int v=0; v < a; v++)
    cout << namearray [v] << " " << endl;
    // Displays values from the file

    new_name.close(); // Closes the file
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 12:47 PM
  2. string arrays
    By Raison in forum C Programming
    Replies: 27
    Last Post: 10-02-2003, 06:27 PM
  3. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM
  4. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM
  5. string handling
    By lessrain in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 07:36 PM