W
wongjoekmeu
Dear All,
I have written a small program to read in from console a user string.
I wanted to be able to read in a string containing of all sorts of
characters untill the user press enter. I have to use in my program
the scanf function. The program works fine, but there is one
exception. When I don't insert any input, but simply press enter, I
want actually to pop up asking the user to enter a valid input. But it
seems according to my output screen as if the std::cout has problem.
Because it continuously print to console and it does not prompt and
wait for the user for input again. What am I doing wrong ? Could
someone please help me out. Here below you can see my test program
code that has this problem. By the way I saw that if I don't do the
print statement std::cout after nothing has been inserted by the user
then scanf seems so wait for input again.
#include <iostream>
int main(int argc, char* argv[])
{
std::cout << "Enter your name: " << std::endl;
char szValue[100] = {0,};
std::string myName;
while(1)
{
scanf("%[^\n]\n", szValue);
myName = (std::string) szValue;
std::cout << "myName = " << myName << std::endl;
std::cout << "Size = " << myName.size() << std::endl;
if ( myName.size() != 0 )
{
break;
}
}
return 0;
}
I have written a small program to read in from console a user string.
I wanted to be able to read in a string containing of all sorts of
characters untill the user press enter. I have to use in my program
the scanf function. The program works fine, but there is one
exception. When I don't insert any input, but simply press enter, I
want actually to pop up asking the user to enter a valid input. But it
seems according to my output screen as if the std::cout has problem.
Because it continuously print to console and it does not prompt and
wait for the user for input again. What am I doing wrong ? Could
someone please help me out. Here below you can see my test program
code that has this problem. By the way I saw that if I don't do the
print statement std::cout after nothing has been inserted by the user
then scanf seems so wait for input again.
#include <iostream>
int main(int argc, char* argv[])
{
std::cout << "Enter your name: " << std::endl;
char szValue[100] = {0,};
std::string myName;
while(1)
{
scanf("%[^\n]\n", szValue);
myName = (std::string) szValue;
std::cout << "myName = " << myName << std::endl;
std::cout << "Size = " << myName.size() << std::endl;
if ( myName.size() != 0 )
{
break;
}
}
return 0;
}