如何修复此 do-while 循环,以便控制台程序在自动关闭之前提示用户?
How do I fix this do-while loop so console program prompts the user before automatically closing?
当我 运行 编写此代码时,一切 运行 都很顺利,直到最后一部分。在询问 "Would you like to repeat?" 问题后,控制台不会提示用户回答,而是结束编程。
如何编辑 do-while 循环的代码以便提示用户回答而不是自动关闭程序?我觉得这是格式说明符的问题,我对此很陌生并且一直遇到问题。谢谢!
#include <stdio.h>
int main(void)
{
double num1, num2;
char operation, repeat = "y";
printf("This is a calculator.");
do {
printf("\nWould you like to multiply(*), divide(/), add(+) or subtract(-) the two numbers you will soon input? \n");
scanf("%c", &operation);
printf("Please enter the first number you would like to deal with. \n");
scanf("%lf", &num1);
printf("And the second?\n");
scanf("%lf", &num2);
switch (operation)
{
case '*':
printf("The product of %1.2lf and %1.2lf is %1.2lf.\n",
num1, num2, num1 * num2);
break;
case '/':
printf("The quotient of %1.2lf and %1.2lf is %1.2lf.\n",
num1, num2, num1 / num2);
break;
case '+':
printf("The sum of %1.2lf and %1.2lf is %1.2lf.\n",
num1, num2, num1 + num2);
break;
case '-':
printf("The difference of %1.2lf and %1.2lf is %1.2lf.\n",
num1, num2, num1 - num2);
break;
}
printf("Would you like to repeat?(y/n)\n");
scanf("%c", &repeat);
} while (repeat == "y" || repeat == "Y");
}
stdin
中有上次输入操作留下的换行符。你的
scanf("%c",&repeat);
读取该换行符,因为转换说明符 %c
不会跳过空白字符。使用
scanf(" %c", &repeat);
跳过前导空格。
在 C 和 C++ 中,单个字符用单引号引起来。
char ch;
ch == "A";
会将 ch
的值与字符串文字 "A"
.
的地址进行比较
所以...
while(repeat=="y"||repeat=="Y");
~>
while(repeat == 'y' || repeat == 'Y');
和
char operation, repeat="y";
~>
char operation, repeat = 'y';
你的编译器应该已经警告过你了。如果不是,您应该提高编译器的警告级别。
您可能还想检查未定义的被零除。
最后一件事:printf()
不关心 %lf
中的长度说明符 l
,因为默认参数传播,它与 %f
相同。调用带有可变数量参数的函数中的 float
参数在传递给函数之前总是转换为 double
。所以 printf()
.
只有 %f
PS:
正如 Cacahuete Frito 在评论中所说:
You should check the return value of scanf()
是的,你应该。永远不要相信用户。
当我 运行 编写此代码时,一切 运行 都很顺利,直到最后一部分。在询问 "Would you like to repeat?" 问题后,控制台不会提示用户回答,而是结束编程。
如何编辑 do-while 循环的代码以便提示用户回答而不是自动关闭程序?我觉得这是格式说明符的问题,我对此很陌生并且一直遇到问题。谢谢!
#include <stdio.h>
int main(void)
{
double num1, num2;
char operation, repeat = "y";
printf("This is a calculator.");
do {
printf("\nWould you like to multiply(*), divide(/), add(+) or subtract(-) the two numbers you will soon input? \n");
scanf("%c", &operation);
printf("Please enter the first number you would like to deal with. \n");
scanf("%lf", &num1);
printf("And the second?\n");
scanf("%lf", &num2);
switch (operation)
{
case '*':
printf("The product of %1.2lf and %1.2lf is %1.2lf.\n",
num1, num2, num1 * num2);
break;
case '/':
printf("The quotient of %1.2lf and %1.2lf is %1.2lf.\n",
num1, num2, num1 / num2);
break;
case '+':
printf("The sum of %1.2lf and %1.2lf is %1.2lf.\n",
num1, num2, num1 + num2);
break;
case '-':
printf("The difference of %1.2lf and %1.2lf is %1.2lf.\n",
num1, num2, num1 - num2);
break;
}
printf("Would you like to repeat?(y/n)\n");
scanf("%c", &repeat);
} while (repeat == "y" || repeat == "Y");
}
stdin
中有上次输入操作留下的换行符。你的
scanf("%c",&repeat);
读取该换行符,因为转换说明符 %c
不会跳过空白字符。使用
scanf(" %c", &repeat);
跳过前导空格。
在 C 和 C++ 中,单个字符用单引号引起来。
char ch;
ch == "A";
会将 ch
的值与字符串文字 "A"
.
所以...
while(repeat=="y"||repeat=="Y");
~>
while(repeat == 'y' || repeat == 'Y');
和
char operation, repeat="y";
~>
char operation, repeat = 'y';
你的编译器应该已经警告过你了。如果不是,您应该提高编译器的警告级别。
您可能还想检查未定义的被零除。
最后一件事:printf()
不关心 %lf
中的长度说明符 l
,因为默认参数传播,它与 %f
相同。调用带有可变数量参数的函数中的 float
参数在传递给函数之前总是转换为 double
。所以 printf()
.
%f
PS: 正如 Cacahuete Frito 在评论中所说:
You should check the return value of
scanf()
是的,你应该。永远不要相信用户。