#Check if entry exists in array

24 messages · Page 1 of 1 (latest)

trim thicket
#

Hi all, I'm newish to c++ and am having trouble with an array. Essentially I'm writing a program where a random number is chosen, and then it checks if that number is found in an array. If found, it ends the loop and adds it to the array, otherwise it picks a new number and tries again.

Currently, all it does is output debug over and over.
What it should do is log debug a few times and then Question found and stop doing anything. I'm happy to provide more information as needed.

while (foundQuestion == false)
{
srand(time(0));
questionNumber = 1 + (rand() % totalQuestionNumber);
cout << "Debug";
for(int i = 0; i < 500; i++)
{
if(askedQuestions[i] == questionNumber)
{
cout << "Question found";
foundQuestion = true;
}
}
}
foundQuestion = false;
questionAskedCount++;

runic lintelBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For more information use !howto ask.

brittle cobaltBOT
#

@trim thicket, your formatted code (missing deletion permissions):
Hi all, I'm newish to c++ and am having trouble with an array. Essentially I'm writing a program where a random number is chosen, and then it checks if that number is found in an array. If found, it ends the loop and adds it to the array, otherwise it picks a new number and tries again.

Currently, all it does is output debug over and over.
What it should do is log debug a few times and then Question found and stop doing anything. I'm happy to provide more information as needed.

while (foundQuestion == false) {
    srand(time(0));
    questionNumber = 1 + (rand() % totalQuestionNumber);
    cout << "Debug";
    for (int i = 0; i < 500; i++) {
        if (askedQuestions[i] == questionNumber) {
            cout << "Question found";
            foundQuestion = true;
        }
    }
}
foundQuestion = false;
questionAskedCount++;```
half flame
#

If found, it ends the loop and adds it to the array

#

what?

#

you mean you add it to a different array, right?

trim thicket
#

It's supposed to add it to the askedQuestions array

half flame
#

if found?

#

I don't get it. You search an array A for a thing, and if it is inside, you... add it again?

trim thicket
#

Here's a basic explanation

generate a random number
check if number is found in askedQuestions array
if in the array, generate a new number and try again
if not in the array, add number to the array and end the loop

half flame
#

if not

#

I have a feeling you're just trying to shuffle questions

#

but you're now focusing on a single method

#

instead of getting the result

#

am I right?

trim thicket
#

yeah pretty much, but I don't want them to repeat

half flame
#

;compile cpp

#include <random>
#include <vector>
#include <algorithm>
#include <iostream>
#include <string>

int main() {

  auto rng = std::default_random_engine( std::random_device{}() );

  auto questions = std::vector< std::string >{
    "Is 2 + 2 equal to 4?",
    "What is Pi equal to?",
    "What is 10 squared?",
    "What is the highest mountain on earth?",
    "What is the longest river on our planet?"
  };
  std::shuffle(questions.begin(), questions.end(), rng);

  std::cout << "Shuffled questions:\n";
  for (auto& q: questions)
    std::cout << "- " << q << '\n';
}
```\
weak timberBOT
#
Program Output
Shuffled questions:
- What is the longest river on our planet?
- Is 2 + 2 equal to 4?
- What is the highest mountain on earth?
- What is 10 squared?
- What is Pi equal to?
half flame
#

use std::shuffle with a random engine

trim thicket
#

I'll look into it thanks

#

!solved

runic lintelBOT
#

Thank you and let us know if you have any more questions!

#

[SOLVED] Check if entry exists in array

runic lintelBOT
#

@trim thicket

This question thread is being automatically marked as solved.