Makis Renieris
Makis Renieris

Reputation: 17

C++ ifstream help (simple)

I am trying to get some names from a .txt file and add them on a char array but something weird happens. Here's my input portion of the code:

int main()
{
string namelist[30];
int i=0;
string line;
ifstream file("C:\\names.txt");
if (file.is_open())
{
    while ( getline (file,line).good () )
    {
        getline(file,line);
        cout << line << endl;  // It prints the names normally (it was added for   debugging) //
        namelist[i] = line;
    }
    file.close();
}
cout << namelist;  // Here is the prob.

On the last line of the code it prints a pointer on the console and not the list and I have no clue why. I'm pretty new to c++ so don't be rude!

The text file is something like:

John
Nick
Samatha
Joe
...

Any help would be appreciated :)

Upvotes: 0

Views: 136

Answers (1)

Maxwe11
Maxwe11

Reputation: 486

because the name of array it's a pointer, write

for (int i = 0; i < 30; ++i)
    std::cout << namelist[i] << std::endl;

or

#include <algorithm>
#include <iterator>
//...
std::copy(namelist
         , namelist + 30
         , std::ostream_iterator<std::string>(std::cout, "\n")
);

Upvotes: 1

Related Questions