Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Problems with scanf and gets

Posted on 2004-08-26
9
220 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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 

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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

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…
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 opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

789 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