user3401108
user3401108

Reputation: 109

Output not satisfactory in C program due to scanf function

I am executing this program. But on running this program, first it asks " Enter the Number of Test Cases" and after that i enter the number of test cases . but even after entering the number of test cases the cursor again is in state of asking an input from user. I don't know that after giving the number of test cases as input why is it asking for another input . Any help will be very grateful?

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int main()
{ 
   int i,N;
   char * arr[N];
   printf("Enter the Number of Test Cases\n");
   scanf("%d \n",&N);
   printf("Enter the string\n");

   for(i=0;i<N;i++)
   { 
      arr[i]= (char *)malloc(100*sizeof(char));
      scanf(" %s ",arr[i]);
   }
   for(i=0;i<N;i++)
   {
      printf("The Enter String : %s \n",arr[i]);
   }
   for(i=0;i<N;i++)
   { 
      free(arr[i]);
   }
   return 0;
}

Thanks friends for discussing this question. I got the answer and the real problem is with the scanf function. I got a nice link to read about this problem. Hope you like it. http://c-faq.com/stdio/gets_flush2.html

Upvotes: 0

Views: 156

Answers (5)

Michael Burr
Michael Burr

Reputation: 340208

There are several small changes you need to make to the program:

  • fix how the arr[] array is allocated. Move the declaration of arr[] to after the scanf("%d",&N); that initializes N.

  • change scanf("%d \n",&N); to scanf("%d",&N);

  • change scanf(" %s ",arr[i]); to scanf("%s",arr[i]);

In my opinion scanf() isn't a great way to parse interactive input - it's too difficult to detect and handle minor variations on how someone might type in text. scanf() might be fine for for test/demo programs or programs that are expected to deal with data files that have a rather well-defined, fixed format.

Anyway, what's happening in your program as posted is that the scanf() calls are getting stuck matching runs of whitespace in the input stream. So if you remove that whitespace from your format specs, the program behaves more like you expect it to.

Upvotes: 0

Kalanidhi
Kalanidhi

Reputation: 5092

I have edited this code after getting the N value then declare the array of pointer else junk value assigned

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
main()
{ 
  int i,N;
  printf("Enter the Number of Test Cases\n");
  scanf("%d",&N);
  char *arr[N-1];
  printf("Enter the string\n");
  for(i=0;i<N;i++)
  { 
    arr[i]= (char *)malloc(100*sizeof(char));
    scanf("%s",arr[i]);
   }
  for(i=0;i<N;i++)
  {
    printf("The Enter String : %s \n",arr[i]);
  }
  for(i=0;i<N;i++)
  { 
    free(arr[i]);
  }
 return 0;
}

Upvotes: 0

Raman
Raman

Reputation: 51

Please read about string formatting in scanf here.

I think what you really intended to use was:

scanf("%d", &N);

Just a heads up on something else wrong with the code. You are using an uninitialized value (N) when declaring your array:

  char * arr[N];

You may want to have a look at that as well.

Upvotes: 0

user2076694
user2076694

Reputation: 836

You probably should verify your arr[N] because you are creating a static array with a size of N which isn't defined.

Upvotes: 0

Sakthi Kumar
Sakthi Kumar

Reputation: 3045

The problem is in the scanf statement

scanf("%d \n",&N);

Please remove the ' ' and the '\n' and it will work

scanf("%d",&N);

Also,

char * arr[N];

does not work the way you want. A better solution would be.

char **arr = NULL;
/* get input for N */
arr = malloc(N * sizeof(char *));

Upvotes: 1

Related Questions