[英]How do I get the printf at the end of do while loop to be executed? it just skips it
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int min;
do
{
printf("Minutes: ");
min = get_int();
}
while(min <= 0);
return 0;
printf("Bottles: %i\n", min * 12);
}
OK i am really new at c.
好的,我真的很新c。 The code should work like this: If the user types in a negative integer it will continue to ask for the integer otherwise it should run the printf statement at the end but that is not happening.
代码应该像这样工作:如果用户键入一个负整数,它将继续请求整数,否则它应该在结束时运行printf语句,但这不会发生。 Can someone help me please?
有人能帮助我吗?
Move the return 0;
移动
return 0;
line after the printf
function, because the return 0;
printf
函数后的行,因为return 0;
let your application finish. 让你的申请完成。 --> So no
printf("Bottles: %i\\n", min * 12);
- >所以没有
printf("Bottles: %i\\n", min * 12);
would have been called. 会被称为。
int main(void)
{
int min;
do
{
printf("Minutes: ");
min = get_int();
}
while(min <= 0);
printf("Bottles: %i\n", min * 12);
// Change the position of return
return 0;
}
As you are new in C, 你是C的新人,
return 0;
this means it will return 0 value after executing the program up to it and will exit the main() function. 这意味着它在执行程序之后将返回0值并退出main()函数。 Nothing will be executed after return 0;
返回0后不会执行任何操作;
So for your case, the line printf("Bottles: %i\\n", min * 12);
所以对于你的情况,行
printf("Bottles: %i\\n", min * 12);
is not executed because this line is written after the return 0; 未执行,因为此行在返回0后写入; statement.
声明。
So put that line before return 0; 所以在返回0之前放入该行; and your code should run fine !
你的代码应该运行正常!
change the position of 改变立场
return 0; 返回0;
from serveral use of return,one is return use to break function 从几个使用返回,一个是返回使用打破功能
here your return 0; 在这里你的回报0; statement breaking your main method and skiping following statements.
声明破坏您的主要方法并滑动以下语句。
From now, make a habit to use return 0; 从现在开始,养成使用return 0的习惯; in the end of code
在代码的最后
like this 像这样
int main(void)
{ {
int min;
do
{
printf("Minutes: ");
min = get_int();
}
while(min <= 0);
printf("Bottles: %i\n", min * 12);
return 0; // always use before the right parenthesis of main function
} }
Happy Coding 快乐的编码
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.