私はタイトルが漠然としていますが、サイコロをするプログラムを作っています。(あなたは永遠にコースで見たように)私は混乱しています。ロールからの負の数値結果の印刷
私は6面ダイを1面だけ回転させ、各面の発生をカウントしています。私の結果はリストフォームのようにidをどのように表示するのですか?しかし、プログラムに30回言うと言うと、私が受け取る結果は-858993460となります。私はそれがどこから来るのか分からず、誰かが私がどこに間違っていたのか、私のヘッダーとソースファイルのコードを投稿して、私がコメントを残した特定の領域では不完全であるというアイデアを私に与えてくれることを願っています。
//Dice.h
#pragma once
#include <iostream>
using namespace std;
class aDie {
public:
aDie();
int Roll();
int getRoll();
void setRolls(int r);
int getTotal();
void setTotal(int t);
int numOnes = 0, numTwos = 0, numThrees = 0, numFours = 0, numFives = 0, numSixes = 0;
int rollTotal();
int Display();
int numRolls;
int allRolls = 0;
int randomRoll;
protected:
int roll;
int totalRoll;
int DisplaySomeRolls;
};
aDie::aDie() {
}
int aDie::getRoll() {
return roll;
}
void aDie::setRolls(int r) {
roll = r;
}
int aDie::getTotal() {
return totalRoll;
}
void aDie::setTotal(int t) {
totalRoll = t;
}
int aDie::Roll() {
//roll the dice here
return 0;
}
int aDie::Display() {
int DiceFaces[6] { numOnes, numTwos, numThrees, numFours, numFives, numSixes };
return DisplaySomeRolls;
//use this to display the outputs of each face
}
// ProjectNumberTwo.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <ctime>
#include <cstdlib>
#include "Dice.h"
using namespace std;
int main(){
aDie getRolls;
aDie DiceFaces[6];
int numRolls = 0;
int rollTotal = 0;
int numOnes = 0, numTwos = 0, numThrees = 0, numFours = 0, numFives = 0, numSixes = 0;
int randomRoll = rand() % 6 + 1;
srand((unsigned int)time(NULL));
cout << "How many rolls? "; // GEt user input of how many rolls
cin >> numRolls;
cin.ignore();
cout << "**Dice Roll Statistics***" << endl;
getRolls.setRolls(numRolls);
if (numRolls >= 1) { // Rolls dice numRolls times
for (int i = 0; i < 6; i++) //makes a for-loop that makes an array of all the rolls from the user input
DiceFaces[i].setRolls(randomRoll);
numRolls = randomRoll;
if (rollTotal == 1) { // Count number of occurences 1-6
numOnes += 1;
}
else if (rollTotal == 2) {
numTwos += 1;
}
else if (rollTotal == 3) {
numThrees += 1;
}
else if (rollTotal == 4) {
numFours += 1;
}
else if (rollTotal == 5) {
numFives += 1;
}
else if (rollTotal == 6) {
numSixes += 1;
}
}
for (int i = 1; i < 6; i++)
cout << i << ": " << DiceFaces[i].Display() << endl;
// The printout needs to be rollFaces so it prints how many times the face
// happened, not getroll which only shows one single roll
system("pause");
return 0;
}
'DisplaySomeRolls'は設定されません。あなたの出力は、それが取るデフォルト値です。これは何でもかまいません。 –
あなたのコードは非常に混乱しています。作成するロールの数は、1つのダイと思われるオブジェクトに配置され、その後は完全に無視されます。何回ロールが入っても、コードは常に6つのロールだけを作成します。それから、常に0に初期化された完全に異なるintをとり、それを1から6までの数字と比較します。これが何を意味すると思われますか、あなたの推測は私のものと同じくらい良いものです。 –
だから私はそれが印刷される前に、顔の各ロールの値と同じに設定する必要がありますか? –