vendredi 2 septembre 2022

C++, Random Number Generator Return using user inputted value

Given the task of seeing if a user defined integer returns as a given value in an array of ten choices. Those choices are displayed from a random number generator from 0-100. If the function returns a flag as true it is to record that the user given value is in the array. I am also using linear search as the recursive function. I have the function beneath the main and I have put in three error checks. From what I can tell the function is able to accurately read what value the user inputted, when/if the flag is triggered, and to record the value. The issue I am running into is when the function returns to the main and says whether or not it has found the number (if - else statement), it only return the value of 1 and continuously states that it has found the value.

For Note, I have set the random number generator down to the values of 0-20 in order for it not to take forever trying. I do not think the time elements are used for anything either but just incase, I added them.

Based on whether or not your the used inputted value is found, one should find the 'Found it' cout error checker working along with the state of the flag error checker but it should show the Integer Returned checker returning 1 or -1 (it seems in the test code shown below it returns 64?).

I know this is a lot, my first question on this site.

#include <iostream>

#include <iostream>
#include <cstdlib>
#include <cctype>
#include <ctime>
using namespace std;
bool isMember(int *, int, int);
const int ARRAY_SIZE = 10;

int main()
{
    int choice, num, num1, num2;
    int myArray[ARRAY_SIZE];
    srand(time(NULL));

    for (int x = 0; x < ARRAY_SIZE; x++)
    {
        myArray[x] = (rand() % 20) + 1;
    }
    cout << "\n\nISMEMBER ARRAY FUNCTION\n";
    cout << "Enter an integer:  ";
    cin >> num;
    cout << "\nHere are the array values:  ";
    for (int x = 0; x < ARRAY_SIZE; x++)
    {
        cout << myArray[x] << " ";
    }
    
    // print if the value that the user entered is in the array or not here

    int results; // int created to store the return value
    results = isMember(myArray, ARRAY_SIZE, num);
    cout << endl
         << "Integer returned: " << results; // ERROR CHECKING
    if (results == num)
    {
        cout << "\n"
             << num << " is not in the array! Try again.";
    }
    else
    {
        cout << "\n"
             << num << " is in the array!" << endl;
    }
}

bool isMember(int *myArray, int ARRAY_SIZE, int num)
{
    int index = 0;      // Used as a subscript to search array
    int results = -1;   // To record position of search value
    bool found = false; // Flag to indicate if the value was found

    while (index < ARRAY_SIZE && !found)
    {
        if (myArray[index] == num) // If the value is found
        {
            found = true;             // Set the flag
            results = myArray[index]; // Record the value's subscript
            cout << endl
                 << endl
                 << "found it!"; // ERROR CHECKING
        }
        index++; // Go to the next element
    }
    // More ERROR CHECKING
    if (found == false)
    {
        cout << endl
             << "Found is False";
    }
    if (found == true)
    {
        cout << endl
             << "Found is True";
    }
    return results;
}



Aucun commentaire:

Enregistrer un commentaire