I've been writing this c++ lotto simulator and im trying to get it to display the users lotto numbers (userNums array), the winning numbers that are randomly generated and inserted into an array (pBallDrawing) and then your winnings but when i try to output the two arrays using a for loop i just get garbage values (-84792048 that kind of thing) or the last value of the array.
I believe it goes wrong around the duplicate tests for both arrays but i dont know how exactly. I've tried mixing it around but then it just displays the last inputted value.
EDIT: ok so ive narrowed down the problem to these functions. I have these functions set in a loop inside main. Thank you for the suggestions about using things like std::shuffle
and shoring up my output but im in a beginners class and my professor hasn't taught use that so im pretty sure i'd get knocked off points for using 'shortcuts' like shuffle.
void userPBInput(int &num) {
cout << "Please enter 5 unique numbers you would like to use for the lottery--> ";
cin >> num;
}
//*************userPBInputTest********************************
//Description: tests the userNums to make sure they are destinct and within range.
//Pre: recives the userNums array and num cin value from the preveious function.
//Post: will test the array and then pass it back to userPBInput.
//************************************************************
void userPBInputTest(int num, int userNums[BALLCNT]) {
for (int r = 0; r < BALLCNT - 2; r++) {
while (num<MIN || num>MAX) {
cout << "Error. Please enter a number within 1 and 69 --> ";
cin >> num;
}
while (num == userNums[r]) {
cout << "Error. Please enter a new number that you haven't entered already--> ";
cin >> num;
}
userNums[r] = num;
}
}
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
const int BALLCNT = 6;
const int REDBALL = 5;
const int MAX = 69;
const int MIN = 1;
const int RMAX = 26;
void userPBInput (int &num);
void userPBInputTest(int num, int userNums[BALLCNT]);
void redBall(int userNums[BALLCNT]);
void rngPBall(int drawing);
void rngPBallTest(int drawing, int ballDrawing[BALLCNT]);
void redRngPBall(int ballDrawing[BALLCNT]);
void matchCounter(int userNums[BALLCNT], int ballDrawing[BALLCNT]);
void winnings(int match, bool redMatch);
int main() {
srand(time(NULL));
int userNums[BALLCNT]; //users lotto numbers
int ballDrawing[BALLCNT]; // the winning numbers
int num = 0; // secondary value to validate users numbers for range and duplicates
int drawing = 0; // same as previous but with the winning numbers (duplicate only not range)
int match = 0; //will count how many white ball matches between the winning and the users numbers
bool redMatch = false; //tracks if you get a match with the red lotto ball.
for (int k = 0; k < BALLCNT - 1; k++) { //puts the functions into loop so you can enter 5 numbers and have them tested
userPBInput(num);
userPBInputTest(num, userNums);
}
redBall(userNums); //allows you to enter the redball number in the 6th element of the array
for (int g = 0; g < BALLCNT - 1; g++) { //loop to generate and test 5 numbers
rngPBall(drawing); //Where the user inputs their lotto numbers
rngPBallTest(drawing, ballDrawing); // the teest
}
redRngPBall(ballDrawing);
matchCounter(userNums, ballDrawing);
winnings(match, redMatch);
}
//*******************userPBInput*****************************
//Description: Will accept 5 numbers and the 6th red number from the user.
//Pre: receives num value for comparison in
//post: will be filled with numbers as the userPBInputTest tests to make sure that they are within range and are distinct and then passed to the decideWinnings function.
//****************************************************
void userPBInput(int &num) {
cout << "Please enter 5 unique numbers you would like to use for the lottery--> ";
cin >> num;
}
//*************userPBInputTest********************************
//Description: tests the userNums to make sure they are destinct and within range.
//Pre: recives the userNums array and num cin value from the preveious function.
//Post: will test the array and then pass it back to userPBInput.
//************************************************************
void userPBInputTest(int num, int userNums[BALLCNT] ){
for (int r = 0; r < BALLCNT - 1; r++) {
while (num<MIN || num>MAX) {
cout << "Error. Please enter a number within 1 and 69 --> ";
cin >> num;
}
while (num == userNums[r]) {
cout << "Error. Please enter a new number that you haven't entered already--> ";
cin >> num;
}
userNums[r] = num;
}
}
//*************redBall*********************
//Description: Asks the user for what value they'd like for their red powerball
//Pre: recieves empty int redB value
//post: will pass redB on to the winnings decision function to be compared with the
//randomly generated red ball value
//**************************************
void redBall(int userNums[BALLCNT]) {
cout << " And what would you like for your powerball? (aka the redball)-->";
cin >> userNums[REDBALL];
while ( userNums[REDBALL]> MAX || userNums[REDBALL] < MIN) {
cout << " The red ball need to be within 1 and 26. Try again--> ";
cin >> userNums[REDBALL];
}
}
//**********rngPBall******************************
//Description: Will generate 5 random numbers for the simulated ball drawing and one value for the redBall
//Pre: Will recieve the array meant for the lotto drawing
//Post: will be passed to the test function to make sure they are unique and within range and then passed to the winnings calculator
//*************************************************
void rngPBall(int drawing) {
drawing = MIN + rand() % MAX;
}
//**********rngPBallTest********************
//Description: will test the randomly generated numbers for duplicates and make a new number if so
//Pre: Recieves drawing value with RNG number and the winning numbers array
//post: will pass on fully tested array to main and to the winnings calculator
//******************************************
void rngPBallTest(int drawing, int ballDrawing[BALLCNT]) {
for (int e = 0; e < BALLCNT - 1; e++) { //to test the winning numbers for duplicates
while (drawing == ballDrawing[e])
drawing = MIN + rand() % MAX;
ballDrawing[e] = drawing; //assigns the value of drawing to its place in the ballDrawing Array
}
}
//***********redRngPBall********************************************
// Description: to generate a winning number for the last value in the array, to represent the red ball
//Pre: recieves ballDrawing Array and fills the last element with the red ball drawing
//Post: will pass full array to winnings calculator
//******************************************************************
void redRngPBall(int ballDrawing[BALLCNT]){
ballDrawing[REDBALL] = MIN + rand() % RMAX; //Generates a number for the element #6
}
//**************matchCounter***************************************
// Description: will test each element of both arrays against eachother and track the amount of matches you have and if the
// red number matches.
//Pre: Recieves both users array and the randomly generated array .
//Post: will pass the match/redmatch ints to int winnings.
void matchCounter(int userNums[BALLCNT], int ballDrawing[BALLCNT]) {
int match = 0; //tracks how many matches you get
bool redMatch = false; //since you either have a match or you dont, made it a bool
for (int v = 0; v < BALLCNT - 1; v++) //user/winning match counting loop
while (userNums[v] == ballDrawing[v])
match++;
if (userNums[6] == ballDrawing[6]) //boolean decision.
redMatch = true;
}
//***************winnings********************************************
//Description: Will decide what your winnings are based on if the red powerball matches or not and how many white matches you get
//Pre: recieves the match int and redMatch bool
//post: will cout and return your winnings
//********************************************************************
void winnings(int match, bool redMatch) {
int winnings = 0
; switch (match) { //will decide what you win based on how many matches you get .
case 5: if (redMatch == true)
cout << "YOUVE JUST WON THE JACKPOT";
else
winnings = 1000000;
break;
case 4: if (redMatch == true)
winnings = 50000;
else
winnings = 100;
break;
case 3: if (redMatch == true)
winnings = 100;
else
winnings = 7;
break;
case 2:if (redMatch == true)
winnings = 7;
break;
case 1: if (redMatch == true)
winnings = 4;
break;
default: winnings = 4;
}
cout << " You win $" << winnings<<"."; //displays what you won.
}
I need it to be able to display the 6 numbers of both arrays, but just get garbage values apologies if this is too much code but i didn't want to leave out any context.