Solved

Problem exiting menu on Enter key

Posted on 2004-08-23
6
253 Views
Last Modified: 2010-04-15
I am having a bit of a problem with a project that I am working on...

Here is what I need to be able to do.

The user will be entering data into a set of fields.
If the end user does not enter a value into the field and presses enter,
it will quit the loop.

That is it...

The problem that I have right now is with scanf and gets, if the user presses enter and no value is entered, it will insert a newline character, and remain in the field.

Thanks for your help...
0
Comment
Question by:theravibes
6 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11872370
you can use gets() to read user entry, it will return anyway, and then use sscanf to apply scanf to the string filled by gets.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11872761
sscanf() will return you the number of arguments read, so you will know if input is complete
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 11873008
First of all,usage of gets() is not recommended because of buffer overrun problems.You can use fgets() instead.Read the man pages for gets() for details.

char str[MAX];
You can use scanf("[^\n]",str); or fgets(str,MAX,stdin); to store a line of text(till enter is pressed)

To store everything until enter is pressed into an char array str.Then,you can do the parsing for the values yourself.(You can use sscanf() here to parse the string for values,as jaime pointed out)

0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 11873041
Use 'gets()' instead, e.g.

int main( void )
{
   char line[81];

   printf( "Input a string: " );
   gets( line );
   if (strlen(line))printf( "The string entered was: %s\n", line );
    else printf( "Only the 'ENTER' key was pressed\n");
}

Then, refactor your problem to

int read_value ( char* buf) {

    gets(buf);

    return(strlen(buf));
}

The above function will read the input field and return '0' if only enter was pressed, and the length of the field otherwise.
0
 

Author Comment

by:theravibes
ID: 11909583
Worked like a charm...

Thanks jkr...
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 11911613
Hi Tyson,

Glad to know you solved your problem,but here's why i said gets() should not be used.

From the man pages: (http://www.rt.com/man/getc.3.html)

       Because it is impossible to tell without knowing the  data
       in  advance  how  many  characters  gets()  will read, and
       because gets() will continue to store characters past  the
       end  of  the buffer, it is extremely dangerous to use.  It
       has been used to break  computer  security.   Use  fgets()
       instead.

You can use jkr's idea using fgets() instead of gets()
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question