jeudi 29 mars 2018

User-Friendly Dice Rolling Simulator in C++

I have a problem with my "User-Friendly Random Number Generator" in C++

When I run the program it runs fine but however for the last part of the program which is designed to look for a specific number of the numbers that were randomly generated. When I type in a number that was generated the only output of the program is the number "16". What I was hoping the output would be something like this:

Enter how many numbers do you want to be on your dice 6 Enter the number of dice you want to roll 10 3 1 6 1 4 4 6 2 2 1

Do you want to know how many times a number was the outcome of your rolls

Please enter the number you want to be counted for 1 The number 1 showed up 16 Times

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;

int rand_0toN1(int dice_rolls);

int main() {
    int dice_rolls;
    int num_on_dice;
    int x = 1;
    int output;
    int analysis_num;
    int analysis_counter;
    std::string analysis;

    srand(time(NULL));

    cout << "Enter how many numbers do you want to be on your dice" << endl;
    cin >> num_on_dice;
    while(num_on_dice < 2){
        cout << "" << endl;
        cout << "The amount of numbers on a dice cannot be less than 2" << endl;
        cout << "Re-Enter:" << endl;
        cin >> num_on_dice;
     }
    while(num_on_dice > 1000000){
            cout << "" << endl;
            cout << "The amount of numbers on a dice cannot exceed 1000000" << endl;
            cout << "Re-Enter:" << endl;
            cin >> num_on_dice;
        }



    cout << "Enter the number of dice you want to roll" << endl;
    cin >> dice_rolls;
    while(dice_rolls > 1000000000000){
        cout << "" << endl;
        cout << "For testing purposes the number of dice you can roll cannot exceed 100" << endl;
        cout << "Re-Enter:" << endl;
        cin >> dice_rolls;
    }
    while(dice_rolls < 1){
            cout << "" << endl;
            cout << "The number of dice you can roll cannot be less than 1" << endl;
            cout << "Re-Enter:" << endl;
            cin >> dice_rolls;
        }



    while(x <= dice_rolls){
        output = rand_0toN1(num_on_dice) + 1;
        cout << output << endl;
        if(output == analysis_num){
            analysis_counter++;
        }
        x++;
    }





    cout << "" << endl;
    cout << "Do you want to now how many times a number was the outcome of your rolls <Type yes or no>" << endl;
    cin >> analysis;
    if(analysis == "no"){
        cout << "Good-Bye, I hope I will see you again later" << endl;
    }

    if(analysis == "yes"){
        cout << "Please enter the number you want to be counted for" << endl;
        cin >> analysis_num;
        while(analysis_num > num_on_dice){
            cout << "The number that you want to be searched cannot exceed the number that you entered for how many <how many numbers do you want to be on your dice>" << endl;
            cout << "Re-Enter:" << endl;
            cin >> analysis_num;
        }
        cout << "The number " << analysis_num << " showed up " << analysis_counter << " Times" << endl;
}
return 0; 
}

int rand_0toN1(int dice_rolls){
    return rand() % dice_rolls;
}




Aucun commentaire:

Enregistrer un commentaire