Solved

Problems with scanf and gets

Posted on 2004-08-26
9
219 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 84

Expert Comment

by:ozo
ID: 11908815
What do you want to happen when the enter key is pressed?
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

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 500 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 84

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops 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.

770 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