how the user enter a value ?

Pages: 12
> forgive the attempt to store a string as an int?

There is no attempt to store a string as an int.

std::cin >> i ; // type in "1234 abcd" ;

a. The first four characters '1', '2', '3', '4' are extracted from the input buffer.
b. An integer with a value of 1234 is formed from them, and i gets that value
c. The attempt to read an int is successful, the stream state is good.
d. <space>abcd<newline> remains in the input buffer.

std::getline( std::cin.ignore(), myString ) ;
a. std::cin.ignore()extracts and discards once character (space) from the input buffer
b. abcd<newline> remains in the input buffer.
c. std::getline() extracts abcd<newline>, places abcd into myString, throws away the newline and returns immediately.

In contrast, with:
std::getline( std::cin.ignore( 100, '\n' ), myString ) ;
a. std::cin.ignore( 100, '\n' ) extracts and discards everything in the input buffer (up to a maximum of 100 characters or till a newline is encountered).
b. The input buffer is now empty.
c. std::getline() sees that the buffer is empty and waits for user input.


Topic archived. No new replies allowed.
Pages: 12