-4

help please this doesnt work properly

input
omayma.firstname : AAAAAAAAAAAAAAAAAAAAAAAAAAA
omayma.lastname : BBBBBBBBBBBBBBBBBBBBBBBBBBBB
output :
omayma.firstname : AAAAAAAAAABBBBBBBBBB
omayma.lastname : BBBBBBBBBB
expected output :
omayma.firstname : AAAAA (10 A exatcly)
omayma.lastname : BBBBBB (10)


typedef struct
{
    char firstname[10];
    char lastname[10];
} person;

int main()
{
    person omayma;
    printf("firstname : ");
    scanf("%10s", omayma.firstname);
    fflush(stdin);
    printf("lastname : ");
    scanf("%10s", omayma.lastname);
    fflush(stdin);
    puts(omayma.firstname);
    puts(omayma.lastname);
    return 0;
}
Omayma Benali
  • 119
  • 1
  • 7
  • 2
    `scanf("%10s", omayma.firstname);` -> `scanf("%9s", omayma.firstname);`, to save 1 character for `'\0'`. Also, `fflush(stdin)` is undefined behavior, it probably won't do what you want it to do. – mediocrevegetable1 Jun 02 '21 at 18:29
  • Note that a ***lot*** of names are longer than nine or ten characters. (And not everyone has two names.) – T.J. Crowder Jun 02 '21 at 18:31
  • What do you mean, it isn't working properly? What is the expected output? – Tim Randall Jun 02 '21 at 18:38
  • thank u so much that worked. i use fflush(stdin) to empty buffer i cant always use while(getchar()!='\n') because not always the buffer is full and it will demand from the user to enter a character – Omayma Benali Jun 02 '21 at 18:38
  • @OmaymaBenali you should account for `EOF` as well. So I would do `for (int c; (c = getchar()) != EOF && c != '\n';);` Or perhaps a bit cleaner, `scanf("%*[^\n]%*c");`. But yeah, the buffer being empty issue is always there with these cases. – mediocrevegetable1 Jun 02 '21 at 18:41
  • @mediocrevegetable1 Pretty sure that `scanf` won't work as expected if the name is shorter than 10 characters. The first conversion will fail, so the second conversion won't be performed, leaving the newline in the buffer. – user3386109 Jun 02 '21 at 18:44
  • 1
    @mediocrevegetable1 Sorry, but you can't use `"%*[^\n]%*c"` and "clean" in the same sentence. – Steve Summit Jun 02 '21 at 18:46
  • @user3386109 you're right, it will be bad if the buffer isn't actually still full. – mediocrevegetable1 Jun 02 '21 at 18:47
  • 1
    @mediocrevegetable1 If you find yourself using `%[^\n]` to fix some `scanf`-using code, what you've actually done is proved that `scanf` is, in fact, useless. `scanf`'s *only* virtue is that it's quick'n'easy for newbies to use. But if you have to use `%[]`, it ain't quick'n'easy any more, and you might as well abandon `scanf` entirely and use something better. – Steve Summit Jun 02 '21 at 18:48
  • 1
    @OmaymaBenali Just so you know: `fflush(stdin)` is wrong. It got you past your problem today, so that's fine, but pretty soon, you'll want to stop using it -- and also stop using `scanf`, too. (If you don't call `scanf`, you will never need `fflush(stdin)`.) – Steve Summit Jun 02 '21 at 18:50
  • what can i use other than scanf or gets which is worse ? – Omayma Benali Jun 02 '21 at 18:58
  • @SteveSummit yeah, that's fair enough. I find `fscanf` more useful for file parsing, but I agree that user input and parsing do not mix as well. Though I think that no standard method of string input can avoid characters remaining in the stream if there isn't enough buffer space. Which is why a big issue with OP's code is that the buffer size is too small. – mediocrevegetable1 Jun 02 '21 at 18:59
  • 2
    You can use `fgets()` with a nice big buffer and copy a name of the right length into your array. If people want to fool around and enter a silly name that is longer than the nice big buffer, who cares whether the program truncates that and uses what's left for the next input. Don't use `gets()`. The `gets()` isn't worse: [it is dead](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used). – Weather Vane Jun 02 '21 at 19:11
  • @OmaymaBenali see [this question](https://stackoverflow.com/questions/58403537) for alternatives to `scanf`. – Steve Summit Jun 02 '21 at 19:34

1 Answers1

2

Suggestions to get your code to work as you probably expected it to.

First, provide more space for names. space is cheap. Go big (enough) in struct:

typedef struct
{
    char firstname[50];
    char lastname[50];
} person;

Second, if you have to use scanf(), make the follow on adjustments for larger buffers...

scanf("%49s", omayma.firstname);// adds room for long names, 

Or, you can get rid of scanf() altogether with what may be a better alternative:

fgets(omayma.firstname, sizeof omayma.firstname, stdin);
omayma.firstname[strcspn(omayma.firstname, "\n")] = 0;//remove newline
fgets(omayma.lastname, sizeof omayma.lastname, stdin);
omayma.lastname[strcspn(omayma.lastname, "\n")] = 0;//remove newline

printf("%s %s", omayma.lastname, omayma.lastname);



           

Third, fflush() is only used for output streams:

fflush(stdin);//delete this statement (everywhere)
ryyker
  • 22,849
  • 3
  • 43
  • 87
  • `fgets()` **is** a better alterntive - UV. Yet reading into the `.firstname` and then lopping of the potential `'\n'` does not nicely allow to read a _line_ with a first name of 49 characters, just up to 48. Unfortunately C does not provide a simple alternative. – chux - Reinstate Monica Jun 02 '21 at 19:29
  • @chux-ReinstateMonica - Thanks for those thoughts. _Proper_ name length on this site is a common enough topic. But how often does [someone with a truly long name](https://en.wikipedia.org/wiki/Hubert_Blaine_Wolfeschlegelsteinhausenbergerdorff_Sr.) actually need to enter all of it into a hard coded buffer? – ryyker Jun 02 '21 at 21:31
  • 1
    Whatever the save imit (48, 49, 50, ...., 666, ...) I tend to code for an input buffer 2x to consume the entire line and nicely alert if trimmed input exceeds the target. For line input more than 2x, kinda along [@Weather Vane](https://stackoverflow.com/questions/67810605/limited-input-from-user-in-struct/67810999?noredirect=1#comment119859225_67810605) comment, input is hostile and deserves a more severe response. – chux - Reinstate Monica Jun 02 '21 at 21:39