Could someone explain that code:
int main()
int x;
while (cin >> x){ <-------(explain this!)
for (int i=x; i>0; i--) {
cout << "*";
}
cout << endl;
}
return 0;
}
thanks in advance.
Printable View
Could someone explain that code:
int main()
int x;
while (cin >> x){ <-------(explain this!)
for (int i=x; i>0; i--) {
cout << "*";
}
cout << endl;
}
return 0;
}
thanks in advance.
Hello,
Ah, this. cin is the standard input stream. >> operator means "get from", while << operator means "put to". This code is taking the information from the input stream and storing it in your variable x.Code:while (cin >> x) {
- Stack Overflow
snap you're new to the forum so please read this thread...
and wellcome ;)
about your code, 1st consider what Stack Overflow said.
So after reading something to x you have a while statement, which is like.
While expression evaluates to true, or in other word by C/C++ standards different than 0 (correct me if I'm wrong), the to do code is execited. So while x is different than 0....Code:while( expression ){
//to do here
}
Actually, the operator>> returns a reference to the input stream (in this case cin). The streams have an implicit conversion that allows you to check to see if the input succeeded or failed. So if cin succeeded in getting data, and is still in a good state, then the loop will continue. If cin failed to get the data (for example, the user entered a letter instead of a number), then the failbit will be set and the cin stream will convert to false.
So if the user enters 0, then x will be 0 but the loop will continue because the input was valid and cin will still be in a good state.
Upps.. bah! you're right jlou.. I got blinded a bit, but like someelse here at the forums would say, "was beer...", or not :D
So.. in**couff** cicle.. good!!
Code:ok, i understood.. ;)