user1902849
user1902849

Reputation: 1141

C Scanf in a loop

It works for the first time, when execute on 2nd time, it skip the second scanf function. After google from several pages, noticed that it was the behavior of the scanf function that add \n in the buffered, to solved this, I added fflush(stdin) after the scanf and it did worked, however when executed on 2nd times, it give me a wrong result. Someone can guide me what is the problem of this program ?

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

int main()
{
char UserInput[50];
int i = 0;
int exit;


do{


printf("Please enter a string (less than 50 character): ");
scanf("%[a-z,A-Z, ,]s",&UserInput);

while(UserInput[i] != '\0' && i<50)
{
    i++;
}

if (i==50)
    printf("The string is too long\n");
else
    printf("The length of the string is %d\n",i);

printf("To continue, please key in any numbers other than 0: ");
scanf("%d",&exit);
    fflush(stdin);
}while(exit !=0);

system("PAUSE");
return 0;
}

Upvotes: 0

Views: 1449

Answers (3)

Sander De Dycker
Sander De Dycker

Reputation: 16243

Several mistakes and remarks :

  1. when using a scanset format (%[a-zA-Z ]), you are not supposed to use an s at the end like you do, nor are the comma's supposed to be there.
  2. you should check the return code when calling scanf to make sure that you actually succeeded reading what was supposed to be read.
  3. you have a risk of overflowing the UserInput buffer - it only allows for a string of length 49, but you impose no such limitation when filling the buffer.
  4. you don't poperly reset i back to 0 after each iteration.
  5. exit should have the type int, since you use the format %d to fill it. As pointed out elsewhere, exit isn't a particularly good variable name in C (there is a standard function called exit).
  6. you use multiple scanf calls without properly taking care of reading any leftovers from the previous read. It's better to read input line by line (using fgets eg.) into a buffer, and then parse the data you need out of that buffer.
  7. don't use fflush on an input stream (like stdin). It is only intended for output streams.

Upvotes: 0

unwind
unwind

Reputation: 399713

There are several issues.

Your loop to compute the length is a bit pointless, and it also doesn't reset i after having run once, so it'll break on the second attempt.

Remove the loop, use strlen() instead if you feel you must compute the length.

Even better, remove the use of sscanf() and use fgets() instead.

Note that &UserInput is a bit pointless, the name UserInput will "decay" to a pointer to the first element (&UserInput[0]) in a function call and is the best way to state it:

fgets(UserInput, sizeof UserInput, stdin);

is how I would write it. Don't forget to check the return value.

Upvotes: 0

Ivaylo Strandjev
Ivaylo Strandjev

Reputation: 70931

You don't use ampersand to read an array. Change the line to:

scanf("%[a-z,A-Z, ,]s",UserInput);

Also exit is also a char array, not an integer. Either change exit to be int or change the scanf to: scanf("%s",exit);. And trust me exit is not a good name for a variable in C.

Upvotes: 1

Related Questions