I'm just trying to figure out this:
I have following code:
#include <iostream>
using namespace std;
int main() {
int n, x = 0;
cin >> n;
char s[3];
while (n-- > 0) {
cout << n << endl;
// cin >> s;
}
return 0;
}
If I fill n with 2, everything works fine (1
and 0
get printed), but if I uncomment the cin >> s;
line, only first iteration (1
) will be performed (and the input gets saved into s
).
Now my question: why is working like that? And how can I fix it? Thanks in advance.
looks like classic buffer overflow
this is expected too of course. – n. 1.8e9-where's-my-share m."X++"
? Hint: not three. – n. 1.8e9-where's-my-share m.