Question

I've run into a really strange issue. I can reproduce on my win7 laptop as well as an ubuntu machine.

I have a C++ program like so:

#include <string>
#include <sstream>
#include <iostream>
using namespace std;

int main() {
  for (int i = 0; i < 9; i++) {
    string line;
    getline(cin, line);
    stringstream ss(line);

    for (int j = 0; j < 9; j++) {
      int p = 8;
      ss >> p;
      cout << p;
    }
    cout << endl;
  }
  return 0;
}

Now, if i compile it an run it with ./a.out < test.txt where text.txt contains:

1 2 3 4 5 6 7 8 9
2 2 3 4 5 6 7 8 9
3 2 3 4 5 6 7 8 9
4 2 3 4 5 6 7 8 9
5 2 3 4 5 6 7 8 9
6 2 3 4 5 6 7 8 9
7 2 3 4 5 6 7 8 9
8 2 3 4 5 6 7 8 9
9 2 3 4 5 6 7 8 9

It will output (without spaces):

8 8 8 8 8 8 8 8 8
2 2 3 4 5 6 7 8 9
3 2 3 4 5 6 7 8 9
4 2 3 4 5 6 7 8 9
5 2 3 4 5 6 7 8 9
6 2 3 4 5 6 7 8 9
7 2 3 4 5 6 7 8 9
8 2 3 4 5 6 7 8 9
9 2 3 4 5 6 7 8 9

Why is the first line wrong? I've tried reading the first line out of the loop as well. Also, if I replace ss > p with cin > p I just get an output table full of 8's.

This is not making any sense!!

Okay you guys were right. Some weird stuff as the first character of my input file:

od -c test.txt
0000000 357 273 277   2       0       5       0       0       7       0
0000020       0       6  \n   4       0       0       9       6       0
0000040       0       2       0  \n   0       0       0       0       8
Was it helpful?

Solution

It's a problem with the data (since the code looks OK). Most probably you've saved your text file with UTF-8 encoding with BOM. An UTF-8 BOM is three bytes at the start of the file, and trying to interpret those as a decimal number specification would fail.

Second, third, fourth line etc. OK because you're creating new istringstream object for each line, so not retaining error mode from previous line.

So, fix: save the file without BOM -- assuming the BOM hypothesis is correct.

Cheers & hth.,

OTHER TIPS

Your code seems fine to me, if I were you I'd double check the input file : are you sure there is no empty first line, or some non-numeric character on the beginning of line 1 ?

I suspect you wrote your own getline(), and the bug is there. InputStreams have a getline(char*, int), and I suspect your cramming string.begin() into the first param, and Some Other Number into the latter.

Don't do that.

All your program should be doing is copying the input to the output (given this code and that input). It's not doing that either, even on the lines that "work".

I am seeing a number of Not So Experienced Programmer 'signatures' here. 1) Overly short variable names (outside a for loop counter), "ss" and "p" 2) Magic error number (8), particularly one that doesn't stand out from the data. 3) "using"

1 and 3 both hint at a lack of typing speed, and therefore experience... despite your 1k+ reputation (which is based mostly on asking questions... the situation becomes clearer).

I'd rewrite it something like this:

int curDig;
curLine >> curDig;
if (curLine.good()) {
  cout << curDig;
} else {
  cout << "FAILED at line: " << lineIdx << " containing: " << line << std::endl;
}

Chances are, you're going to see "FAILED at line: 0 containing: " right out of the gate, due to what I think is a bug in your getline().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top