do{...}while(...) loop to chech array?

Hi!

I have an array with numbers from the user. My task is to then std::cin numbers and check if it's in the array. If it is, the program should quit the loop. If it is in the array, it should ask for another number to check.

Here's my code so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
  #include <iostream>
#include <array>
#include <algorithm>

#define DB 4

int main() {

  std::array<int, 10> numsies;
  int num = 0, counter = 0;
  bool isIn = true;

  for(int indi = 0; indi < DB; indi++){
    std::cout << indi + 1 << ". szám: ";
    std::cin >> numsies[indi];
    
  }

  do{

    std::cout << "Adjon meg egy számot: ";
    std::cin >> num;
    
    if(std::find(numsies.begin(), numsies.end(), num) != numsies.end()){
      isIn = false;
    }else{
      isIn = true;
    }

    if(isIn == false){
      break;
    }
    
  }while(!isIn);
  
  return 0;
}


(I'm not entirely new to C++, but this input validation thing just doesn't want to work.. could you help me, please?)

Thanks in advance!!
Do you mean something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <array>
#include <algorithm>

constexpr size_t DB {4};

int main() {
	std::array<int, DB> numsies;

	std::cout << "Enter " << DB << " Numbers\n";

	for (size_t indi {}; indi < DB; ++indi) {
		std::cout << indi + 1 << ". szám: ";
		std::cin >> numsies[indi];
	}

	int num {};

	do {
		std::cout << "Adjon meg egy számot: ";
		std::cin >> num;

	} while (std::find(numsies.begin(), numsies.end(), num) != numsies.end());
}

Did you mean you want to ask for another number if the number was NOT found?

A while loop can also work:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <array>
#include <algorithm>

int main()
{
   constexpr size_t DB { 4 };

   std::array<int, DB> numsies { };

   std::cout << "Enter " << DB << " Numbers\n";

   for (size_t indi { }; indi < DB; ++indi)
   {
      std::cout << indi + 1 << ": ";
      std::cin >> numsies[indi];
   }

   while (true)
   {
      std::cout << "Number to find? ";
      int num { };
      std::cin >> num;

      auto found { std::find(numsies.begin(), numsies.end(), num) };

      if (found != numsies.end())
      {
         std::cout << num << " was found!\n";
         break;
      }
   }

   std::cout << "Done!\n";
}
Enter 4 Numbers
1: 5
2: 10
3: 15
4: 20
Number to find? 23
Number to find? -50
Number to find? 5
5 was found!
Done!
The OP isn't really clear.

If it is in the array, it should ask for another number to check


If it is, the program should quit the loop


These are, of course, mutually exclusive. I took the first requirement. If the second, then my L23 becomes:

 
} while (std::find(numsies.begin(), numsies.end(), num) == numsies.end());

Last edited on
I did the same thing with my code, if the search found the number in the container bail. Otherwise request another number to find.

Want to loop when a number is found? My line 27 if checks for equality with a modified output of num NOT found and jumps the shark.
seeplus's first sugestion worked perfectly fine, just what I wanted my program to do :D Thanks a bunch!! And also thank you to the others, as well!

P.S.: Sorry for the half-Hungarian code ^^'
Topic archived. No new replies allowed.