int* n; cout << "\n Введите количество элементов массива: "; auto old_state = cin.rdstate(); cin >> *n; while ((cin.bad() == false) || (*n <= 0) ) { cout << "\n Ввод некорректен!!! " << "\n Нужно вести целое положительное число в интервале " << "\n от 0 до 100 000" << endl; cout << "\n Введите количество элементов массива: "; cin.setstate(old_state); cin >> *n; } 1 answer
To begin with, your program has undefined behavior. You have declared a pointer, although it is not clear why, but you did not allocate memory that will be addressed by this pointer, and where you are going to enter the data.
int* n; cout << "\n Введите количество элементов массива: "; auto old_state = cin.rdstate(); cin >> *n; There is no point in declaring a pointer instead of an integer type variable.
If you are going to enter only positive values, then declare this variable as unsigned. for example
unsigned int n; When entering data, if the wrong character was typed, then the buffer should be cleared. Otherwise, each successive attempt to read the buffer will bump into this incorrect character in the buffer.
In addition, the input stream has a clear method, which resets the error state of the stream.
Below is a demo program that shows how input can be organized. If desired, you can also add a check for the end of the input stream when the user does not want to enter anything, and in this case, interrupt the program, or assign the value n to the variable n .
#include <iostream> #include <limits> int main() { const unsigned int MAX_VALUE = 100000; int unsigned n; std::cout << "\n Введите количество элементов массива: "; while ( not ( std::cin >> n ) || MAX_VALUE < n ) { std::cin.clear(); std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' ); std::cout << "\n Ввод некорректен!!! " << "\n Нужно вести целое положительное число в интервале " << "\n от 0 до " << MAX_VALUE << std::endl; std::cout << "\n Введите количество элементов массива: "; } std::cout << "n = " << n << std::endl; return 0; } If you consistently enter the following values
a b 1000000 10 then the dialogue with the program will be as follows
Введите количество элементов массива: a Ввод некорректен!!! Нужно вести целое положительное число в интервале от 0 до 100000 Введите количество элементов массива: b Ввод некорректен!!! Нужно вести целое положительное число в интервале от 0 до 100000 Введите количество элементов массива: 1000000 Ввод некорректен!!! Нужно вести целое положительное число в интервале от 0 до 100000 Введите количество элементов массива: 10 n = 10 - The pointer I used is because this part of the code was a function that makes a parameter request. Thanks for the good answer !!! - Semyon Labzov
- @SemyonLabzov I thought so, but that, as they say, written in pen, you can’t cut it out with an ax. :) - Vlad from Moscow
- And I still have a question. If I use unsigned int, and the user enters a negative number, then it will be possible to catch an error - Semyon Labzov
- @SemyonLabzov Just like with other incorrect input, there will be exactly the same error, that is, an invalid character. You do not need to select this particular case, since in your request you indicate that you need to enter a non-negative number. - Vlad from Moscow
*nindicate? - PinkTux