473,387 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

*HELP!!* Lottery prog.. numbers matching are repeating.

mkannah
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:

Expand|Select|Wrap|Line Numbers
  1. // ETO NA TALAGA.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <ctime>
  7. #include <cstdlib>
  8. #include <conio.h>
  9.  
  10. using namespace std;
  11.  
  12. int main()
  13. {
  14.     srand(time(0));
  15.  
  16.     int userinput[5], lottoresults[5];
  17.     int counter=0;
  18.     char ans=0;
  19.  
  20.     do{
  21. //intro:
  22.     cout<<"\t\t\n\n";
  23.     cout<<"\t\t\tWelcome to the lucky lotto! \n\n";
  24.     cout<<"\t\t\tLet's get started..\n\n";
  25.     cout<<"\t\t\t  Instruction:\n\n";
  26.     cout<<"\t\t\t  *Input five numbers between 0 and 9 only.\n\n";
  27.  
  28. //inputnum:
  29.  
  30.  
  31.  
  32.     for (int i=0; i<5; i++)
  33.  
  34.     {cout<<"\n\n\t\tPlease type your five lucky numbers for today (0-9): ";
  35.     cin>>userinput[i];
  36.  
  37.     for(int a=1; a<=5; a++){ 
  38.         while(userinput[i]==userinput[i-a])
  39.         {cout<<"\n\n\tYou've already entered that number. Please type another number: ";
  40.          cin>>userinput[i];}
  41.         }
  42.         while(userinput[i]<0||userinput[i]>10)
  43.         {cout<<"\n\n\tEnter numbers between 0-9 only. Please type another number: ";
  44.         cin>>userinput[i];}
  45.     }
  46.  
  47.     for (int i=0; i<5; i++)
  48.  
  49.     system("cls");
  50.  
  51. //results:
  52.  
  53.     for (int x=0; x<40;x++){
  54.     cout<<"\n\n\t\t\tGenerating numbers.... \n\n";
  55.     cout<<"\n\n\t\t\tPlease wait....\n\n";
  56.     for (int x=0;x<1;x++)
  57.     cout<<" ";};
  58.     system("cls");
  59.     cout<<endl;
  60.  
  61.  
  62.     cout<<"\n\n\t\t\tLOTTO WINNING NUMBERS: ";
  63.     for(int lot=0;lot<5; lot++){
  64.     lottoresults[lot]= (rand()%10);
  65.     cout<<lottoresults[lot]<<" ";
  66.     }
  67.  
  68.  
  69.     cout<<"\n\n\t\t\tUSER NUMBERS: ";
  70.     for (int i=0; i<5; i++)
  71.     {cout<<userinput[i]<<" ";}
  72.  
  73. //matcher:
  74.  
  75.     int matchnum[5];
  76.     for(int x = 0; x < 5; x++)
  77.         for(int y = 0; y < 5; y++)
  78.             if(userinput[x] == lottoresults[y])
  79.                 counter++;
  80.  
  81.     int o=0;
  82.     for(int x = 0; x < 5; x++)
  83.         for(int y = 0; y < 5; y++){
  84.             if(userinput[x] == lottoresults[y]){
  85.                 matchnum[0] = userinput[x];
  86.                 ;o++;}
  87.         }
  88.  
  89.     cout<<endl;
  90.     cout<<"\n\n\t\t\tMATCHING NUMBERS: ";
  91.  
  92.     int x = 5;
  93.     for(int y=0; y<5;y++){
  94.         if(y>counter-1)
  95.             break;
  96.             x--;
  97.         cout<<matchnum[0]<<" ";}
  98.         cout<<"\n";
  99.  
  100.  
  101.     cout<<endl;
  102.          if (counter<2)
  103.              cout<<"\n\n\t\t\tThere are "<<counter<<" matching digit. \n.";
  104.          else
  105.              cout<<"\n\n\t\t\tThere are "<<counter<<" matching digits. \n";
  106.  
  107.  
  108.     cout<<"\n\n\t\t\tPRIZE: ";
  109.     switch(counter){
  110.     case 0:
  111.         cout<<"Sorry, none of your digits matched.\n";
  112.         cout<<"\t\t\t\tBetter luck next time!\n\n\t\t\t";
  113.         break;
  114.     case 1:
  115.         cout<<"PHP10.00 (Balik-Taya)\n\n";
  116.         break;
  117.     case 2:
  118.         cout<<"PHP500.00.\n\n";
  119.         break;
  120.     case 3:
  121.         cout<<"PHP20,000.\n\n";
  122.         break;
  123.     case 4:
  124.         cout<<"PHP100,000.\n\n";
  125.         break;
  126.     case 5:
  127.         cout<<"All of your digits matched the lotto results.\n";
  128.         cout<<"\t\t\t\tYou won PHP500,000.\n\n";
  129.         break;
  130.     }
  131.  
  132.     if(counter!= 0)
  133.         for(int x = 0; x < 1; x++){
  134.             cout<<"\n\n\t\t\tCONGRATULATIONS!!\n\n";
  135.             cout<<"\n\n\t\t";
  136.         for(int y = 0; y < 1; y++)
  137.         ;}
  138.         system ("pause");
  139.         system("cls");
  140.  
  141.  
  142.         cout<<endl<<"\n\t\t\tDo you want to play again [Y/N]? ";
  143.         cin>>ans;
  144.  
  145.         while((ans != 'Y' && ans != 'y')&&(ans != 'N' && ans != 'n')){
  146.             cout<<endl<<"\n\t\tPlease Enter Y or N only!\n\n\t\t";
  147.             cout<<"\t\tDo you still want to play again?: ";
  148.             cin>>ans;
  149.  
  150.         }
  151.  
  152.         system("CLS");
  153.  
  154.     }while(ans=='Y'||ans=='y');
  155.     cout<<"\n\n\n\n\n\t\t\tTHANK YOU FOR PLAYING!\n\n\t";
  156.     cout<<"\t\t\Hope you enjoyed.\n\n\t";
  157.     cout<<"\t\tPlay again next time to win for more!\n\n\t\t\t";
  158.  
  159. cin.sync();
  160. cin.get();
  161. return 0;
  162. }
  163.  
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.
Sep 6 '13 #1
7 4114
weaknessforcats
9,208 Expert Mod 8TB
Exactly what is repeating?
Sep 7 '13 #2
What repeating here is, the numbers that matched the user's input and the lotto results. Try to run this, then you'll see. Please help me, how to remove the repeating matched numbers. Thanks.
Sep 7 '13 #3
weaknessforcats
9,208 Expert Mod 8TB
Anytime you do a rand() you can get the same number more than once.

So, when you generate your lotto number you need to check if you have already received that number, and if so, call rand() again until you get a number you have not received.

The easiest thing to do is add an array of 10 int as an edit mask. Initialize each element of this array to 0.

You now map your rand() %10 number, which must be between 0 and 9 to this 10 int array. If your rand()%10 is 5, then look at the array[5]. If array[5] is 0 you have not received a 5 so change array[5] to 1.

On the other and, if array[5] is 1, then you have already received a 5 and you should call rand()%10 again.
Sep 7 '13 #4
Thank you very much. What i exactly mean is, the matching numbers. For example.
The output will be like this:
Expand|Select|Wrap|Line Numbers
  1. LOTTO WINNING NUMBERS: 6 7 8 4 1
  2. USER NUMBERS: 4 8 0 9 1
  3. MATCHING NUMBERS: 4 1 1 
  4. PRIZE: PHP20,000
  5. CONGRATULATIONS!!
  6.  
  7.  
And there, the matching numbers is repeating. Help me please how to get rid of that.
Sep 7 '13 #5
weaknessforcats
9,208 Expert Mod 8TB
You have some very odd code here:

Expand|Select|Wrap|Line Numbers
  1.     int o=0;
  2.     for(int x = 0; x < 5; x++)
  3.         for(int y = 0; y < 5; y++){
  4.             if(userinput[x] == lottoresults[y]){
  5.                 matchnum[0] = userinput[x];
  6.                 ;o++;}
  7.         }
  8.  
You are using a variable named o and you are doing an o++ but you never use o. Instead you use 0 (zero).

Try to avoid using variables named o or l because they look like 0 and 1.
Sep 7 '13 #6
Ah, i see, thanks. So, what do you think the overall code should be? I am confused, thanks.
Sep 7 '13 #7
weaknessforcats
9,208 Expert Mod 8TB
I would use two loops. The outerloop runs from userinput[0] to userinput[4]. The inner loop runs from lottoresults[0] to lottoresults[4] and in this inner loop you match userinput[x] to every element of lottoresults. If you get a match, increment a balance counter.

After the two loops complete, the balance counter should contain the number of matches.
Sep 7 '13 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Fernando Armenta | last post by:
Hello, I need some help matching a file to a path. This is my code: # I open an expect script to log into a linux system and get the version (6.5)
1
by: Henry | last post by:
I have a table that stores a list of zip codes using a varchar column type, and I need to perform some string prefix pattern matching search. Let's say that I have the columns: 94000-1235 94001...
7
by: brian.digipimp | last post by:
Write a program that prompts the user to input a positive integer. It should then output a message indicating whether the number is a prime number. (Note: An even number is prime if it is 2. An odd...
4
by: Megan | last post by:
Hi- I need some help/ advise on how to code unique numbers for the primary keys of my 2 tables. I inherited a database that covers information about Hearings and Rulings. Information about the...
2
by: john conwell | last post by:
I have 3 5-digit numbers that i'd like to combine into one 15 digit number, and later rip back into the original 3 5-digit numbers. I know i can do this easily with some string manipulations, but...
3
by: ronnchpra | last post by:
Hello, I need help generating 50 unique numbers using rand() BETWEEN 1 and 100 and assign them to a array. I have worked out the number randomization and assignment, but I cant figure out how to...
1
by: kazper | last post by:
Good day to all, I would like to ask for your expertise regarding putting numbers for each item for each group on my report (Visual Basic 6) Example of report HR Department No. ...
1
by: gobblegob | last post by:
Hi guys i am trying to randomize 6 numbers into 6 textboxes up to the value of 60 without repeating a number but i dont know how an error handler would work for duplicate numbers any idea's? this...
15
by: capablanca | last post by:
Hi How to create a program that ask to the user enter 10 numbers and the program can form all the combinations of 5 numbers without repeating. Thanks for any advice.
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.