D
D
Dauren S2016-11-03 17:54:57
C++ / C#
Dauren S, 2016-11-03 17:54:57

Loop with check for number input C++?

You need to make an input loop, only integers, the number of attempts is up to 10.
The code below goes into an infinite loop.

int a;
    int arr[10];
    int i = 0;
    int k = 0;
    while (i < 10)
    {
      while (!(cin >> a))
      {
        cout << "Just a number";
        cin.clear();
      }
      i++;
    }

Answer the question

In order to leave comments, you need to log in

4 answer(s)
D
Dauren S, 2016-11-03
@dauren101

Here is the correct answer!
There is so much garbage on the Internet, when some copy-pasteers, in the hope of promoting their blog, distribute the wrong code without even testing it.
int demo_choice()
{
int a;
int i = 0;
while (!(cin >> a))
{
cin.clear();
cin.ignore(cin.rdbuf()->in_avail());
cout << "Invalid input!!! Enter number : ";
}
cout << a;
return 0;
}

T
tsarevfs, 2016-11-03
@tsarevfs

You end up with an infinite number of failed attempts. The cycle is not endless. After 10 successes, it should end.
Get rid of nested loops. Use break to exit on success.

D
display: block, 2017-01-16
@artekha

for(var i = first.length-1; i>=0; --i){
   if(second.indexOf(first[i]) == -1) return false;
}
return true;

D
ddd329, 2017-01-16
@ddd329

First you check the dimension, if it does not match, then they are not equal. If it matches, you need to somehow sort both the first and second array, and then compare element by element. On a vskidka something like this ...
And if an array element itself is an array, compare it too?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question