gpt4 book ai didi

c++ - 执行while循环后程序崩溃

转载 作者:行者123 更新时间:2023-11-28 04:56:40 25 4
gpt4 key购买 nike

代码在执行while循环后关闭,不执行最后2条printf语句。我不知道出了什么问题.. 在循环运行了选定的时间后,程序就关闭了。

#include <stdio.h> 
int main()
{
int numberofq;
int questionansc;
int questionansic;
int counter;
int answer;

numberofq = 0;
questionansc = 0;
questionansic = 0;
counter = 0;
answer = 0;

while(numberofq <1 || numberofq >5)
{
printf("Hello enter the amount of questions you want between 1 and 5 \n");
scanf("%d", &numberofq);
} // End While

//Program runs until the counter is less than users wanted question number.
while (counter < numberofq)
{
//Question 1
printf("Question 1. what is 2+2? \n");
scanf("%d" , &answer);

//if users answer is equal to 4.
if (answer == 4)
{
printf("You entered %d, you are correct\n", answer);
questionansc = questionansc +1;
} //End If

//If the answer is not equal to 4.
else
{
printf("You entered %d, you are wrong correct answer is 4 \n", answer);
questionansic = questionansic +1;
} // End Else

counter = counter +1;
//End Question 1.

} //End While

printf("You got %d questions correct \n" , questionansc);
printf("You got %d questions wrong" , questionansic);
flushall();

return 0;
} // End Main`

最佳答案

它实际上会打印它们,然后然后退出,但它退出的速度太快了,你没有机会看到它。
您可以在 Windows 上使用 system("pause") 暂停执行,但这被认为是不好的做法。您可以使用 getch() 或其他东西,但您也可以简单地从现有的 CMD/终端调用程序,这样在程序完成后输出将保留在那里。

关于c++ - 执行while循环后程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46993770/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com