Jump to content

C - Detecting no user input

- - - - -

  • Please log in to reply
4 replies to this topic

#1
skybox

skybox

    Newbie

  • Members
  • Pip
  • 4 posts
After reading a chapter on file i/o, I referenced an example in a program of mine that simply will not work. As the title says, I'm having the user enter three different data types, then it saves the information to a file. It is supposed to keep looping until the user just presses enter (At least, I think that's what this segment is supposed to do). Anyway, I've read I shouldn't use the 'feof' function, so I'm hoping someone can offer a better suggesting. Here's the segment:

printf( "Enter ......\n" );

printf( "Enter EOF to end input.\n" );

printf( "? " );

scanf( "%d%s%lf", &account, name, &balance );


while ( !feof( stdin ) )

{

  fprintf( cfPtr, "%d %s %.2lf\n", account, name, balance );

  printf( "? " );

  scanf( "%d%s%lf", &account, name, &balance );

}

Right now, if the user returns an empty line, I get a repeat of '?' on the output until I kill the program. Basically, what I'm wanting is to be able to detect no input given, and work with that condition. I'm certain this isn't the code to do it, but Google searches on the subject haven't turned up anything terribly useful. I hope this is all clear enough, and I really appreciate any and all help in advance. Thanks.

#2
Ancient Dragon

Ancient Dragon

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 400 posts
On MS-Windows press Ctrl+Z <Enter>
Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.

#3
skybox

skybox

    Newbie

  • Members
  • Pip
  • 4 posts

Ancient Dragon said:

On MS-Windows press Ctrl+Z <Enter>

Thanks, Dragon. Yeah, I've decided to scrap that entire approach, as the keystroke is all but obvious to a normal user. How might I go about detecting a blank line entered?

#4
dbug

dbug

    Programmer

  • Members
  • PipPipPipPip
  • 155 posts
I think there is a problem with scanf call. You try to read an integer, a string and a float. The integer is read correctly, but the remaining line is read as a string, because any character is valid in a string. So the float is "eaten" by the string. The scanf function will only parse 2 arguments: an integer and a string.

#5
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
I think it's better if you assign a letter or number that will terminate your program. Sometimes, to me atleast, happens that I accedently press enter or I press enter too fast and get a blank line. Image if user accedently pressed only enter and he would forget to save data, all his work would be lost.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users