mercredi 10 juin 2020

How to display structures Arrays in functions

This program is essentially supposed to count the results of random throws of a dice 100 times and count the occurrence of each face then display all of them as a histogram of asterisks. It would seem the functions could be working but i'm unable to verify because after I make my choice, nothing displays.

#include <iostream>
#include <iomanip>
#include <string>
#include <random>
#include <sstream>
using namespace std;


enum class Side {
  ONE, TWO, THREE, FOUR, FIVE, SIX
};

struct Bar {
  int value;
  Side label;
};


//roll dice function
void rollDice( Bar h[], int n = 100);

void rollDice( Bar h[], int){

default_random_engine en;
uniform_int_distribution<> dist{1,6};

    int results[] ={0,0};

    for( int n; n<=100; n++){
        cout << dist(en);
        results[dist(en)]++;
      h[n].value = results[n];        

      if(h[n].value == 1){
          h[n].label =  Side::ONE;
      }
      else if(h[n].value == 2){
        h[n].label =  Side::TWO;
      }
       else if(h[n].value == 3){
        h[n].label =  Side::THREE;
      }
       else if(h[n].value == 4){
        h[n].label =  Side::FOUR;
      }
       else if(h[n].value == 5){
        h[n].label =  Side::FIVE;
      }
       else {
        h[n].label =  Side::SIX;
      }


    }


};

string getHistogram(Bar h[], char c = '*');

string getHistogram(Bar h[], char c ){

stringstream ast;

    for( int n; n<=100; n++){


          switch (h[n].label)
          {
          case Side::ONE:
              return to_string(c);
            break;
          case Side::TWO:
              return to_string(c);
            break;
          case Side::THREE:
            return to_string(c);
            break;
          case Side::FOUR:
            return to_string(c);
            break;
          case Side::FIVE:
            return to_string(c);
            break;  
          case Side::SIX:
            return to_string(c);
            break;  

          default:
            break;
          }
    }

ast << "One: " << c << endl;
ast << "Two: " << c << endl;
ast << "Three: " << c << endl;
ast << "Four: " << c << endl;
ast << "Five: " << c << endl;
ast << "Six: " << c << endl;

string output = ast.str();

cout<< output;
return output;
}


int main (){

Bar histogram[] = {
  {0,Side::ONE},{0,Side::TWO}, {0,Side::THREE},
  {0,Side::FOUR},{0,Side::FIVE}, {0,Side::SIX}

};

char choice;

do {
  cout << "DICE ROLLING SIMULATION" << endl
       <<"===============================" << endl
       << "r. Roll Dice" << endl
       << "h. Display histogram" << endl
       << "q. Quit program\n" << endl

       << "\nEnter your choice:" << endl;

  // Reading a single character using the scanner
  cin >> choice;



  switch(choice) {
  case 'r': case 'R':
     rollDice(histogram, 100);
    break;
  case 'h': case 'H':
    cout<< getHistogram(histogram, '*');
    break;
  case 'q': case 'Q':
    cout << "Good bye\n" << endl;
    break;
  default:
    cout << "Invalid choice\n" << endl;
  }
} while(choice != 'q');

}



Aucun commentaire:

Enregistrer un commentaire