2016-04-06 15 views
0

私はタイトルが漠然としていますが、サイコロをするプログラムを作っています。(あなたは永遠にコースで見たように)私は混乱しています。ロールからの負の数値結果の印刷

私は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; 
} 
+1

'DisplaySomeRolls'は設定されません。あなたの出力は、それが取るデフォルト値です。これは何でもかまいません。 –

+0

あなたのコードは非常に混乱しています。作成するロールの数は、1つのダイと思われるオブジェクトに配置され、その後は完全に無視されます。何回ロールが入っても、コードは常に6つのロールだけを作成します。それから、常に0に初期化された完全に異なるintをとり、それを1から6までの数字と比較します。これが何を意味すると思われますか、あなたの推測は私のものと同じくらい良いものです。 –

+0

だから私はそれが印刷される前に、顔の各ロールの値と同じに設定する必要がありますか? –

答えて

0

なぜあなたは、このやっている:あなたは、ダイロールを設定しているか、あなたはランダムなロールの割り当てを作っているときに見て、また

numRolls = randomRoll;

を。

正直なところ、使用しているロジックは、必要以上に複雑に思えます。あなたのコードは、あなたが最初から始めることを提案するのに十分なほど素朴です(違反はありません)。カウンターに10個のダイスロールを割り当て、そこから展開するコードを書いてください(デバッガーを恐れてはいけません)。

私はこれがいくつか役に立ったと思います。

+0

何も取られていない、私はその働きが好きではないので、これを作っている間、私の論理がそこにないように、本質的に自分自身を教えています。 –

+0

Samsの提案はあなたにとって非常に役に立つものだと思います。簡単に始める。 1)10種類のダイスロールを印刷します。 2)6個のカウンターを作成し、10個のダイスロール結果を割り当てます。 3)これがどのようにクラスに配置されるのかを勉強し始める。 4)あなたのラバーダッキーなしで家にいないでください。 –

+0

ありがとう、どこに行けばいいのかわかりません –

0

あなたがそうしているかどうか知っておいてください!

#include<iostream> 
#include<stdlib.h> 
using namespace std; 
class dice 
{ 
public: 
    int a,z=0,y=0,x=0,w=0,v=0,u=0; 
    void number_generation() 
    { 
     a=rand()%6+1; 
    } 
    void checkcount(int t) 
    { 
     if(t==1)z++; 
     else if(t==2)y++; 
     else if(t==3)x++; 
     else if(t==4)w++; 
     else if(t==5)v++; 
     else u++; 
    } 
    void display() 
    { 
     cout<<"OCCURENCE OF EACH NUMBER IS AS FOLLOWS:\n"; 
     cout<<"1-"<<z<<endl<<"2-"<<y<<endl<<"3-"<<x<<endl<<"4-"<<w<<endl<<"5-"<<v<<endl<<"6-"<<u<<endl; 
    } 
}; 
int main() 
{ 
    int i,throws; 
    dice d; 
    cout<<"Enter the number of throws"; 
    cin>>throws; 
    for(i=0;i<throws;i++) 
    { 
     d.number_generation(); 
     d.checkcount(d.a); 
    } 
    d.display(); 
} 
関連する問題