1

I am not too familiar with C syntax. I need to process some data based on user input. Although I processed the data successfully but I am stuck at user input section. I removed the unnecessary data processing section and made a simple example of how I am taking the user input. Can anyone tell me what's the problem with below code :

int i, number;
char *str;
str=(char*)malloc(1000*sizeof(char));
printf("Enter count : ");
scanf("%d", &number);
for(i=0; i<number; i++)
{
    printf("\nEnter string: ");
    scanf ("%[^\n]%*c", str);
    printf("%s", str);      
}

Output:

"Enter count : " appears fine, but whenever I provide some value and hit enter it's showing me only 'count' number of Enter string: without enabling user to enter the string.

For example -

Enter count : 2

Enter string:
Enter string:

But if I discard the count input section and provide any fixed value, like

for(i=0; i<5; i++)

it works fine

Thanks in advance

4
  • 2
    or scanf("%d%*c", &number); Commented Nov 20, 2014 at 10:52
  • use fgets for reading strings, scanf is a PITA. Commented Nov 20, 2014 at 11:02
  • @BLUEPIXY hey thank you, can you please post this as answer? Commented Nov 20, 2014 at 11:02
  • @MichaelWalz suggestion gladly accepted, but still I need to change the scanf("%d", &number) section as suggested by BLUEPIXY otherwise teh program skips first string input Commented Nov 20, 2014 at 11:04

3 Answers 3

2

FYI, there is no issue in for(i=0; i<number; i++), problem is in scanning logic.

Actually, scanf ("%[^\n]%*c", str); is not right. you should use %s to read strings, not %c, which reads a single character, including the ENTER (newline).

Rather, i would suggest, use fgets() for inputs. It's a whole lot better in every way. Check the man page here.

Maybe you can use something like

//Dummy code

int i, number;
char *str;

printf("Enter count : ");
scanf("%d", &number);
str=malloc(number*sizeof(char));   //yes, casting not required
fgets(str, (number-1), stdin );      //"number" is used in different context
fputs(str, stdout);

EDIT:

Working code

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

#define SIZ 1024

int main()
{
    int i, number;
    char * str = malloc(SIZ * sizeof (char));

    printf("Enter the number :\n");
    scanf("%d", &number);
    getc(stdin);                      //to eat up the `\n` stored in stdin buffer
    for (i = 0; i < number; i++)
    {
        printf("Enter the string %d :", (i+1));
        fgets(str, SIZ-1, stdin);
        printf("You have entered :");
        fputs(str, stdout);
    }

    return 0;
}
Sign up to request clarification or add additional context in comments.

5 Comments

hey, thank you for your answer, in my string there are lot of blank spaces, and if i simply use scanf("%s", str); it takes only first word, but regarding fgets() thank you for that suggestion, but still i need to change the scanf("%d") section, otherwise, first string input will skip
@Diptarag first string input will skip.. what do you mean? in your version, number was besing used to scan number of single characters, here we're directly getting the string. right?
No, in my original version number is being used to take some string inputs repeatedly and print them instantly, the number of times the program would ask for input string is denoted by variable 'number' which I am taking at first, I admit fgets() is better but there was really no problem with the scanf scanning inside the loop, but the real problem was with scanf of number, please run the code with fgets only without making any change to scanf("%d", &number) and you will know, and btw I did not downvote
the calls to scanf() should check the returned code to assure that an input was successfully performed. The scanf() format parameter should contain a leading ' ' so that white space, like the newline, is consumed.
this line: fgets(str, SIZ-1, stdin); should be: fgets(str, SIZ, stdin); because fgets() allows for a final null termination char.
0

scanf("%s",str); Use this instead of the code you are using to take string inputs in a character array.

2 Comments

hey, thank you for your answer, in my string there are lot of blank spaces, and if i simply use scanf("%s", str); it takes only first word
you have to use fgets() or else take inpute one character character in a loop, as %s takes space as an escape character like enter
0

There is a newline character \n after entering count value which is picked up by %c in your scanf() Just use %s to scan strings as shown below.

scanf("%s",str);

If there are spaces in your input.

Then do

char c[50];

fgets(c,sizeof(c),stdin);

Check the below code:

#include <stdio.h>
#include<stdlib.h>
int main(){
   int i, number;
   char *str;
   str=malloc(1000*sizeof(char));
   printf("Enter count : ");
   scanf("%d%*c", &number);
   for(i=0; i<number; i++)
   {   
      printf("\nEnter string: ");
      fgets(str,1000,stdin);
      printf("%s", str);    
   }   
}

4 Comments

hey, thank you for your answer, in my string there are lot of blank spaces, and if i simply use scanf("%s", str); it takes only first word
@Diptarag Check what can be done to read your whole input in the edited version.
thank you, but the real problem lies with the scanf("%d", &number) section, if i use fgets instead of scanf still it would skip first input if i do not use scanf("%d%*c", &number)
@Diptarag Yes you need to change you scanf as you have done because to skip the newline character.I have shown how to read a string with white spaces to be read

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.