S
S
Samoniel2020-11-30 09:31:48
C++ / C#
Samoniel, 2020-11-30 09:31:48

How to repeatedly ask to enter the data until the correct ones are entered?

It seems that some kind of simple cycle is needed, I don’t remember which one. The user enters incorrect data, and the program should constantly ask him to enter the correct ones

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
Samoniel, 2020-11-30
@Samoniel

#include<iostream>
using namespace std;

int main()
{
  long long int a;
  cout << "Введите пятизначное число: ";
  cin >> a;
  if(a >= 10000 && a <= 99999)
  {
    int f = a % 10;
    int e = (a / 10) % 10;
    int d = (a / 100) % 10;
    int c = (a / 1000) % 10;
    int b = (a / 10000) % 10;
    cout << b << endl;
    cout << c << endl;
    cout << d << endl;
    cout << e << endl;
    cout << f << endl;
  }
  else
  {


    do
    {
      cout << "ТЫ ЧЁ ДУРАК БЛЯТЬ?" << endl;
      cout << "Христом богом заклинаю, только пятизначные, плес. Пробуй: " << endl;

      cin >> a;
      if (a >= 10000 && a <= 99999)
      {

        int f = a % 10;
        int e = (a / 10) % 10;
        int d = (a / 100) % 10;
        int c = (a / 1000) % 10;
        int b = (a / 10000) % 10;
        cout << "Число номер 1: " << b << endl;
        cout << "Число номер 2: " << c << endl;
        cout << "Число номер 3: " << d << endl;
        cout << "Число номер 4: " << e << endl;
        cout << "Число номер 5: " << f << endl;
      }
      
    }
    

    while (a > 99999 || a < 10000);
  }
  
  
  

  return 0;
}

R
res2001, 2020-11-30
@res2001

That's not what you're asking.
Any cycle can be used:

for(;;) {...}
while(true) {...}
do {...} while(true)

Exit the loop with break when the input is normal or change the loop condition.
In fact, an eternal loop is not needed in this case. It is best to provide, for example, 3 attempts to enter, and if not successful, we end. In this case, for() is the best fit.
In general, in C/C++, for and while loops have the same functionality, but for is more suitable for counters and the like, because in it, the initialization, condition and end of the loop are collected in one line (more compact than while). The same can be done in while, but the initialization and ending will have to be written separately.

6
6db, 2020-11-30
@6db

Works until the user enters the required data. ( Until _a equals 7) . I apologize for the code, I am writing from the phone on the road.

#include <iostream>

using namespace std;

int main() {
int _a;
  do
  {
    cout<<"'Input a: ";
    cin>>_a;
  } 
         while(_a!=7);
           cout<<"Correct input\n";
}

Same with for
#include <iostream>

using namespace std;

int main(){
  
  for(int _a;  _a!=7; cin>> _a ){
    
    cout<<"'Input a: ";
    
}

}

Conclusion:
5fc49e0e216f3168808506.jpeg

R
Roman, 2020-11-30
@myjcom

#include <iostream>
using namespace std;

struct MyData {
  int value;
  MyData(int v) : value(v){}
  MyData() : value(0){}
};

istream& operator>>(istream& is, MyData& data)
{
  is >> data.value;
  return is;
}

ostream& operator<<(ostream& os, MyData& data)
{
  os << data.value;
  return os;
}

bool operator==(const MyData& lh, const MyData& rh)
{
  return lh.value == rh.value;
}

bool operator!=(const MyData& lh, const MyData& rh)
{
  return !(lh.value == rh.value);
}

MyData try_read_while_not(MyData d, const string& message)
{
  MyData val;
  while(val != d)
  {
    cout << message;
    cin  >> val;
  }
  return val;
}

int main()
{
  MyData correct(42);
  MyData val = try_read_while_not(correct, "Введите значение: ");
  cout << val;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question