?
Solved

Problems with scanf and gets

Posted on 2004-08-26
9
Medium Priority
?
225 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
ID: 11907812
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
ID: 11908763
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 85

Expert Comment

by:ozo
ID: 11908815
What do you want to happen when the enter key is pressed?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:theravibes
ID: 11908830
I want the following

if the end user does not enter any character, and presses the enter key -> assign exit_value=1
0
 
LVL 11

Accepted Solution

by:
avizit earned 1000 total points
ID: 11908930
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
ID: 11909611
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
ID: 11909628
read "if eventhen its answered"
as "if even then its NOT answered"

0
 
LVL 85

Expert Comment

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

Expert Comment

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

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
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…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
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.
Suggested Courses

807 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