简体   繁体   中英

C++ How to check the number of the input from Shell

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?

First, you need to read input as string, then convert string to int with std::stoi checking for errors. Assuming you need ony "error" message, use try-catch block catching only std::exception and output "error". Second, to repeat call to test() in case of error, you need to use some kind of lopp and don't call test() from inside of test(). This technique called recursive call and is used for another purposes, not for simple repeating. I've modified your test() function to return bool value and it will return true in case of success and false in case of error, then call it from while() loop, which will repeat call in case of false returned. About you second question, you need to input numbers separately, each on distinct line. The program will be able to check each number individually. See code:

#include <string>
#include <iostream>
bool test() {
    int i ,j;
    std::string si,sj;
    try{
        cout << "enter i:" << endl;
        cin >> si;
        i = std::stoi(si);
        cout << "enter j:" << endl;
        cin >> sj;
        j = std::stoi(sj);
    }catch(std::exception e){
        cout << "error";
        return false;
    }
    if (j <= 5 && j > 0 && i > 0 && i <= 9) {
        cout << "right" <<endl;
        return true;
    } else {
        cout << "error" << endl;
        return false;
    }
}

int main(int argc, const char * argv[]) {
    while(!test()){}
}

cin >> i >> j; just skips leading whitespace, then reads two formatted int values separated by whitespace. If you enter more like in your example the rest stays in the input stream buffer. If you call test() again, cin reads from that buffer.

You could use cin.ignore(numeric_limits<streamsize>::max()) to solve that issue as it clears the buffer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM