mardi 3 janvier 2017

Storing Randomly Generated Numbers Into An Array

I'm currently doing a project where I need to create a bingo program in C++. For the project I need a "Bingo called" to call out random numbers, these random numbers then need to be stored into an array and outputted via a switch statement that should display the stats. I have nearly all of this done but for the life of me I cannot figure out how I can store the randomly generated bingo caller numbers into an array. I have searched everywhere online and any help would be GREATLY appreciated as I have been tearing my hair out over this for the past couple of hours. Here is my code.

    #include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include <algorithm> // for std::find
#include <iterator> // for std::begin, std::end
#include "stdafx.h"



void load_menu(void);
void sum(void);
void rest(void);

int main(int argc, char** argv)
{
    load_menu();
    return 0;

}

void load_menu(void)
{
    int choice;

    do
    {
        printf("=============================================================\n");
        printf("              WELCOME TO BINGO 2016          \n");
        printf("=============================================================\n");
        printf("Please Enter Your Choice Of Action\n");
        printf("1. Play Bingo\n");
        printf("2. Load An Old Game Of Bingo\n");
        printf("3. Exit\n");
        scanf("%d",&choice);

        switch(choice)
        {
            case 1: sum();
                break;
            case 2: rest();
                break;
            case 3: printf("Quitting program!\n");
                exit(0);
                break;
            default: printf("Invalid choice!\n");
                break;
        }

    } while (choice != 3);

}

void sum(void)
{
    int bingo[5][5], row, col;
    int stats[90], i;
    int num = 90;
    int ch;
    int choice2 = 0;
    int callerNumber;
    int counter = 0;


    printf("Here Is Your Bingo Card!\n");
    // Seed used to make sure that the rand use is different each time
    srand(time(NULL));

    printf("Player 1 Bingo Card\n");
    printf(" \n");
    for (row = 0; row<5; row++)
    {
        for (col = 0; col<5; col++)
        {
            bingo[row][col] = rand() % num + 1;
        }
        printf("\n");
    }
    for (row = 0; row<5; row++)
    {
        for (col = 0; col<5; col++)
        {
            printf("%d\t", bingo[row][col]);
        }
        printf("\n");
    }
    while (choice2 != 3 || counter != 90) {
    printf("Please Enter Your Choice Of Action\n");
    printf("1. Draw The Next Number\n");
    printf("2. Display Stats\n");
    printf("3. Exit\n");
    scanf("%d", &choice2);



    //while (choice2 != 3 || counter != 90) {
    switch (choice2)
    {
    case 1:
        callerNumber = rand() % 91 + 1;

        printf("The Caller Picks A Number, The Number %d Was Drawn\n", callerNumber);
        counter++;

            break;
        case 2:
            printf("This Is Where The Stats Should Be Displayed\n");

            break;
        case 3: printf("Quitting program!\n");
            exit(0);
            break;
        default: printf("Invalid choice!\n");
            break;
        }

    }





    /* Flushes input buffer from the newline from scanf() */
    while ( (ch = getchar()) != '\n' && ch != EOF) ;

    printf("\n\nPress ENTER to continue.");
    while ( (ch = getchar()) != '\n' && ch != EOF)
        ;

    return;
}       

void rest(void)
{
    int ch;
    printf("Sleepy sleepy... zZZzZzZz\n");
    printf("You now feel awake again!\n");


    /* Flushes input buffer */
    while ((ch = getchar()) != '\n' && ch != EOF) ;

    printf("\n\nPress ENTER to continue.");
    while ((ch = getchar()) != '\n' && ch != EOF)
        ;

    return;
}

Please ignore the "rest" part and some other parts of the code as they are simply there for something I need to do later on in the project. Most of what I need help with is inside "Void sum". Every time the user enters the number 1 the bingo caller should then call another random number and I want all the numbers he has called to be stored and displayed via an array. They should then be displayed by pressing 2. Any help at all would be amazing. Thank you.




Aucun commentaire:

Enregistrer un commentaire