Visual Studio ignores gets s
Has anyone ever encountered a problem where you need to write to the buffer from the console using gets_s (buf, size) or fgets (buf,size,/stream/), but Visual Studio just skipped them and didn't read anything? If you put two gets_s in a row, it works. How to count a string with spaces? The C language. In the line of the editStr function: gets_s(buf, LINE); Program code:
static void editInt(int *num) {
int value;
scanf("%d", &value);
fflush(stdin);
*num = value;
}
static void editStr(char *line) {
char buf[50];
fflush(stdin);
gets_s(buf, LINE);
buf[strcspn(buf, "\n")] = '\0';
strcpy(line, buf);
}
void editingList(List **list) {
printf("\nWould you like to edit list?\n");
int num,fl;
if (char c = _getch() == 'y') {
printf("\nEnter surname of the element you want to edit: \n");
char buf[20];
scanf("%s", buf);
fflush(stdin);
Human *temp = (*list)->first;
while (temp) {
if (!strcmp(temp->surname, buf)) {
while(1){
printf("\nWhich parameter would you like to change?\n");
printf("\n(1)Surname\n(2)Name\n(3)Patronymic\n(4)Gender\n(5)Birthday\n(6)Month of birthday\n(7)Year of birthday\n(8)Height\n(9)Weight\n(10)City\n(11)Interests\n(12)Bad habits\n(13)Personal text\n(14)Min preferred age\n(15)Max preferred age\n(16)Preferred bad habbits\n Press other button to return to menu\n");
int decision;
scanf("%d", &decision);
switch (decision) {
case 1:
printf("\nEnter new surname\n");
editStr(temp->surname);
break;
case 2:
printf("\nEnter new name\n");
editStr(temp->name);
break;
case 3:
printf("\nEnter new patronymic\n");
editStr(temp->patronymic);
break;
case 4:
printf("\nEnter new gender\n");
editStr(temp->gender);
break;
case 5:
printf("\nEnter new day of birthday\n");
editInt(&temp->dateOfBirth.day);
break;
case 6:
printf("\nEnter new month of birthday\n");
editInt(&temp->dateOfBirth.month);
break;
case 7:
printf("\nEnter new year of birthday\n");
editInt(&temp->dateOfBirth.year);
break;
case 8:
printf("\nEnter new height\n");
editInt(&temp->height);
break;
case 9:
printf("\nEnter new weight\n");
editInt(&temp->weight);
break;
case 10:
printf("\nEnter new city\n");
editStr(temp->city);
break;
case 11:
printf("\nEnter new interests\n");
editStr(temp->interests);
break;
case 12:
printf("\nEnter new bad habits\n");
editStr(temp->badHabits);
break;
case 13:
printf("\nEnter new selfinfo\n");
editStr(temp->textMyself);
break;
case 14:
printf("\nEnter new preferred min age\n");
editInt(&temp->prefAgeMin);
break;
case 15:
printf("\nEnter new preferred max age\n");
editInt(&temp->prefAgeMax);
break;
case 16:
printf("\nEnter new preferred bad habits\n");
editStr(temp->prefBadHab);
break;
default:
myFree(&temp);
return;
}
system("cls");
}
}
temp = temp->next;
}
}
}
0
2 answers
Everything is clear.
After reading scanf
, at least a newline character remains in the buffer, which is read as a separate (empty) string.
Reset the string to the end after reading scanf
- but at least just reading to nowhere until the \n
:
while (fgetc(stdin) != '\n');
1
Author: Harry, 2018-04-21 14:18:03
It helped me:
while (char c = getchar() != '\n' && c != EOF);
0
Author: kravien, 2018-04-21 15:43:54