void test() {
int i ,j;
cout << "enter the i and j" << endl;
cin >> i >> j;
if (j <= 5 && j > 0 && i > 0 && i <= 9) {
cout << "right" <<endl;
} else {
cout << "error" << endl;
test();
}
}
int main(int argc, const char * argv[]) {
test();
}
How to check the input from command line is totally correct?
below is some false test that we should run the code in the else
part.
foo ags
but the result in the command line is 28 rows of error information. But What I want is just one line code show "error"
What's the problem?
Another
below is my C++ code:
void test(int array[], int length) {
int index; // the index of heap array that human want to modify
int num; // the number of heap in the index position
cout << "input the index and num" << endl << flush;
string si,sj;
try{
cin >> si >> sj;
index = stoi(sj);
num = stoi(si);
}catch(std::exception e){
cout << "error, try again" << endl;
test(array, length);
}
if (index <= length && index > 0 && num > 0 && num <= array[index - 1]) {
array[index - 1] -= num;
// print(array, length);
} else {
cout << "error, try again" << endl;
test(array, length);
}
}
And now there is a shell to run this code, but in the shell, there exist input like below:
input the index and num 2 1
this is the correct one
input the index and num 2
it just have 1 value, and the program blocked here to wait for another input, i should figure that out and output "error, try again"
input the index and num 1 2 3
this is also incorrect because there are more than 2 input value. the same, I should figure that out and output "error, try again"
How to deal with this?