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.

7
  • So, why don't you use one 2D array then with proper indexing? Commented Apr 6, 2014 at 5:41
  • Why: i < !infile.eof() !? Commented Apr 6, 2014 at 5:42
  • @LaszloPapp so like NAMES[][] ?? Commented Apr 6, 2014 at 5: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.. Commented Apr 6, 2014 at 5:46
  • while (!infile.eof()) is bad practice. Commented Apr 6, 2014 at 5:56

3 Answers 3

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());

Sign up to request clarification or add additional context in comments.

4 Comments

The OP wanted to read the data into an array, so this does not answer the question.
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: you can sort an array with std::sort as well as some other containers.
@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.
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;
}

1 Comment

Actually, the OP's code is invalid, and I would not suggest to copy and paste that further on. Please also see the comments.
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.

1 Comment

Quick question here, would mixing pre/post increment of i here make any difference??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.