Вы можете добавить наивную проверку в свой код, контролируя, принадлежит ли каждое входное значение к интервалу [1,1000], и предлагая вставить правильные значения в случае неудачной проверки:
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int sum = 0;
int x, y, z;
cin >> x >> y >> z;
while(x<1 || x>1000 || y<1 || y>1000 || z<1 || z>1000){
cout << "Error: each input value must belong to [1,1000]!" << endl;
cin >> x >> y >> z;
}
sum = x + y + z;
cout << "The sum is " << sum << endl;
return 0;
}
Может быть, вы вместо этого main
хочу вернуть код ошибки, когда ввод не находится в заданном интервале:
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int sum = 0;
int x, y, z;
cin >> x >> y >> z;
if(x<1 || x>1000 || y<1 || y>1000 || z<1 || z>1000){
cout << "Error: each input value must belong to [1,1000]. Abort." << endl;
return -1;
}
sum = x + y + z;
cout << "The sum is " << sum << endl;
return 0;
}
Я предлагаю вставить инструкцию с инструкцией в начале, что-то вроде
cout << "Insert 3 values belonging to [1,1000]" << endl;