1

I want to solve a programming contest task (C++ with XCode) which has a relatively big input (300 lines). When copying a test input into the console, it doesn't read it all. So I have written a minimalistic test program to simply read in 300 lines:

#include <iostream>

using namespace std;

int main(int argc, const char * argv[])
{
    ios_base::sync_with_stdio(false);

    string xxx;
    for(int i = 0; i < 300; i++)
        cin >> xxx;
    return 0;
}

If I execute it and copy 340 lines with "aaaaaaaaaa" into the console, it doesn't end. If I stop it with the debugger, it says i = 92. If I then continue, it quits. But when I copy pieces of 50 lines into the console, it quits immeadiately as it should...

Can anyone help me with this?

PS: I inserted the 'ios_base::sync_with_stdio(false);', because I read that this would spped the input up.

Martin G
  • 17,357
  • 9
  • 82
  • 98
RaFi
  • 11
  • 2
  • cin >> xxx will read a word, not lines. You need to use getline. – László Papp Apr 14 '14 at 21:12
  • Make your `string xxx`a global variable instead of a local, and check again. Possibly you are killing your local (function) stack. – Jongware Apr 14 '14 at 21:12
  • (OT: I would hesitate to call 300 lines "relatively big input". I frequently run tests on files exceeding 10,000 lines of text; finding storage space is generally not a problem.) – Jongware Apr 14 '14 at 21:14

3 Answers3

2

I want to solve a programming contest task (C++ with XCode) which has a relatively big input (300 lines). When copying a test input into the console, it doesn't read it all

This is possible when you have more than one word per line because cin >> xx will read words as opposed to lines.

You would need to use the getline method to actually read lines.

while (getline(cin, xxx));

If I execute it and copy 340 lines with "aaaaaaaaaa" into the console, it doesn't end. If I stop it with the debugger, it says i = 92.

Yes, this is exactly the same symptom. Even if you had one word per line, you would reach only 300 lines, never 340.

Here is the whole code I would write for your reference:

#include <iostream>

using namespace std;

int main(int argc, const char * argv[])
{
    ios_base::sync_with_stdio(false);

    string xxx;
    while (getline(cin, xxx))
        cout << xxx << endl;
    return 0;
}
László Papp
  • 51,870
  • 39
  • 111
  • 135
  • What sort of limit is the OP encountering? – Jongware Apr 14 '14 at 21:15
  • So is that a built-in limit to `cin`? (Addendum: see the OP's attempt to debug, using 340 "words"/"lines".) – Jongware Apr 14 '14 at 21:16
  • @Jongware: The OP reads 300 words, so 340 words will not be read in. – László Papp Apr 14 '14 at 21:19
  • But then where does `i=92` and his never-ending loop come into play? (I'll accept "In some code not shown in this post" as a good answer ;-) – Jongware Apr 14 '14 at 21:21
  • 1
    @Jongware: beats me based on the code shown, but the intent of reading line with operator>> was wrong either way. – László Papp Apr 14 '14 at 21:22
  • For this test, I have only one word per line. I also didn't want to read in all the 340 lines but just wanted the programm to read in its 300 lines, ignore the other ones and then end. But it doesn't end. I tested your programm end with 314 input lines, it only echoed 93 and then stuck. – RaFi Apr 14 '14 at 22:48
  • I didn't not look close at the code, but may it not possibly also be a buffer problem of the console itself not being able to hold all pasted data? – t.niese Apr 15 '14 at 05:32
  • @RaFi: I really cannot reproduce your issue, nor my friends that I asked to try. – László Papp Apr 18 '14 at 11:59
0

cin >> string inputs words, not lines. Try:

string xxx;

while (getline(cin, xxx)) {
  ;
}

to input a full line at a time into xxx.

Casey
  • 41,449
  • 7
  • 95
  • 125
0

I couldn't solve the problem with X-Code but it works with Qt. So I use Qt now.

RaFi
  • 11
  • 2