5

Possible Duplicate:
Why doesn’t getchar() recognise return as EOF in windows console?

I have a simple problem... Lets say I want to read lines from standart input as long as there is something, but I dont know how many lines it will be. For example I am doing school work and input is

a
ababa
bb
cc
ba
bb
ca
cb

I dont know exactly how many lines it will be, so I tried

string *line = new string[100];
    int counter = 0;
   while(getline(cin,line[counter]))
   {
    counter++;
   }

But it doesn't work... thanks for help.

Community
  • 1
  • 1
user1751550
  • 85
  • 2
  • 7
  • Well for example i enter "aa", enter, "bb", enter and now i dont enter anything and just press enter but nothing happen.. and i need to end reading from input after last value was entered. – user1751550 Nov 23 '12 at 18:08

5 Answers5

4

If you want input to end on an empty line then you have to test for it. For instance.

string *line = new string[100];
int counter = 0;
while (getline(cin, line[counter]) && line[counter].size() > 0)
{
    counter++;
}

Congrats for using getline() correctly BTW. Unlike some of the answers you've been given.

john
  • 85,011
  • 4
  • 57
  • 81
  • 1
    if your counter getts bigger than 100 this will lead to seq faul. right? – dreamcrash Nov 23 '12 at 18:27
  • @dreamcrash, Yes. Better code would be to use `std::vector` and `push_back()` to add a line to the vector. But I like to introduce one new thing at a time. – john Nov 23 '12 at 18:31
  • ``!line[counter].empty()`` would be much faster than ``line[counter].size() > 0`` – hinafu Nov 23 '12 at 19:27
  • 2
    @hinafu How much money would you be willing to bet on that claim? :) – jrok Nov 23 '12 at 19:30
  • I don't know, the money I get when I get TLE instead of AC in an online judge, which is zero. – hinafu Nov 23 '12 at 19:32
1

You can get the number of lines with something like:

   string *line = new string[SIZE];
   int lines = 0;

    while(lines < SIZE && getline(cin, line[lines]) && line[lines].size() > 0) 
   {
        cout << input_line << endl;
        lines++;
   }

Don't forget to check if you are not adding more lines than the size that string line can handle, otherwise you can get Segmentation Fault.

dreamcrash
  • 47,137
  • 25
  • 94
  • 117
1

The simplest line counter I can think of would be something like this:

#include <string>
#include <iostream>

unsigned int count = 0;

for (std::string line; std::getline(std::cin, line); )
{
    ++count;
}

std::cout << "We read " << count << " lines.\n";

Test:

echo -e "Hello\nWorld\n" | ./prog

If you want to discount empty lines, say if (!line.empty()) { ++count; } instead.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
0

You can also use the end of file marker for this. Its usage is like this.

std::ifstream read ("file.txt") ;

while(!read.eof())
{
  //do all the work
}

This function returns true if end of file has been reached. So it would continue until you encounter that.

Edit:

As mentioned in the comments as well, the method eof could be dangerous and not provide the desired result. So there is no guarentee that it would run in every case. You can have a look here about when this might happen.

Reading from text file until EOF repeats last line

Community
  • 1
  • 1
Coding Mash
  • 3,338
  • 5
  • 24
  • 45
  • 2
    That's an extremely dangerous suggestion that usually leads to terrible code. It's nearly impossible to use `eof()` in a sane or correct fashion. – Kerrek SB Nov 23 '12 at 18:08
  • Absolutely right. As OP has school work and input format is given, i thought it would most probably run fine. – Coding Mash Nov 23 '12 at 18:10
  • 1
    *Teaching* this sort of terrible idea to innocents is even worse -- that's why almost every SO post pertaining to C++ I/O is *wrong* in the same way, and people with "years of C++ experience" don't get this stuff right, either. The best thing we can do is never even mention that `eof()` exists to anyone but the most advanced programmers. – Kerrek SB Nov 23 '12 at 18:12
  • 2
    What! How did this get three up-votes? -1 from me. – john Nov 23 '12 at 18:13
0

This should work:

int cont = 0;
string s;
while(cin >> s) { //while(getline(cin,s)) if needed
  if(s.empty()) break;
  ++cont;
}
hinafu
  • 689
  • 2
  • 5
  • 13