如何停止c++程序?

  • 本文关键字:程序 c++ 何停止 c++
  • 更新时间 :
  • 英文 :


我编写了一个程序,提示用户猜测1-10之间的数字(我已经将其编程为产生一个随机数),如果用户猜对的数字与生成的随机数相同,则打印"恭喜",否则提示用户再试一次。但我想让用户在一段时间后(游戏邦注:如Game Over)停止回答问题。但是提示不断出现,我尝试在我的while循环中使用中断,但它不起作用,我还尝试使用退出函数,这实际上阻止了程序运行,但它在回答2次后停止了它,这不是我想要的。

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
int a,b,c,d,e,f;
//  generate a random number, prompt the user for any number if the users guess is in line with the random number generated the user wins else try again
//generate a random number between 1 - 10 store it in a variable
cout << "Random Number Generator n n";
srand(time(0));
for(int i = 1; i == 1; i++){
a =  1+(rand() % 10);
cout << a << endl;
}
//prompt the user for numbers ranging from 1 - 10
cout << "type in a number from (1 - 10)n";
cin >> b;
c++;
//check if the number is the same as the random number
//this checks to see if the user gets the question, else it continues running till he gets it
while(a != b){
cout << "You're incorrect!n";
cout << "type in a number from (1 - 10)n";
cin >> b;

while(b <= 3){
exit(3);
}

}
//print result
if(a == b){
cout << "congratulations";
}
return 0;
}

我怎样才能使它工作?

您可以计算用户回答的次数,并在执行了所需次数后停止。

//prompt the user for numbers ranging from 1 - 10
cout << "type in a number from (1 - 10)n";
cin >> b;
int answer_count = 1; // variable to count answers (there is already 1 answer here)
const int max_attempts = 10; // number of attempts the user has
//check if the number is the same has the random number
//this checks to see if the user gets the question, else it continues running till he gets it
while(a != b){
cout << "You're incorrect!n";
cout << "type in a number from (1 - 10)n";
cin >> b;
answer_count++; // count this new answer
if (answer_count >= max_attempts){ // check if the count reached the "certain amount of time"
break; // exit from this loop
}   
}

或者,您也可以给用户一定的时间来猜测。例如,10秒。这可以使用c++chrono库轻松实现:

#include <chrono>
#include <iostream>
#include <random>
int main(int argc, char **argv)
{
const int max_time = 10; // seconds
const int min_secret = 1;
const int max_secret = 10;

// This generates a random number between min_secret and max_secret using the STL random library
std::random_device r;
std::default_random_engine e(r());
std::uniform_int_distribution<int> uniform_dist(min_secret, max_secret);
int secret = uniform_dist(e);
auto start = std::chrono::system_clock::now();
int guess;
do {
std::cout << "Type a number from (1 - 10)n";
std::cin >> guess;
if (guess == secret)
break;
std::cout << "Your guess is incorrect!n";
// See if the time elapsed since the start is within max_time
auto now = std::chrono::system_clock::now();
auto elapsed_time = std::chrono::duration_cast<std::chrono::seconds>(now - start);
if (elapsed_time.count() > max_time) {
std::cout << "You ran out of time.n";
exit(0);
} else {
std::cout << "You still have " << max_time - elapsed_time.count() << " seconds leftn";  
}
} while (guess != secret);
std::cout << "Your guess was correct, congratulations!";
}

注意,时间检查只在用户尝试猜测之后执行,所以如果时间限制是10秒,用户等待30秒才输入,它仍然允许。要在c++中使用计时器完全终止程序,您可以使用thread库生成第二个线程来处理经过的时间,或者甚至使用基于中断的方案(参见https://stackoverflow.com/a/4001261/15284149定时器示例)。

还要注意的是,用户输入并没有经过处理,如果用户输入的不是数字,那么你的程序就会有未定义的行为。

您需要在代码中修复几件事情:

  1. 变量c保持未初始化,并在以后增加以不使用。删除这个。注意d,e,f也没有使用。

  2. 在循环中:

    for(int i = 1; i == 1; i++) {
    a = 1 + (rand() % 10);
    cout << a << endl;
    }
    

    你已经告诉编译器迭代到i == 1,增加1,它只做一次-你可能不想做,但i < 10

    同样,您不是使用数组来存储这10个随机数,而是最后一个。您需要创建一个包含10个房间的数组,并将其分配给每个房间:

    int a[10];
    // Since the array index begins at zero
    for (int i = 0; i < 10; i++) {
    a[i] = (rand() % 10) + 1;
    cout << a[i] << endl;
    }
    
  3. 在成功赋值之后,是时候引入一个随机选择的索引作为正确答案(它应该放在while循环之前):

    // To choose the random index
    int shuffle = a[rand() % 10];
    

    同时,替换祝贺语句:

    // It was a == b previously
    if (shuffle == b)
    cout << "congratulations";
    
  4. 最后,要在三次错误尝试后退出,替换while循环:

    int count = 0;
    while (shuffle != b) {
    count++;
    cout << "You're incorrect!n";
    cout << "type in a number from (1 - 10)n";
    cin >> b;
    if (count == 2) {
    cout << "Game Over" << endl;
    exit(0);
    }
    }
    

最新更新