I am writing a lottery program and I am almost done but have one problem. The user input cannot have the same number twice and the assignment came with a function to use so that doesnt happen. I am unsure of what to put in the while loop with the function in it. please help.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
void getChoices( int array[], int numValues )
{
int i, upperBd=10;
for (i=0; i < MAX_PICK; i++)
{
cout << "Enter a Number ";
while (isDuplicate(array, numValues, )) // should be something after numValues,but I dont know what
{
cout << "Enter a Number ";
}
array[i] = getUserNum( upperBd );
}
}
I wrote to survivor31, who probably has the same homework as you:
You have two nested loops. Would it be enough to have just one?
1. Read number
2. If number is valid, then add it to list
3. Repeat from 1 until the list is full
Here is a simple code that has a user enter numbers 10 times, but asks for a different number if any are repeated. myArray[10] is a list of 0s {0 0 0 0 0 0 0 0 0 0}
and when (for example) 5 is guessed, element 4 becomes a 1 {0 0 0 0 1 0 0 0 0 0}
the array will keep track of what numbers are guessed.
// PBachmann
// Duplicate Integer Input Prevention
// 2 April 2016
#include <iostream>
usingnamespace std;
// Returns the number(1 or 0) at myArray[x] then sets that element to 1
bool checkDuplicate(int x, bool myArray[]){
bool placeholder = myArray[x];
myArray[x] = 1;
return (placeholder);
}
// Displays the array
void print_myArray(bool myArray[]){
cout << "{" << myArray[0];
for(int i = 1; i < 10; i++){
cout << " " << myArray[i];
}
cout << "}\n";
}
int main(){
int value;
bool myArray[10] = {0}; // myArray[10] means there are 10 guessable numbers: 1-10
for(int i = 0; i < 10; i++){ // Runs 10 times
print_myArray(myArray);
cout << "Enter a number: ";
cin >> value;
// I use value-1 here to shift the guessable numbers 1-10 to elements 0-9 in the array
while (checkDuplicate(value-1, myArray)){
cout << "You already entered " << value << ", try again.\nEnter a number: ";
cin >> value;
}
}
return 0;
}