0

EDIT I've seen a few wondering what I am asking for. I haven't written the algorithm for it yet, but after the program runs and outputs the 2 arrays, I need to output how many numbers matched. There is an example of that below. My apologies, I really thought that I made it as clear as possible. Here is the example again. If the randomly generated array comes up with the numbers 0,1,2,3,4 in that order, and the user inputs into the "player" array the numbers 0,1,4,3,2 in that order, then they matched 2 digits (0 and 1). Basically it's not enough to just have the same numbers anywhere in the array, they must be in the same spot or index. I need to write an algorithm for this, though I have not yet. I'm sure I need a function with a for loop. I'm looking for any tips on how to go about it.

I've seen many iterations of this program on StackOverflow but none have been able to help me out so here I am asking for help.

I have this program that has 2 arrays. In one array that generates 5 random numbers from 0-9, the name is winningDigits. The other array (player) is for 5 numbers the user inputs. The goal of the user is to input 5 numbers and hope they all match with the randomly generated numbers.

I am almost done with the program, my only issue is trying to output how many digits were matched. For digits to match, they must be in the same spot in the player array as the winningDigits array.

For example If the randomly generated array comes up with the numbers 0,1,2,3,4 in that order, and the user inputs into the "player" array the numbers 0,1,4,3,2 in that order, then they matched 2 digits (0 and 1). Basically it's not enough to just have the same numbers anywhere in the array, they must be in the same spot or index.

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

int main() {
  int player[5];
  int size = 5;
  int count = 0;
cout << "Welcome to Lottery Simulator." << endl << endl;
cout << "Enter 5 numbers ranging from 0-9 one at a time, enter -1 when finished." << endl;

// Loop for user input
  cout << "Enter number: ";
  cin >> player[count];
  while (player[count] != -1)
    {
      count++;
      cout << "Enter number: ";
      cin >> player[count];
    }


  cout << endl;
cout << "Winning Numbers    User Numbers" << endl;
cout << "---------------    ------------" << endl;

// Generating random winning numbers
  srand(time(0));
  int winningDigits[size]; 
  for (int i = 0; i < size; i++) {
    winningDigits[i]= rand() % 10;
    cout << setw(8) << winningDigits[i] << setw(17) << player[i] << endl;
    }

return 0;
}

Now I'm thinking I'll need to make a function with a for loop, but so far I'm stuck. Just in case it wasn't clear enough, I am looking to output how many digits were matched. I hope my example from above will help you all understand what I'm looking for. Sorry, this is only my second post here and I am trying to make everything as clear as possible. Please ask if you need any information or help in understanding what I wrote.

Bahnu
  • 17
  • 7
  • Numbers and digits are different things. – molbdnilo Nov 27 '20 at 12:15
  • 1
    I'd like to ask what you are actually asking. You have working code, right? And you seem to know what you have to do next, so what is your specific question? – Lukas-T Nov 27 '20 at 12:16
  • note that `int size = 5;` should be `const int size = 5;`. Read: [Why aren't variable-length arrays part of the C++ standard?](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard). Its not that important for your question, but imho it is important that you understand that your code is non-portable, because it relies on a compiler extension (and that you actually don't need it) – 463035818_is_not_an_ai Nov 27 '20 at 12:30
  • ps: `int winningDigits[size];` is a VLA, `int player[5];` is fine of course – 463035818_is_not_an_ai Nov 27 '20 at 12:31
  • another semi-related problem is that in your input loop, if the player enters 5 numbers and for the 6th input they enter `-1` then you are accessing `player[5]` which is out-of-bounds already – 463035818_is_not_an_ai Nov 27 '20 at 12:34
  • Well, yes, you clarified what you intend to do, but that's your goal, not the problem you are facing. Is there even a problem you are struggling with? You should try it on your own first, then, if you encounter a problem you can't solve on your own, feel free to ask another question with a proper problem description, a [mre] and a speciifc question. – Lukas-T Nov 27 '20 at 16:30

0 Answers0