C 程序:Do-while 循环到 运行 程序再次无法正常工作
C Program: Do-while loop to run program again not working properly
我的程序旨在对文本文件中的记录进行排序,但我的主要问题是让 main 函数询问用户是否愿意 运行 程序再次读取和写入另一个文件。我现在的程序,当用户再次输入1到运行时,跳过第一题进入程序阅读。这是为什么?感谢您的帮助!这只是我的主要功能:该程序仅在第一个 运行.
期间编译
int main()
{
int fieldCount = 0;
int lineCount = 0;
char file[STR_LEN];
char filepath[STR_LEN];
char** fields = NULL;
char** lines = NULL;
int recCount = 0;
Person **sortedRecs = NULL;
int x;
do {
printf("Enter path of the file to read: ");
gets(filepath);
printf("Enter path to copy to: ");
gets(file);
fields = readFieldsDynamic(filepath, &fieldCount);
lines = readLinesDynamic(filepath, &lineCount);
recCount = getPersons(fields, fieldCount, &sortedRecs);
if (recCount && lines && sortedRecs && (recCount <= lineCount)) {
writeRecsToFile(file, sortedRecs, recCount, lines, lineCount);
printf("Sorted records are written in %s\n", file);
}
if (fields) {
freePointerToChars(fields, fieldCount);
}
if (lines) {
freePointerToChars(lines, lineCount);
}
if (sortedRecs) {
freeRecs(sortedRecs, recCount);
}
printf("Enter 1 to run program again: ");
scanf("%d%*c", &x);
} while (x == 1);
return 0;
}
你可以做的是添加一个 while 循环到 "eat up" 留在 stdin
流中的所有换行符,以防止下一个 getchar
不阻塞真正的用户输入。
while ((ch=getchar()) != EOF && ch != '\n')
;
另外请不要在您的代码中使用 gets
。请尝试 fgets
。
此答案基于您的输出,如下所示:
Enter path of the file to read: /my/input/here/
Enter path to copy to: /blah/copy/here/
Enter 1 to run program again: 1
Enter path of the file to read:
Enter path to copy to: help/I/cant/type/in/the/above/field
我的理解是你的程序可能在循环之间携带换行符。
我在 C++ 中遇到过类似的问题,在输入后放置 cin.get()
修复了它。我不确定 C 等价物。
您需要在 %c 之前保留一个 space。
int main()
{
char ch;
do
{
something();
printf("\nDo you want to continue?");
scanf(" %c", &ch);
}while(ch=='y');
return 0;
}
我的程序旨在对文本文件中的记录进行排序,但我的主要问题是让 main 函数询问用户是否愿意 运行 程序再次读取和写入另一个文件。我现在的程序,当用户再次输入1到运行时,跳过第一题进入程序阅读。这是为什么?感谢您的帮助!这只是我的主要功能:该程序仅在第一个 运行.
期间编译int main()
{
int fieldCount = 0;
int lineCount = 0;
char file[STR_LEN];
char filepath[STR_LEN];
char** fields = NULL;
char** lines = NULL;
int recCount = 0;
Person **sortedRecs = NULL;
int x;
do {
printf("Enter path of the file to read: ");
gets(filepath);
printf("Enter path to copy to: ");
gets(file);
fields = readFieldsDynamic(filepath, &fieldCount);
lines = readLinesDynamic(filepath, &lineCount);
recCount = getPersons(fields, fieldCount, &sortedRecs);
if (recCount && lines && sortedRecs && (recCount <= lineCount)) {
writeRecsToFile(file, sortedRecs, recCount, lines, lineCount);
printf("Sorted records are written in %s\n", file);
}
if (fields) {
freePointerToChars(fields, fieldCount);
}
if (lines) {
freePointerToChars(lines, lineCount);
}
if (sortedRecs) {
freeRecs(sortedRecs, recCount);
}
printf("Enter 1 to run program again: ");
scanf("%d%*c", &x);
} while (x == 1);
return 0;
}
你可以做的是添加一个 while 循环到 "eat up" 留在 stdin
流中的所有换行符,以防止下一个 getchar
不阻塞真正的用户输入。
while ((ch=getchar()) != EOF && ch != '\n')
;
另外请不要在您的代码中使用 gets
。请尝试 fgets
。
此答案基于您的输出,如下所示:
Enter path of the file to read: /my/input/here/
Enter path to copy to: /blah/copy/here/
Enter 1 to run program again: 1
Enter path of the file to read:
Enter path to copy to: help/I/cant/type/in/the/above/field
我的理解是你的程序可能在循环之间携带换行符。
我在 C++ 中遇到过类似的问题,在输入后放置 cin.get()
修复了它。我不确定 C 等价物。
您需要在 %c 之前保留一个 space。
int main()
{
char ch;
do
{
something();
printf("\nDo you want to continue?");
scanf(" %c", &ch);
}while(ch=='y');
return 0;
}