简体   繁体   English

分裂和cout的C ++问题

[英]C++ problems with dividing and cout

I am doing an assignment for Computer Science. 我正在做计算机科学的任务。 We have to make a batting average program. 我们必须制定击球平均计划。 I have it working so that it will calculate the hits, outs, singles, etc. but when it comes to making it calculate the batting average, it's coming up with 0.000. 我有它的工作,它将计算击中,出局,单打等,但当涉及到计算击球率时,它会提高0.000。 I have no clue why, I've done a ton of google searching, tried making the variable double and float, etc. here is the code: 我不知道为什么,我已经做了大量的谷歌搜索,尝试使变量双和浮动等,这里是代码:

#include <iostream>
#include <iomanip>
#include <cstdlib>

using namespace std;

int main(){
const int MAX_TIMES_AT_BAT = 1000;
int hits = 0, timesBatted = 0, outs = 0, walks = 0, singles = 0, doubles = 0, triples = 0, homeRuns = 0;
float battingAverage = 0.0, sluggingPercentage = 0.0;

for(int i = 0; i < MAX_TIMES_AT_BAT; i++){
    int random = rand() % 100 +1;

    if(random > 0 && random <= 35){ 
        outs++;
    }else if(random > 35 && random <= 51){
        walks++;
    }else if(random > 51 && random <= 71){
        singles++;
        hits++;
    }else if(random > 71 && random <= 86){
        doubles++;
        hits++;
    }else if(random > 86 && random <= 95){
        triples++;
        hits++;
    }else if(random > 95 && random <= 100){
        homeRuns++;
        hits++;
    }else{
        cout << "ERROR WITH TESTING RANDOM!!!";
        return(0);
    }
    timesBatted++;
}
    cout << timesBatted << " " << hits << " " << outs << " " << walks << " " << singles << " " << doubles << " " << triples << " " << homeRuns << endl;


battingAverage = (hits / (timesBatted - walks));
sluggingPercentage = (singles + doubles * 2 + triples * 3 + homeRuns*4) / (timesBatted - walks);

cout << fixed << setprecision(3) << "Batting Average: " << battingAverage << "\nSlugging Percentage: " << sluggingPercentage << endl;


return 0;
}

Any help would be great! 任何帮助都会很棒! What is going wrong??? 怎么了? I calculated it, and the batting average should be 0.5646 and the slugging percentage should be 1.0937. 我计算了它,击球率应该是0.5646,击球率应该是1.0937。 what its showing is 0.0000, and 1.0000. 它的显示是0.0000和1.0000。 Thanks in advance!!! 提前致谢!!!

You are performing integer divisions. 您正在执行整数除法。 Explicitly cast at least one of the operands to a double . 将至少一个操作数显式地转换为double For instance: 例如:

battingAverage = (static_cast<float>(hits) / (timesBatted - walks));

Same thing for the assignment to sluggingPercentage . sluggingPercentage的任务也是如此。

Simple as dividing an int by an int is another int . 简单地将int除以int是另一个int Simply cast one to double . 简单地将其中一个double

For example, 例如,

battingAverage = static_cast<double>(hits) / (timesBatted - walks)
sluggingPercentage = static_cast<double>(singles + doubles * 2 + triples * 3 + homeRuns*4) / (timesBatted - walks)

Always use C++ casts ( static_cast<double>() ) rather than C casts (double)() , as the compiler will give you more hints about when you're doing something wrong. 始终使用C ++强制转换( static_cast<double>() )而不是C强制转换(double)() ,因为编译器会为您提供有关何时出错的更多提示。

PS Don't hate C++! PS不要讨厌C ++! :( Show it a little love and it will love you back! :(显示它有点爱,它会爱你回来!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM