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:
Expand|Select|Wrap|Line Numbers
- // ETO NA TALAGA.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <iostream>
- #include <ctime>
- #include <cstdlib>
- #include <conio.h>
- 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<<endl;
- cout<<"\n\n\t\t\tLOTTO WINNING NUMBERS: ";
- for(int lot=0;lot<5; lot++){
- lottoresults[lot]= (rand()%10);
- cout<<lottoresults[lot]<<" ";
- }
- cout<<"\n\n\t\t\tUSER NUMBERS: ";
- for (int i=0; i<5; i++)
- {cout<<userinput[i]<<" ";}
- //matcher:
- int matchnum[5];
- for(int x = 0; x < 5; x++)
- for(int y = 0; y < 5; y++)
- if(userinput[x] == lottoresults[y])
- counter++;
- int o=0;
- for(int x = 0; x < 5; x++)
- for(int y = 0; y < 5; y++){
- if(userinput[x] == lottoresults[y]){
- matchnum[0] = userinput[x];
- ;o++;}
- }
- cout<<endl;
- cout<<"\n\n\t\t\tMATCHING NUMBERS: ";
- int x = 5;
- for(int y=0; y<5;y++){
- if(y>counter-1)
- break;
- x--;
- cout<<matchnum[0]<<" ";}
- cout<<"\n";
- cout<<endl;
- if (counter<2)
- cout<<"\n\n\t\t\tThere are "<<counter<<" matching digit. \n.";
- else
- cout<<"\n\n\t\t\tThere are "<<counter<<" matching digits. \n";
- cout<<"\n\n\t\t\tPRIZE: ";
- switch(counter){
- case 0:
- cout<<"Sorry, none of your digits matched.\n";
- cout<<"\t\t\t\tBetter luck next time!\n\n\t\t\t";
- break;
- case 1:
- cout<<"PHP10.00 (Balik-Taya)\n\n";
- break;
- case 2:
- cout<<"PHP500.00.\n\n";
- break;
- case 3:
- cout<<"PHP20,000.\n\n";
- break;
- case 4:
- cout<<"PHP100,000.\n\n";
- break;
- case 5:
- cout<<"All of your digits matched the lotto results.\n";
- cout<<"\t\t\t\tYou won PHP500,000.\n\n";
- break;
- }
- if(counter!= 0)
- for(int x = 0; x < 1; x++){
- cout<<"\n\n\t\t\tCONGRATULATIONS!!\n\n";
- cout<<"\n\n\t\t";
- for(int y = 0; y < 1; y++)
- ;}
- system ("pause");
- system("cls");
- cout<<endl<<"\n\t\t\tDo you want to play again [Y/N]? ";
- cin>>ans;
- while((ans != 'Y' && ans != 'y')&&(ans != 'N' && ans != 'n')){
- cout<<endl<<"\n\t\tPlease Enter Y or N only!\n\n\t\t";
- cout<<"\t\tDo you still want to play again?: ";
- cin>>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;
- }