buffer is not clearing

I was trying to solve a programming challenge when I encountered this buffer issue. I am using getline command and I think cin buffer is not clearing but I don't know how to solve this. Can someone help me out?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
#include <sstream>
using namespace std;


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    string inStr;
    unsigned long val = 4294967295;
    int inVal; //variable to store input integer value
    unsigned long result; //variable to store result
    int count; // to count number of lines to print out
    getline(cin,inStr);
    istringstream buffer(inStr);   
    buffer>>count; //converting string to an integer value using stringstream
    buffer.clear();
    cin.clear();
    while(count){
		getline(cin, inStr);
		istringstream buffer(inStr);
		buffer >> inVal; //converting string to an integer value using stringstream
		result = val - inVal;
		cout << result << endl;
		buffer.clear();
		inStr.clear();
		cin.clear();
		count--;    
        inVal=0;
        result=0;
    }
    return 0;
}


This is the input to the code

10
606478781
2105669450
924295380
995178010
277153377
34375824
2927870501
1445985000
2854010163
3305365756


This is the output I am getting

3688488514
3370671915
4017813918
2147483648
2147483648
4294967295
4294967295
4294967295
4294967295
4294967295


This is the correct output

3688488514
2189297845
3370671915
3299789285
4017813918
4260591471
1367096794
2848982295
1440957132
989601539
closed account (SECMoG1T)
Hi , make a utility function that will do that for you and call it at any point that you need to clean your buffer contents or error states.

1
2
3
4
void Clean_Buffer(std::stringstream& ss)
{
    ss.str(""); ss.clear();
}


/The main function of member function .clear() is to turn off an error state bit that might have been set when reading data, it doesn't at any point clean the buffer content, i myself would empty the content of the string member if i needed clean the buffers.

hope that helps.
Your problem isn't with the buffer not clearing. You're trying to store a value in a type that's too small to hold it, and it's overflowing.

In line 15, you define inVal as an int.
In line 26, you do buffer >> inVal - but consider the numbers in your input some of them are bigger than an int can hold and inVal will overflow, therefore giving you a wrong result.

Define inVal as an unsigned long as well.

unsigned long inVal;

Some other suggestions:
review your overuse of clear()
line 14 should have type specifiers:
unsigned long val = 4294967295ul;
Don't use the same name for different things, as you've done with "buffer", shadowing the previous definition inside your while loop. This is legal, but not advised.
Thanks @andy1992 & @tipaye, changing it to unsigned long worked..
Topic archived. No new replies allowed.