• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 320
  • Last Modified:

gets not reading the string

#define SIZE 100
int main()
{
  char s[SIZE];
  char d[SIZE];

  int choice,length;

  printf("enter your choice : \n");
  scanf("%d",&choice); // choice = 1 entered
  switch(choice)
  {
          case 1 :  printf("enter a string to find length");
                    gets(s); // not readinf the string

                    length=string_length(s);
                    printf("length of %s is %d",s,length);
                    break;

         default :
                    break;
  }


   return 0;
}


In the above code gets() is not reading what is the problem . I am not able to find out
0
nagaharikola
Asked:
nagaharikola
  • 4
  • 3
1 Solution
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi naga,

The scanf() is reading the integer from your initial string and leaving the newline character in the buffer.

Either do a getchar/getch/getc to read the newline, or add a '\n' to the end of the format string in the scanf.


Good Luck,
Kent
0
 
nagaharikolaAuthor Commented:
gets(s) in case 1 is not working
0
 
nagaharikolaAuthor Commented:
why gets(s) is not working
0
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

 
Kent OlsenData Warehouse Architect / DBACommented:

Yep.

You call scanf before you get to the switch statement.

When you enter the data for the scanf, you enter 012\n (where \n is the newline/carriage return).  Scanf read the 012 and leaves the \n in the buffer.  When you next call gets(), the \n is still in the buffer.  Follow the advise above and your code will work.  :)


Kent
0
 
nagaharikolaAuthor Commented:
why does it happens.
cant we use scanf before gets()
0
 
nagaharikolaAuthor Commented:
when i use getchar i am getting the correct output
why does it happens.
can't we use scanf before gets()
0
 
Kent OlsenData Warehouse Architect / DBACommented:
Sure you can use as many calls to scanf and gets as you want, and in any order.  But you need to understand what they do.

Input is the TOTAL stream of all characters that you enter from the time the program starts until the program stops.  The first call to scanf() reads ONLY the digits that you enter.  It does NOT process the newline character, even though the Input driver put it into the buffer.  The "%d" format specifier specifically tells scanf() to STOP processing as soon as a non-digit is encountered.  The newline character is the first non-digit.

I've modified the scanf() format string below.  It tells scanf() to skip past the newline character after the program reads the initial number.  It will work just fine, too.


Kent

#define SIZE 100
int main()
{
  char s[SIZE];
  char d[SIZE];

  int choice,length;

  printf("enter your choice : \n");
  scanf("%d\n",&choice); // choice = 1 entered
  switch(choice)
  {
          case 1 :  printf("enter a string to find length");
                    gets(s); // not readinf the string

                    length=string_length(s);
                    printf("length of %s is %d",s,length);
                    break;

         default :
                    break;
  }


   return 0;
}

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Protect Your Employees from Wi-Fi Threats

As Wi-Fi growth and popularity continues to climb, not everyone understands the risks that come with connecting to public Wi-Fi or even offering Wi-Fi to employees, visitors and guests. Download the resource kit to make sure your safe wherever business takes you!

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now