Question

I need help with coding with c++. I need to make a lottery program where you...

I need help with coding with c++. I need to make a lottery program where you enter in 5 numbers from 0-9 and the program will generate 5 random numbers 0-9 to represent the lottery numbers. I'm almost done, but the output isn't exactly what I want. It works fine, but the format should look like this:

lottery array: 7 4 9 1 3
user array: 4 2 9 7

p.s. I also need to count how many matching numbers there are from the user input and the lottery and need help with that as well, because it says match is uninitialized for some reason.

I have been trying to get it done, but i am stuck. Here's my code:

#include <iostream>
#include<ctime>
#include <cstdlib>
#include <iomanip>
using namespace std;

void input(int user[], int raysize);
void rand(int lot[], int raysize);
void print(int lot[], int user[]);
int compare(int lot[], int user[]);
void matches(int match);

int main()
{
  
   const int raysize = 5;
   int user[5], lot[5];
   int match;
   srand(time(0));
   input(user, raysize);
   rand(lot, raysize);
   print(lot, user);
   compare(lot, user);
   matches(match);
   system("PAUSE");
}

void input(int user[], int raysize)
{
   for (int i = 0;i < 5;i++)
   {
       cout << "Enter in five numbers from 0-9" << endl;
       cin >> user[i];
   }
   int i = 0;
   while (user[i] < 0 || user[i]>9)
   {
       cout << "Error!" << endl;
       exit(1);
   }
}

void rand(int lot[], int raysize)
{
   for (int i = 0; i < 5; i++)
   {
       lot[i] = rand() % 10;
   }

}

void print(int lot[], int user[])
{
   for (int i = 0; i < 5; i++)
   {
      
       cout << "Lottery numbers are: "<< lot[i]<< " " << " your numbers are: " << user[i] << endl;
      


   }
  
}

int compare(int lot[], int user[])
{
   int match = 0;
   for (int i = 0;i < 5;++i)
   {
       if (user[i] == lot[i])
       {
           match++;
       }
   }
   return match;

  
}

void matches(int match)
{
  
}

If possible can you try to do this question similar to this structure? It will help more that way.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
#include<ctime>
#include <cstdlib>
#include <iomanip>
using namespace std;

void input(int user[], int raysize);
void rand(int lot[], int raysize);
void print(int lot[], int user[]);
int compare(int lot[], int user[]);

/*EDITED*/
//i have changed the protoype of this function. I have passed two arrays into it
void matches(int match,int[],int[]);

int main()
{
  
const int raysize = 5;
int user[5], lot[5];
int match;
srand(time(0));
input(user, raysize);
rand(lot, raysize);
print(lot, user);

/*
    This part is edidted
*/

//as we know compare function is returning some value..
//then store the returned value into some variable...
//store it in match variable and then pass this variable to matches function
match =compare(lot, user);

//pass array and match variable to matches function
matches(match,lot,user);
system("PAUSE");
}

void input(int user[], int raysize)
{
for (int i = 0;i < raysize;i++)
{
cout << "Enter in five numbers from 0-9" << endl;
cin >> user[i];
}
int i = 0;
while (user[i] < 0 || user[i]>9)
{
cout << "Error!" << endl;
exit(1);
}
}

void rand(int lot[], int raysize)
{
for (int i = 0; i < raysize; i++)
{
lot[i] = rand() % 10;
}

}

void print(int lot[], int user[])
{
for (int i = 0; i < 5; i++)
{
  
cout << "Lottery numbers are: "<< lot[i]<< " " << " your numbers are: " << user[i] << endl;
  


}
  
}

int compare(int lot[], int user[])
{
int match = 0;
for (int i = 0;i < 5;++i)
{
if (user[i] == lot[i])
{
match++;
}
}
return match;

  
}

void matches(int match, int lot[], int user[])
{
//print the total number of matches
cout<<"\nTotal matches are: "<<match<<endl;
  
//print lottery array
cout<<"Lottery Array: ";
for(int i=0;i<5;i++)
   cout<<lot[i]<<" ";
  
//print user array
cout<<"\nUser Array: ";
for(int i=0;i<5;i++)
   cout<<user[i]<<" ";
  
cout<<endl<<endl;
}

================

Output

