switch文のcase2の動作が正常に作動しません。printf("Enter a new employee name:");このパートがうまくいってないみたいで下記の出力のような次のprintfに繋がった出力になってしまいます。
case2を独立させて動かしてみたところ問題なく期待通りの出力を得られました。
問題のあるプログラム
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <string.h> void search(); void assing(); int main(void) { int ch; while (1) { printf("==================Employee Management System=============\n\n"); printf("1. Search\n\n"); printf("2. assign\n\n"); printf("0. Exit\n\n"); printf("========================================================\n\n"); printf("\nPlease enter your Choice:"); scanf("%d", &ch); switch (ch) { case 1: search(); break; case 2: assing(); break; case 0: exit(0); } } return 0; } void search() { FILE* fp; fp = fopen("tel.txt", "r"); char str[1000]; char id[100]; char name[100]; int key; printf("Do you want to search by 1.ID or 2.name?(Enter number 1 or 2): "); scanf("%d", &key); if (key == 1) { printf("Enter search ID: "); scanf("%s", id); while (1) { if (fgets(str, sizeof(str), fp) == NULL) break; if (strstr(str, id) != NULL) { fgets(str, sizeof(str), fp); printf("%s", str); } } } else if (key == 2) { printf("Enter search Name: "); scanf("%s", name); while (1) { if (fgets(str, sizeof(str), fp) == NULL) break; if (strstr(str, name) != NULL) { fgets(str, sizeof(str), fp); fgets(str, sizeof(str), fp); printf("%s", str); } } } fclose(fp); } void assing() { int cnt = 0; char name[50]; char id[10]; char tel[15]; char department[30]; int c; int count = 0; FILE* fp; fp = fopen("tel.txt", "a+"); while ((c = getc(fp)) != EOF) { if (c == '\n') cnt++; } count = cnt / 5 + 1; rewind(fp); printf("Enter a new employee name:"); fgets(name, sizeof(name), stdin); printf("Enter a new employee tel number:"); fgets(tel, sizeof(tel), stdin); printf("Enter a new employee department:"); fgets(department, sizeof(department), stdin); fprintf(fp, "NAME: %s", name); fprintf(fp, "ID: TP0%d\n", count); fprintf(fp, "TEL: %s", tel); fprintf(fp, "DEP: %s", department); fprintf(fp, "========================\n"); fclose(fp); }
出力
==================Employee Management System============= 1. Search 2. assign 0. Exit ======================================================== Please enter your Choice:2 Enter a new employee name:Enter a new employee tel number:12345678 Enter a new employee department:1234567
case2の独立したプログラム
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <string.h> char name[50]; char str[100]; char tel[15]; char department[30]; int c; int count = 0; int cnt; int main() { FILE* fp; fp = fopen("tel.txt", "a+"); while ((c = getc(fp)) != EOF) { if (c == '\n') cnt++; } count = cnt / 5 + 1; rewind(fp); printf("Enter a new employee name:"); fgets(name, sizeof(name), stdin); printf("Enter a new employee tel number:"); fgets(tel, sizeof(tel), stdin); printf("Enter a new employee department:"); fgets(department, sizeof(department), stdin); fprintf(fp, "NAME: %s", name); fprintf(fp, "ID: TP0%d\n", count); fprintf(fp, "TEL: %s", tel); fprintf(fp, "DEP: %s", department); fprintf(fp, "========================\n"); fclose(fp); return 0; }
Enter a new employee name:xxx Enter a new employee tel number:07000000000 Enter a new employee department:asia
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/02/21 07:42
2021/02/21 07:44
2021/02/21 08:17
2021/02/21 08:23
2021/02/21 08:29
2021/02/21 08:38
2021/02/21 08:48
2021/02/21 08:56