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.
Aside from the useless declaration of variable x , I think there's nothing wrong with your code.
If n is filled with 2 the result is always 1 and 0 get printed, regardless the cin >> s; is uncommented or not.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.