简体   繁体   English

我得到除以零的错误,但不知道为什么

[英]I am getting a Divide by zero error but can't figure out why

The error happens on this line: 该行发生错误:

return(rand()% sides + 1);

Here is my program: 这是我的程序:

//  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();
}

It's likely at return(rand()% sides + 1); 它很可能在return(rand()% sides + 1);

Modulo can also cause a divide-by-zero, and it has a higher precedence than addition. 模数也会导致被零除,并且比加法具有更高的优先级。 You're effectively saying return (rand() % sides) + 1; 您实际上是在说return (rand() % sides) + 1; , and if sides is 0 , then you get the exception. ,如果sides0 ,那么您将获得异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法弄清楚为什么我的反向功能变得糟糕 - Can't figure out why I am getting bad out put from my reverse function 我有一个错误与我的for循环,不能找出为什么,我有一个“预期声明”错误 - I am having an error with my for loop and can't figure out why, I have an “expected declaration” error 我在 Qt 上收到错误,无法弄清楚我哪里出错了 - I am getting an error on Qt, and can't figure out where I went wrong 我收到一个错误,无法将 int* 转换为 int,但我无法弄清楚究竟是什么原因造成的 - I am getting an error, cannot convert int* to int, but I can't figure out exactly what's causing it 无法弄清楚为什么我的变量不断恢复为零 - Can't figure out why my variable keeps reverting to zero 获取out_of_range:C ++的向量错误,但无法弄清原因 - Getting an out_of_range: vector error for c++ but can't figure out why 我收到“字符串下标超出范围错误”。 我不知道为什么 - I'm getting a “string subscript out of range error”. I can not figure out why 有人可以找出这是什么问题吗? 我收到链接错误 - can somebody figure out what is wrong in this? I am getting linking error 编译错误我想不通 - Compile error i can't figure out 为什么我得到零? - Why am I getting zero?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM