Solved

Problems with scanf and gets

Posted on 2004-08-26
9
217 Views
Last Modified: 2010-05-18
I am working on a project, and I am having a bit of a problem...

More specifically, I need a way that the end user can be entering data into a field, and if a character is entered, then it just stores it to an int (have this working just fine),
and when they do not enter any characters and press the enter key, then it will assign a variable 'exit_value' to 1

the problem that I am currently having is that when the end user presses enter and there is nothing on the line, it prints a new line, rather than taking the enter key.

Any help on this will be greatly appreciated...

Tyson Edwards
0
Comment
Question by:theravibes
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 22

Expert Comment

by:grg99
Comment Utility
check the return value from scanf-- it will probably be "0" when they don't enter anything.

so you can write:

NumAnswers = scanf( ..... probably something like:  "%d\n", &TheNum );

if ( NumAnswers < 1 ) exit_value = 1; else ... whatever...;

0
 

Author Comment

by:theravibes
Comment Utility
That is not really the problem. What the problem is that if you sit at a prompt, how can you take the Enter key as a value, and continue through the code?

So, I am sitting at a command prompt with the following code...
What do I need to modify in order to make it so I will accept the enter key, rather than just drawing a new line when no characters are entered?

See below...

      printf("  Test Score 1:  ");
      scanf("%i", &test_score_01);
      if(test_score_01=='\0')
        {
        exit_value=1;
        }  
      if(exit_value==0)
          {      
          printf("  Test Score 2:  ");
        scanf("%i", &test_score_02);
        if(test_score_02==NULL)
            {
            exit_value=2;
            }
        }
      if(exit_value==0)
        {
        printf("  Test Score 3:  ");
        scanf("%i", &test_score_03);
        if(test_score_03=='\0')
            {
            exit_value=3;
            }
        }
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
What do you want to happen when the enter key is pressed?
0
 

Author Comment

by:theravibes
Comment Utility
I want the following

if the end user does not enter any character, and presses the enter key -> assign exit_value=1
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 11

Accepted Solution

by:
avizit earned 500 total points
Comment Utility
is this what you wanted ?



char s[50];
gets(s);
if(s[0] == '\0'){  /* if the user pressed teh enter key */
   exit_value = 1;
}else  {
    test_score_01 = atoi(s)
}



0
 
LVL 11

Expert Comment

by:avizit
Comment Utility
Thanks but is a grade of 'C' justified. ?
if your question wasnt answered to your satisfaction you can leave it open so that other experts may look at it.

and if eventhen its answered you can ask for the question to be deleted and get your points refunded.

What's the right grade to give?  --> http://experts-exchange.com/help.jsp#hi73
0
 
LVL 11

Expert Comment

by:avizit
Comment Utility
read "if eventhen its answered"
as "if even then its NOT answered"

0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
Perhaps theravibes found a problem when the user typed more than 50 characters.
0
 
LVL 11

Expert Comment

by:avizit
Comment Utility
Then obviously he could have waited for someone else to come up with a better solution.
or pointed out here .

0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now