std::cout problem

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;
}
 
C

Christopher

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;

}

Use std::cin, then read:
http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.2
 
M

Martin York

char szValue[100] = {0,};
scanf("%[^\n]\n", szValue);

This will kill you if your line is longer than 100 characters,
myName = (std::string) szValue;
The fact that this works is just scary.
Using C casts is not a good idea. Just assign the C-String to myName
and it will work.

myName = szValue;

To do this in C++ it is mutch easier to use std::getline();

std::string myName;
std::getline(std::cin,myName);

All done.
 
M

Martin York

To answer your original question:

why does:
scanf("%[^\n]\n", szValue);

not work when I just type <enter>

The above scanf() fails because the %[^\n] does not match anything.
Because the match fails scanf() aborts, and it does not read anything
from the stdin so the <enter> character is still on the input stream.
You loop around and repeat. The scanf never works because the <enter>
character is never matched and therefore never removed from the input
stream.

Try:
scanf("%[^\n]",szValue);
scanf("\n");

The first scanf() may work or fail (depending if their is text) then
the second scanf() will read the "\n" thus removing it from the input
stream. Hope that helps.

Martin.

PS. Use the C++ version.
 
M

Martin York

Or to make it really safe:

scanf("%99[^\n]",szValue); // scan the data you want max 99
characters. Leave room for terminator.
scanf("%*[^\n]"); // ignore anything after 99 upto the endof
line
scanf("\n"); // remove the end of line.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top