Can anyone help me figure out how to create a cin statement that does not halt a program from placing output on the screen?
Printable View
Can anyone help me figure out how to create a cin statement that does not halt a program from placing output on the screen?
I dont quite get the question you are asking, could you elaborate a little?
Try looking into cin.peek( ).
>Can anyone help me figure out how to create a cin statement that does not halt a program from placing output on the screen?
cin cannot do this. What you want is a second process/thread of execution. Or you could perform some kind of non-blocking test and read to get some of that functionality:
But that isn't nearly as powerful as a second process, and can get cumbersome.Code:#include <iostream>
#include <string>
// Nonstandard
#include <windows.h> // For sleep
#include <conio.h> // For kbhit
void next_bar()
{
static size_t pos;
static std::string spin ( "|\\-/" );
std::cout<< spin[pos++ % spin.length()];
std::cout<<'\b'<<std::flush;
Sleep ( 100 ); // Slow the bar down a bit
}
int main()
{
std::string name;
bool done = false;
std::cout<<"Enter your name: "<<std::flush;
while ( !done ) {
if ( kbhit() ) {
std::getline ( std::cin, name );
done = true;
}
else
next_bar();
}
std::cout<<"Hello, "<< name <<'!'<<std::endl;
}
-Prelude
Forgot to return 0.
I presume you're referencing Prelude's code? If so, return 0 is not required for a return from main() in C++, as it is the guaranteed default.Quote:
Originally posted by XSquared
Forgot to return 0.
>I presume you're referencing Prelude's code?
Indeed.
>If so, return 0 is not required for a return from main() in C++, as it is the guaranteed default.
I didn't know that. VC++ always spits out an error whenever I forget to.
[edit]
I guess I was wrong. Checked Stroustrup's site and it does mention that. Cool
[/edit]