-1

This code generates 5 random numbers from 1-6. It then stores the numbers into an array that holds 5 numbers. After that, it then prompts the user to enter those five numbers. How do I check if 'cin' matches the numbers in the array?

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <windows.h>
using namespace std;

int magicNumbers;
int code;
int array[5];

void enterCode()
{
    cout << "\n\n\n\nEnter your given code: ";
    cin >> code;

    if(code == array[])
    {
        cout << "Yes!";
    }

    else if(code != array[])
    {
        cout << "No!";
    }

}

int main()
{
    srand(time(NULL));

    for(int x = 0; x < 5; x++)
    {
        magicNumbers = 1+rand()%6;
        array[x] = magicNumbers;
        cout << magicNumbers << endl;
    }

    cout << "\nThe Array" << endl;
    cout << "-----------------" << endl;
    cout << array[0] << array[1] << array[2] << array[3] << array[4];

    Sleep(2000);
    enterCode();

    return 0;
}

David Skx
  • 131
  • 8
  • Possible duplicate: https://stackoverflow.com/questions/19215027/check-if-element-found-in-array-c https://stackoverflow.com/questions/45679999/find-if-an-element-exists-in-c-array https://stackoverflow.com/questions/21830425/c-check-if-item-is-in-a-array https://stackoverflow.com/questions/9079712/c-check-if-element-exists-in-array – Jerry Jeremiah Nov 17 '19 at 22:14
  • Unrelated, `array` is a *terrible* name for a variable in modern C++, *especially* in code that [ill-advisedly uses `using namespace std;`](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). – WhozCraig Nov 17 '19 at 22:30
  • @WhozCraig Well deal with it. This is just an example. – David Skx Nov 17 '19 at 22:33

1 Answers1

0

You can ask your user each number of the code separately, by creating a second array you fill with the user's input. You then compare each array one item at a time.

If you still want to ask for the code all at once, you need to split each digit of the code into an array : - you can use stringstream to convert the number into a string and then split it up character by character - you can directly split the number up for example by using techniques described here : C: how to break apart a multi digit number into separate variables?

ShadowMitia
  • 2,411
  • 1
  • 19
  • 24