### Matching numbers are repeating, how do i get rid of that?

closed account (L1USLyTq)
Hi, i am a beginner. I, uh, should do a lottery that asks the user to input 5 numbers and then generate a 5 random numbers for the lotto results then after it should show what numbers matched, and how many numbers matched then shows the user his/her prize.

My problem is in my code, when i debug it, it shows which numbers matched, but it is repeating.

The question:

Lotto is a game in which winners are chosen by random drawing of numbers from among those who have paid money to participate. Write a program that will simulate the lotto game by asking the user five numbers (use array) and should generate a random number in the range of 0 to 9 (see example below) for each array element. The program will compare the corresponding elements in the two arrays and keep a count of the digits that matches. For instance, if the lottery array contains: 8 5 0 2 4 and the user array contains: 5 3 0 8 4, we can say that there are three numbers that matches. The program should display the random numbers stored in the lotto array and the matching digits. If all of the digits match, display a message proclaiming the user as a grand prize winner. Prizes are as follows:

5 Matching Digits – 500,000.00
4 Matching Digits – 100,000.00
3 Matching Digits – 20,000.00
2 Matching Digits – 500.00
1 Matching Digits – 10.00 (“Balik-Taya”)
0 Matching Digits – Try Again

-----------------------------------
Hint: use rand() for generating random numbers

#include <iostream>
#include <cstdlib>
#include <ctime>

int main()
{ int x = (rand()%10);
cout << x;}

My code goes like this:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162`` ``````// ETO NA TALAGA.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include #include #include #include using namespace std; int main() { srand(time(0)); int userinput[5], lottoresults[5]; int counter=0; char ans=0; do{ //intro: cout<<"\t\t\n\n"; cout<<"\t\t\tWelcome to the lucky lotto! \n\n"; cout<<"\t\t\tLet's get started..\n\n"; cout<<"\t\t\t Instruction:\n\n"; cout<<"\t\t\t *Input five numbers between 0 and 9 only.\n\n"; //inputnum: for (int i=0; i<5; i++) {cout<<"\n\n\t\tPlease type your five lucky numbers for today (0-9): "; cin>>userinput[i]; for(int a=1; a<=5; a++){ while(userinput[i]==userinput[i-a]) {cout<<"\n\n\tYou've already entered that number. Please type another number: "; cin>>userinput[i];} } while(userinput[i]<0||userinput[i]>10) {cout<<"\n\n\tEnter numbers between 0-9 only. Please type another number: "; cin>>userinput[i];} } for (int i=0; i<5; i++) system("cls"); //results: for (int x=0; x<40;x++){ cout<<"\n\n\t\t\tGenerating numbers.... \n\n"; cout<<"\n\n\t\t\tPlease wait....\n\n"; for (int x=0;x<1;x++) cout<<" ";}; system("cls"); cout<counter-1) break; x--; cout<>ans; while((ans != 'Y' && ans != 'y')&&(ans != 'N' && ans != 'n')){ cout<>ans; } system("CLS"); }while(ans=='Y'||ans=='y'); cout<<"\n\n\n\n\n\t\t\tTHANK YOU FOR PLAYING!\n\n\t"; cout<<"\t\t\Hope you enjoyed.\n\n\t"; cout<<"\t\tPlay again next time to win for more!\n\n\t\t\t"; cin.sync(); cin.get(); return 0; }``````

THANK YOU VERY MUCH FOR THOSE WHO'LL ANSWER, i'm so in need of help, and this needs to be submitted on or before this monday, which is september 9 2013.
Last edited on
 ``1234567891011121314151617181920212223242526`` ``````#include #include #include #include // std::random_shuffle #include // std::begin, std::end int main() { std::srand( std::time(0) ) ; // the actual results are the five numbers at the beginning of the array int lottoresults[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } ; // generate 5 non-repeating numbers for lottoresults by // generating a random permutation of the elements in the array // and then picking the first five // see: http://en.cppreference.com/w/cpp/algorithm/random_shuffle std::random_shuffle( std::begin(lottoresults), std::end(lottoresults) ) ; // or: std::random_shuffle( lottoresults, lottoresults+10 ) ; // legacy C++ // print them out for( int i = 0 ; i < 5 ; ++i ) std::cout << lottoresults[i] << ' ' ; std::cout << '\n' ; }``````
Last edited on
closed account (L1USLyTq)
Thank you very much. But, what i exactly mean is that, the matching numbers is the one that is repeating. When you run the code, you'll see and output like this:

LOTTO WINNING NUMBERS: 6 7 4 2 1
USER INPUT: 5 9 0 8 1
MATCHING NUMBERS: 1 1
PRIZE: PHP20,000
CONGRATULATIONS!!

Try to run the code, and you'll see.
Will you please tell me what code should be replaced in my code? I'm so confused, thank you.
> Will you please tell me what code should be replaced in my code? I'm so confused, thank you.

You need to write a nested loop.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556`` ``````#include #include #include #include // std::random_shuffle #include // std::begin, std::end int main() { const int N = 5 ; std::srand( std::time(0) ) ; // the actual results are the five numbers at the beginning of the array int lottoresults[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } ; // generate 5 non-repeating numbers for lottoresults by // generating a random permutation of the elements in the array // and then picking the first five // see: http://en.cppreference.com/w/cpp/algorithm/random_shuffle std::random_shuffle( std::begin(lottoresults), std::end(lottoresults) ) ; // or: std::random_shuffle( lottoresults, lottoresults+10 ) ; // legacy C++ // print them out std::cout << "lotto winning numbers: " ; for( int i = 0 ; i < N ; ++i ) std::cout << lottoresults[i] << ' ' ; std::cout << '\n' ; // numbers entered by the user int userinput[N] = { 5, 9, 0, 8, 1 } ; // say, as an examplew std::cout << "user input: " ; for( int i = 0 ; i < N ; ++i ) std::cout << userinput[i] << ' ' ; std::cout << '\n' ; // check for matching numbers int cnt_matches = 0 ; // #matches std::cout << "matching numbers: " ; for( int i = 0 ; i < N ; ++i ) // for each number entered by the user { const int user_num = userinput[i] ; // check if the number number entered by the user is a winning number // (could use std::find() instead of writing a loop of out own) for( int j = 0 ; j < N ; ++j ) // for each winning number { if( user_num == lottoresults[j] ) // is it the same as user_num? { // yes, we got a match ++cnt_matches ; std::cout << user_num << ' ' ; } } } std::cout << "\nnumber of matches: " << cnt_matches << '\n' ; }``````

http://ideone.com/UX3cEZ
Topic archived. No new replies allowed.