Add a comment
Know the answer?
Add Answer to:
I need help with coding with c++. I need to make a lottery program where you...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a C++ program that simulates a lottery. The user will select 5 numbers 0 through...

    Write a C++ program that simulates a lottery. The user will select 5 numbers 0 through 9 and put this in an array. Then these user numbers are compared to the random numbers the computer generated. The program will display both the computer random number and below the user selected numbers. The program will tell the user how many matches are made. If the user guesses all five you let them know they are the grand winner. Here is the...

  • Hello, I am working on a C++ pick 5 lottery game that gives you the option...

    Hello, I am working on a C++ pick 5 lottery game that gives you the option to play over and over. I have everything working right except that every time the game runs it generates the same winning numbers. I know this is an srand or rand problem, (at least I think it is), but I can't figure out what my mistake is. I've tried moving srand out of the loop, but I'm still getting the same random numbers every...

  • Using C++ create a lotto program Lottery Design a program that simulates a lottery. Requirements:  The program...

    Using C++ create a lotto program Lottery Design a program that simulates a lottery. Requirements:  The program should have an array of 5 integers named lottery and should generate a random number in the range of 1 through 99 for each element of the array. The user should enter five digits, which should be stored in an integer array named user. The program is to compare the corresponding elements in the two arrays and keep a count of the digits that...

  • Modify this C++ below to show the selection sorting algorithm method. Then write a test program...

    Modify this C++ below to show the selection sorting algorithm method. Then write a test program and show that it works. HINT: Replace the bubbleSort function with the selectionSort. Your test program should perform the following steps: Create an array of random numbers. Display the array in its original order. Pass the array to your sorting function. Display the array again to show that it has been sorted. Provide a screen shot showing that the above steps are working. CODE:...

  • I need to make a few changes to this C++ program,first of all it should read...

    I need to make a few changes to this C++ program,first of all it should read the file from the computer without asking the user for the name of it.The name of the file is MichaelJordan.dat, second of all it should print ,3 highest frequencies are: 3 words that occure the most.everything else is good. #include <iostream> #include <map> #include <string> #include <cctype> #include <fstream> #include <iomanip> using namespace std; void addWord(map<std::string,int> &words,string s); void readFile(string infile,map<std::string,int> &words); void display(map<std::string,int>...

  • This is a c++ program. Use the description from Parking Ticket Simulator (listed below) as a basis, where you need to...

    This is a c++ program. Use the description from Parking Ticket Simulator (listed below) as a basis, where you need to create a Car class and Police Officer class, to create a new simulation. Write a simulation program (refer to the Bank Teller example listed below) that simulates cars entering a parking lot, paying for parking, and leaving the parking lot. The officer will randomly appear to survey the cars in the lot to ensure that no cars are parked...

  • I need help fixing my code: In C++ *************** 1) I want to sum the digits...

    I need help fixing my code: In C++ *************** 1) I want to sum the digits of an n*n matrix 2) find the average I have completed the rest ****Do not use C++ standard library. You must use pointers and pointer arithmetic to represent the matrix and to navigate through it. MY CODE: (I have indicated at which point I need help) #include <iostream> using namespace std; void swap(int *xp, int *yp) { int temp = *xp; *xp = *yp;...

  • c++, I am having trouble getting my program to compile, any help would be appreciated. #include...

    c++, I am having trouble getting my program to compile, any help would be appreciated. #include <iostream> #include <string> #include <string.h> #include <fstream> #include <stdlib.h> using namespace std; struct record { char artist[50]; char title[50]; char year[50]; }; class CD { //private members declared private: string artist; //asks for string string title; // asks for string int yearReleased; //asks for integer //public members declared public: CD(); CD(string,string,int); void setArtist(string); void setTitle(string); void setYearReleased(int); string getArtist() const; string getTitle() const; int...

  • In C++ please.. I only need the game.cpp. thanks. Game. Create a project titled Lab8_Game. Use...

    In C++ please.. I only need the game.cpp. thanks. Game. Create a project titled Lab8_Game. Use battleship.h, battleship.cpp that mentioned below; add game.cpp that contains main() , invokes the game functions declared in battleship.h and implements the Battleship game as described in the introduction. ——————————————- //battleship.h #pragma once // structure definitions and function prototypes // for the battleship assignment // 3/20/2019 #include #include #ifndef BATTLESHIP_H_ #define BATTLESHIP_H_ // // data structures definitions // const int fleetSize = 6; // number...

  • i have two issues that i need help. 1- that in case three and two only...

    i have two issues that i need help. 1- that in case three and two only one line is being read from the file 2- my case 4 is not working correctly as it should as the output is only " Enter 1 for Trump, 2 for Warren:" #include <iostream> #include <cctype> // For the letter checking functions #include <fstream> // For file input #include <iomanip> // For setw #include <ctime> #include <cstdlib> // For exit and abs #include <errno.h>...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT