-4

Set2 while loop does not populate for some reason. Set1 works just fine.

std::stringstream ss;
std::string line;
std::getline(infile, line);
ss.str(line);
int input;

// Populate set1
while(ss >> input)
{
    set1.insert(input);
    std::cout << "Populate set1 with " << input << "\t pos is " << set1.getUsed() << std::endl;
}

// Populate set2
std::getline(infile, line);
ss.str(line);

std::cout << "\n2nd getline verification: " << line << std::endl;

while (ss >> input)
{
    set2.insert(input);
    std::cout << "Populate set2 with " << input << "\t pos is " << set2.getUsed() << std::endl;
}

It only populates set1 and not set2. Thank you for your help.

Edit: It reads getline now, thank you. But it doesn't inport the values in "line" to the ss stringstream, so for some reason the second loop for set2 doesn't get recognized.

  • What *does* your program do? Where is `infile` defined? have you opened the file? – crockeea Feb 16 '14 at 05:31
  • Yes it is defined. set1 imports just fine. set2 does not. – Haakon Sjøgren Feb 16 '14 at 06:17
  • If you've solved your previous problem, please update the code to reflect that as it's highly confusing to keep the old code and refer to a corrected and working update without any evidence of it. Also, what is `set1` and `set2`? I know they're sets, but what is their purpose? – jrd1 Feb 16 '14 at 06:31
  • @jrd1 actually, no. The code shouldn't be updated, SO isn't a prototyping site. This question should be marked answered, as per your answer, and a new question should be opened with the new problem. This also will help the OP cut his question in smaller problems, which might help him solve it by himself. Imho. – Kheldar Feb 16 '14 at 13:06

1 Answers1

2

That's not surprising as you've only read the line once - you aren't looping over the stream at all. Your code should be:

std::string line
while(std::getline(infile, line)) {
    std::cout << line << std::endl;//see what's in the line
    //other code here...
}

Why? Because you want to keep reading from the stream (until an EOF is encountered). In other words: you want to keep reading from the stream while you can get a line of data from the stream infile.

UPDATE:

The OP's question is different now relative to the above.

If for example your data file looks like this:

123 2978 09809 908098 
198 8796 89791 128797

You can read the numbers like this:

std::string line
while(std::getline(infile, line)) {
    //you line is populated
    istringstream iss(line);
    int num;

    while (!(iss >> num).fail()) {
        //save the number
    }
    //at this point you've reached the end of one line.
}
jrd1
  • 10,358
  • 4
  • 34
  • 51
  • Thanks for the reply, but it still only reads one line. while (std::getline(infile, line)) { while(iss >> input) { set1.insert(input); } } – Haakon Sjøgren Feb 16 '14 at 05:39
  • @HaakonSjøgren: Correct: because that's how you've done in in your code. Correct me if I am wrong, but your original problem is that you could only read one line. You now have a new unrelated problem - one that's related to other aspects of your code that you didn't address in your original post. Please edit your post to reflect that. – jrd1 Feb 16 '14 at 05:42
  • I don't wish to output the string though, I wish to convert the input-string-stream into ints. – Haakon Sjøgren Feb 16 '14 at 06:10
  • I edited it once more. Thank you. – Haakon Sjøgren Feb 16 '14 at 06:12
  • @HaakonSjøgren: I figured as much, and I've edited my question to reflect your update (although, I have no idea what `set1` and `set2` are). However, it should be noted that this is entirely against protocol, as it's only good etiquette to ask a new question if your previous problem was solved. – jrd1 Feb 16 '14 at 06:28