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

struct student {
    char name[30];
};
typedef struct student sv;
int main() {
    int i;
    sv basedata[50] ;
    int totalstudent;
    do
    {
        printf("How many people are there: ");
        scanf_s("%i", &totalstudent);
    } while (totalstudent<0);
    fflush(stdin);
    for ( i = 0; i < totalstudent; i++)
    {
        printf("Person %i name: ", i + 1);
        gets(basedata[i].name);
    }
}

output: How many people are there: 1 Person 1 name:
D:\hello\chuong7\x64\Debug\chuong6.exe (process 26572) exited with code 0. Press any key to close this window . .

Why I can't enter name for Person 1 name when run my program, my program pass to end and I can't gets(basedata[i].name). Sorry I'm just study English picture here

An Nguyen
  • 3
  • 2
  • https://stackoverflow.com/questions/35178520/how-to-read-parse-input-in-c-the-faq explains most of the problems in your code. – Lundin Oct 06 '21 at 14:12
  • Now the real question is what source of learning you are using, because it seems very outdated. – Lundin Oct 06 '21 at 14:26

1 Answers1

2

This program has many bugs, but the one that is specifically tripping you up is that you are calling scanf, which doesn't read input in lines, and then gets, which does. After the scanf, the carriage return that the user typed after the number is still waiting to be read. gets reads that carriage return instead of the additional input that you are trying to prompt for. Your call to fflush(stdin) does not change this; what fflush(stdin) does, varies from system to system, but it never does what people seem to expect it to do.

In addition, gets is dangerous and should never be used, scanf is broken-as-specified and should never be used, and all of the _s functions that Microsoft's compilers like to push on you are unportable and don't fix the problems Microsoft was trying to solve, and (wait for it) should never be used.

Here's what you should do instead:

  1. Put #define _CRT_SECURE_NO_WARNINGS 1 at the top of your file (before all the #includes) to make MSVC stop giving you bad advice.
  2. Use fgets, nothing else, to read all lines of input from the user.
  3. Use strtol to convert the decimal number input to a machine integer.
  4. Since you are using a statically allocated array of records, check whether the number input is bigger than the size of the array, before entering the loop.

Best of luck.

zwol
  • 135,547
  • 38
  • 252
  • 361
  • 1
    Good answer, this seems completely in line with my answer here: [Which functions from the standard library must (should) be avoided?](https://stackoverflow.com/a/46563868/584518) Though today I would probably add all `_s` functions too indeed, after the C11 "bounds checking interface" attempt turned into a fiasco. Also feel free to vote on other answers claiming "deprecated because Microsoft says so" in that same post... :) – Lundin Oct 06 '21 at 14:18
  • I've also written a post regarding Microsoft myths and `strcpy` specifically here: [Is strcpy dangerous and what should be used instead?](https://software.codidact.com/posts/281518) – Lundin Oct 06 '21 at 14:22
  • hello ZWOL, I see some Compiler Warning some function, class member, variable, or typedef that's marked deprecated in visual studio. Can you help me know about that ?, I find a lot function has deprecated but some website which I find information still used. – An Nguyen Oct 08 '21 at 10:55
  • @AnNguyen Please ask a new question about that. Make sure to show the _complete and unedited_ text of all errors. You may be asked for a "MCVE": see https://stackoverflow.com/help/mcve – zwol Oct 08 '21 at 13:40
  • @zwol how about textcolor(), textbackground() and clrscr; – An Nguyen Oct 09 '21 at 09:17
  • @zwol Can you explain to me what is non-static libraries and non-standard function? – An Nguyen Oct 09 '21 at 09:30
  • @AnNguyen Please post a new question. Asking unrelated questions in the follow-up comments to an answer is against site policy. – zwol Oct 09 '21 at 17:12