1

I have a very basic question. I'm trying to read from a file that has data saved like this:

Collins, Bill
Smith, Bart
Allen, Jim
.
.
.
Holland, Beth

and I would like my code to read the data and save them into one column of an array. So what I did was,

#include<iostream>
#include<string>
#include<cstring>
#include<iomanip>
#include<fstream>

using namespace std;

int main()
{
string first, last, FIRST[200], LAST[200];

ifstream infile;
infile.open("names.dat");

while (!infile.eof())
{
for (int i = 0; i < !infile.eof(); i++)
{
    infile >> first;
    FIRST[i] = first;
    cout << FIRST[i] << " ";

    infile >> last;
    LAST[i] = last;
    cout << LAST[i] << " " << endl;
    }
}
return 0;
}

However, I want just one array named NAME[], not two(FIRST[] & LAST[]). So basically if I call NAME[0] would be Collins, Bill.

I really don't know how to do this... reading sources makes me even more confused..

This is just a little part of the entire program that I have to write, which is sorting names alphabetically and I haven't even passed this stage yet.

Jerry Coffin
  • 437,173
  • 71
  • 570
  • 1,035
weathergirl
  • 41
  • 1
  • 6
  • So, why don't you use one 2D array then with proper indexing? – lpapp Apr 06 '14 at 05:41
  • Why: `i < !infile.eof()` !? –  Apr 06 '14 at 05:42
  • @LaszloPapp so like NAMES[][] ?? – weathergirl Apr 06 '14 at 05:45
  • @DieterLücking cuz I thought I needed to do i < infile.eof() instead of !infile.eof() but it didn't work... this code at least worked so.. – weathergirl Apr 06 '14 at 05:46
  • `while (!infile.eof())` is [bad](http://stackoverflow.com/questions/21647/reading-from-text-file-until-eof-repeats-last-line) [practice](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). – jrok Apr 06 '14 at 05:56
  • @jrok that was what my book said... but okay – weathergirl Apr 06 '14 at 05:58
  • There are bad C++ books out there, unfortunately. See [here](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) for reccomendations. – jrok Apr 06 '14 at 05:59

3 Answers3

2

You might just read each line:

#include<iostream>
#include<fstream>
#include<deque>

int main()
{
    std::deque<std::string> names;
    std::ifstream infile("names.dat");
    std::string name;
    while(getline(infile, name))
        names.push_back(name);
    return 0;
}

Testing for EOF is often not a test for a successful input. Here the getline returns the stream and the condition while(stream) is testing the stream state.

Regarding the comment:

Have a #include<algorithm> and std::sort(names.begin(), names.end());

  • The OP wanted to read the data into an array, so this does not answer the question. – lpapp Apr 06 '14 at 05:58
  • the reason why i wanted an array is that I have to eventually write a code that will sort the names alphabetically and thought an array will be easier – weathergirl Apr 06 '14 at 05:59
  • @weathergirl: you can sort an array with std::sort as well as some other containers. – lpapp Apr 06 '14 at 06:25
  • @DieterLucking I tried your method and it gives me bunch of errors. I don't know what is wrong, since I haven't used deque ever yet. – weathergirl Apr 07 '14 at 01:33
1

Just use a 2D dimensional array then as follows:

main.cpp

#include<iostream>
#include<string>
#include<cstring>
#include<iomanip>
#include<fstream>

using namespace std;

int main()
{
    string first, last, myarray[200][2];

    ifstream infile;
    infile.open("names.dat");

    int i = 0;
    while (!infile.eof()) {
        infile >> first;
        myarray[i][0] = first;
        cout << myarray[i][0] << " ";

        if (infile.eof()) {
            cout << endl;
            break;
        }

        infile >> last;
        myarray[i][1] = last;
        cout << myarray[i][1] << " " << endl;
        ++i;
    }
    return 0;
}

Output

Collins, Bill
Smith, Bart
Allen, Jim

That being said, I would personally use std::array or some more intelligent container type in C++, in general.

lpapp
  • 47,035
  • 38
  • 95
  • 127
1

Just use one array of strings instead of two.

int main()
{
   string first, last, NAME[200];

   ifstream infile;
   infile.open("names.dat");
   int i = 0;

   while (true)
   {
      infile >> first;
      infile >> last;

      if (!infile.eof() && infile.good() )
      {
         NAME[i] = last + ", " + first;
         cout << NAME[i] << std::endl;
      }
      else
      {
         break;
      }
      ++i;
   }
   return 0;
}
R Sahu
  • 196,807
  • 13
  • 136
  • 247
  • Actually, the OP's code is invalid, and I would not suggest to copy and paste that further on. Please also see the comments. – lpapp Apr 06 '14 at 05:48