Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Problems with scanf and gets

Posted on 2004-08-26
9
Medium Priority
?
224 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
[X]
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
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
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.
Suggested Courses

636 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