2016-04-06 6 views
0

Ich weiß, der Titel ist eine Art vage, aber ich baue ein Programm zum würfeln (wie Sie alle in Kurse für immer gesehen haben), was mich verwirrt ist das Ergebnis ich mit meiner Ausgabe bekommen.negative Zahl Ergebnis Drucken von Rolle

Ich rolle nur einen sechsseitigen Würfel und zähle das Vorkommen jedes Gesichts. Mein Ergebnis gibt aus, wie id in Listenform angezeigt wird, aber wenn ich sage, dass das Programm 30 Mal sagen soll, ist das Ergebnis, das ich erhalte, -858993460. Ich habe keine Ahnung, woher es kommen könnte, und ich hoffe, dass jemand mir eine Vorstellung davon geben konnte, wo ich falsch gelaufen bin, krank meine Kopfzeile und Source-File-Code, einige davon unvollständig in bestimmten Bereichen, in denen ich Kommentare hinterlassen habe.

//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' wird nie gesetzt. Ihre Ausgabe ist einfach der Standardwert, der alles sein kann. –

+0

Ihr Code ist sehr unorganisiert. Die Anzahl der zu erstellenden Rollen wird in ein Objekt gelegt, das ein Würfel sein soll, und dann vollständig ignoriert. Egal wie viele Rollen eingegeben werden, der Code macht immer nur 6 Rollen. Dann braucht es ein ganz anderes "int", das immer auf 0 initialisiert wird, und vergleicht es dann mit den Zahlen 1 bis 6. Was das alles bedeuten soll, ist deine Schätzung so gut wie meine. –

+0

Also müsste ich es gleich jedem Roll-Wert für die Gesichter vor dem Drucken setzen, oder? –

Antwort

0

Warum tun Sie das:

numRolls = randomRoll;

auch schauen, was Sie würfelt zu setzen und wenn Sie die Zufallsrollenzuweisung machen.

Ehrlich gesagt scheint die Logik, die Sie verwenden, viel komplizierter als es sein muss. Ihr Code ist naiv genug, dass ich vorschlage, dass Sie von Anfang an beginnen (nichts für ungut). Schreiben Sie einen Code, um 10 Würfelwürfe auf Ihre Spielsteine ​​zu legen, und erweitern Sie dann von dort aus (NICHT DEN DEBUGGER BETRAGEN).

Ich hoffe, das hilft etwas.

+0

None genommen, ive begann über eine Handvoll Mal während der Herstellung dieser, weil ich nicht mag, wie es funktioniert. Im Wesentlichen lehre ich mich selbst, also ist meine Logik nicht dort. –

+0

Ich denke, Sams Vorschlag ist sehr hilfreich für Sie. Fangen Sie einfach an. 1) 10 verschiedene Würfelwürfe ausdrucken. 2) Erstellen Sie Ihre 6 Zähler und weisen Sie ihnen die 10 Würfelergebnisse zu. 3) Fange an zu lernen, wie dies in eine Klasse gebracht wird. 4) Verlasse dein Zuhause nicht ohne deinen Gummienten. –

+0

Danke, krank bringen ihn mit mir wohin ich gehe –

0

VERSUCHEN SIE DIESE UND LASSEN SIE MICH WISSEN, WENN ES FÜR SIE FUNKTIONIERT ODER NICHT!

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