• C

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
nagaharikolaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kent OlsenDBACommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nagaharikolaAuthor Commented:
gets(s) in case 1 is not working
0
nagaharikolaAuthor Commented:
why gets(s) is not working
0
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

Kent OlsenDBACommented:

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 OlsenDBACommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.