gpt4 book ai didi

c++ - 我收到除以零的错误,但无法弄清楚原因

转载 作者:行者123 更新时间:2023-11-30 00:54:00 28 4
gpt4 key购买 nike

错误发生在这一行:

return(rand()% sides + 1);

这是我的程序:

//  main.cpp
// dcChecker
//
// Created by Max Huber on 2/21/13.
// Copyright (c) 2013 S0URC3 Studioss. All rights reserved.
//

#include <iostream>
#include <ctime>
#include <cstdlib>

int rollDice(int);
int diceCheck(int diceMod, int dci, int roll);
void clearScreen();
void pressEnter();
void dcFunction();


int main()
{
dcFunction();
clearScreen();
main();
}

// Roll a n sided die
int rollDice(int sides)
{
//int gen;

//create time variable
time_t timer;

//set time variable to current time in seconds
time(&timer);

//seed random number generator using current time in seconds
srand(timer);

//generate random number
//gen = (rand()% sides + 1);

return(rand()% sides + 1);
}

// Check if the dice roll + modifier (if any) is greater than the check value
int diceCheck(int diceMod, int dci, int roll)
{
if(roll + diceMod >= dci)
{
return 0;
}
else
return 1;
}

void clearScreen()
{
#ifdef WINDOWS
std::system ( "CLS" );

#else
// Assume POSIX
std::system ( "clear" );
#endif
}
void pressEnter()
{
std::cin.sync();
std::cout << std::endl << "Press ENTER to continue..." << std::endl;
std::cin.get();
}

void dcFunction()
{
//define variables
int neededRoll;
int diceMod;
int sides;
int roll;

//get user inputs
std::cout << "How many sides: ";
std::cin >> sides;
std::cout << "Enter needed roll: ";
std::cin >> neededRoll;
std::cout << "Enter dice modifiers: ";
std::cin >> diceMod;

//store roll in variable so it can be gotten later
roll = rollDice(sides);

//check if your roll was better than the dice check

//success check
if(diceCheck(diceMod, neededRoll, roll) == 0)
{
std::cout << "\nSUCCESS\n\n"
<< "You rolled a " << sides << " sided die\n"
<< "You rolled: " << roll << " + " << diceMod << " = " << roll + diceMod << "\n"
<< "You needed: " << neededRoll << "\n";
}

//otherwise you fail
else
{

std::cout << "\nFAILURE!!!\n\n"
<< "You rolled a " << sides << " sided die\n"
<< "You rolled: " << roll << " + " << diceMod << " = " << roll + diceMod << "\n"
<< "You needed: " << neededRoll << "\n";
}
pressEnter();
}

最佳答案

可能在 return(rand()% sides + 1);

模也可以导致被零除,并且它的优先级高于加法。你实际上是在说 return (rand() % sides) + 1;,如果 sides0,那么你会得到异常。

关于c++ - 我收到除以零的错误,但无法弄清楚原因,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15075690/

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