C - Do While 添加额外的步骤
C - Do While adding extra Step
当我 运行 下面的代码时,它可以正确执行,但在我可以再次输入之前添加了额外的迭代。不知道为什么。我将使用它来将父进程派生并传递给子进程。该步骤将在用户输入进程数后进行。
代码:
#include <stdio.h>
int main(void) {
int num_processes;
int running = 1;
do {
printf ("How many processes do you want? (1, 2 or 4) \n");
num_processes = getchar();
num_processes -= '0';
if(num_processes == 1 || num_processes == 2 || num_processes == 4){
int temp = num_processes - 1;
printf("\n1 Parent and %d child processes\n", temp);
printf("----------------------------------\n");
running = 0;
} else {
printf("Invalid Input, please try again.\n");
}
} while(running == 1);
// Do important stuff
return(0);
}
输出:
How many processes do you want? (1, 2 or 4)
3
Invalid Input, please try again.
How many processes do you want? (1, 2 or 4)
Invalid Input, please try again.
How many processes do you want? (1, 2 or 4)
2
1 Parent and 1 child processes
----------------------------------
想想当你第一次输入时会发生什么。您键入 3,然后按 Enter。
现在,在标准输入缓冲区中,有两个项目等待使用,3
和 \n
(即 Enter)。
你第一次进入循环体,消耗了3个。现在下一个等待 \n
...
当你再次(第二次)进入循环体时,标准输入缓冲区要读取的下一个字符是\n
,这就是第二次getchar()
愉快的returns给你。
这是一个快速修复:
do {
printf ("How many processes do you want? (1, 2 or 4) \n");
// Eat trailing newline character, if any
do {
num_processes = getchar(); // read a character
} while(num_processes == '\n'); // if it was the newline character, repeat
// until `num_processes` is not the newline character
// Continue with your logic..
num_processes -= '0';
输出:
How many processes do you want? (1, 2 or 4)
3
Invalid Input, please try again.
How many processes do you want? (1, 2 or 4)
2
1 Parent and 1 child processes
----------------------------------
PS:正如@Barmar 评论的那样,如果您想阅读行,请使用 fgets()
. Read more here:
当我 运行 下面的代码时,它可以正确执行,但在我可以再次输入之前添加了额外的迭代。不知道为什么。我将使用它来将父进程派生并传递给子进程。该步骤将在用户输入进程数后进行。
代码:
#include <stdio.h>
int main(void) {
int num_processes;
int running = 1;
do {
printf ("How many processes do you want? (1, 2 or 4) \n");
num_processes = getchar();
num_processes -= '0';
if(num_processes == 1 || num_processes == 2 || num_processes == 4){
int temp = num_processes - 1;
printf("\n1 Parent and %d child processes\n", temp);
printf("----------------------------------\n");
running = 0;
} else {
printf("Invalid Input, please try again.\n");
}
} while(running == 1);
// Do important stuff
return(0);
}
输出:
How many processes do you want? (1, 2 or 4)
3
Invalid Input, please try again.
How many processes do you want? (1, 2 or 4)
Invalid Input, please try again.
How many processes do you want? (1, 2 or 4)
2
1 Parent and 1 child processes
----------------------------------
想想当你第一次输入时会发生什么。您键入 3,然后按 Enter。
现在,在标准输入缓冲区中,有两个项目等待使用,3
和 \n
(即 Enter)。
你第一次进入循环体,消耗了3个。现在下一个等待 \n
...
当你再次(第二次)进入循环体时,标准输入缓冲区要读取的下一个字符是\n
,这就是第二次getchar()
愉快的returns给你。
这是一个快速修复:
do {
printf ("How many processes do you want? (1, 2 or 4) \n");
// Eat trailing newline character, if any
do {
num_processes = getchar(); // read a character
} while(num_processes == '\n'); // if it was the newline character, repeat
// until `num_processes` is not the newline character
// Continue with your logic..
num_processes -= '0';
输出:
How many processes do you want? (1, 2 or 4)
3
Invalid Input, please try again.
How many processes do you want? (1, 2 or 4)
2
1 Parent and 1 child processes
----------------------------------
PS:正如@Barmar 评论的那样,如果您想阅读行,请使用 fgets()
. Read more here: