这里是学生程序员。每次我运行程序时,编译器都会终止并提示存在浮点异常。这是我的代码供参考:
#include <iostream>
#include <cmath>
#include <vector>
#include <cstdlib>
#include <string>
using namespace std;
int guesses = 3;
int quantity;
int discard_value;
vector<int> void_numbers(quantity);
int points = 10 + quantity * 5;
bool unique = 1;
int target = rand() % quantity + 1;
string check_guess(int guess)
{
for (int i = 0; i < void_numbers.size(); i++)
{
if (guess == void_numbers[i])
{
return "You guessed a void number. You lose.";
}
}
if (guess < target && guesses != 0)
{
return "Your guess is lower than the number.";
}
else if (guess > target && guesses != 0)
{
return "Your guess is higher than the number.";
}
else if (guess == target && guesses != 0)
{
return "You guessed the number. You win " + to_string(points) + " points.";
}
else if (guesses == 0)
{
return "You ran out of guesses. You lose.";
}
else
{
return "Invalid input. Please try again.";
}
}
void test_input()
{
cout << "How many void numbers would you like to add? (1-19): " << endl;
while(invalidinput)
{
cin >> quantity;
if (quantity < 1 || quantity > 19 || cin.fail())
{
cin.clear();
cin.ignore();
cin >> discard_value;
cout << "Invalid input. Please try again." << endl;
}
else
{
break;
}
}
while (void_numbers.size() < quantity)
{
int void_number = rand() % quantity + 1;
for (int i = 0; i < void_numbers.size(); i++)
{
if (void_numbers[i] == void_number)
{
unique = 0;
break;
}
}
if (unique)
{
void_numbers.push_back(void_number);
}
}
for (int i = 0; i < void_numbers.size(); i++)
{
for (int j = i + 1; j < void_numbers.size(); j++)
{
if (void_numbers[i] == void_numbers[j])
{
void_numbers.erase(void_numbers.begin() + j);
j--;
int temp_number = rand() % quantity + 1;
for (int i = void_numbers.size() - 1; i >= 0; i--)
{
if (void_numbers[i] == temp_number || void_numbers[i] == target)
{
void_numbers.erase(void_numbers.begin() + j);
}
}
}
}
}
for (int i = 0; i < void_numbers.size(); i++)
{
cout << void_numbers[i] << endl; //currently have this here to test if the void numbers are being generated correctly
cout << target;
}
}
int main()
{
srand(time(0));
int quantity;
int target = rand() % quantity + 1;
char play;
cout << "Welcome to the Numbers Game." << endl;
cout << "1. In this game you have 3 guesses to guess a randomly-generated number between 1 and 20. The game will inform you if your guess is higher or lower than the random number." << endl;
cout << "2. If you guess the number without running out of guesses, you receive 10 points. If you want more points, you have the option of inputting a quantity of void numbers." << endl;
cout << "3. Void numbers make you instantly lose the game if you guess one of them, but each additional void number adds 5 additional points to your total score." << endl;
cout << "4. If you use all your guesses and don't guess the number, you lose the game." << endl;
test_input();
}
整个程序是一个用户猜数字的游戏。在游戏开始之前,用户可以输入一定数量的无效数字,如果猜中其中一个数字,则用户将输掉游戏。 test_input 函数应该用于在向量中生成从 1 到用户想要的任何数量(最多 19)的数字,而无需任何重复数字。向量中的数字也不应该与目标变量中存储的数字匹配。
真的要避免全局变量。
int quantity;
这是一个没有初始化器的变量定义。当它出现在块作用域时,它的值将是不确定的。相反,这里我们将它放在文件范围内,并且它是一个全局变量,其值初始化为零。
int target = rand() % quantity + 1;
这通过计算
target
来初始化另一个全局变量 rand() % quantity +1
。由于 quantity
为零,因此除以零,这是未定义的行为,任何事情都可能发生。在您的特定情况下,它可能会导致除零浮点异常信号,但这不能保证。