In a program that runs n times by taking the number n, how do I end the current run and move on to the next run?

Asked 2 years ago, Updated 2 years ago, 22 views

If there is a code that repeats the code 10 times the same, the current stage is the 5th, but if I receive an incorrect input during execution, I would like to print out Wrong input and move on to the 6th execution. If you use exit, the entire program will shut down completely without moving on to the sixth run. When I write try-catch, I have to enter a conditional expression when I write throw, but I don't know what to write about else. It's not like a=b. I also know that try-catch should not be used in the original function that is not used by the user. How can we solve this problem?

If the exit (0) of the comment section is removed from the code, the sentence within the conditional statement of the main function is printed along with Wrong Input.

If you correctly take out the conditional statement of the main function and output only one of Correct / InCorrect / Wrong Input, the problem will be solved, but I also can't even try to write a conditional expression for the condition of the else part ㅠ<

bool areParanthes Balanced(string exp) { // function that performs push, pop according to parentheses
    ArrayStack<char>  S;
    for (int i = 0; i < exp.length(); i++) {
        if (exp[i] == '(' || exp[i] == '{' || exp[i] == '[') // open parentheses push
            S.push(exp[i]);
        else if (exp[i] == '') || exp[i] == '} || exp[i] == ']') { // Closed parentheses pop
            If (S.isEmpty() || !arePair(S.top(), exp[i])) //False if empty or mismatched 
                return false;
            else
                S.pop();
        }
        else {
            cout << "Wrong Input\n"; 
            How do I exit (0); // exit to exit the entire program and just exit the current run?
        }
    }
    return S.isEmpty() ? true : false;
}

int main()
{
    int repeatcount = 0;
    sin >> repeat count; // Enter the number of iterations of the program

    while (repeatcount--) {
        string expression;
        cout << "Enter an expression: "; 
        sin >> expression; // Enter parentheses and keywords

        if (expression == string("X") || expression == string("x") // exit program when X is entered during execution
            exit(0);
        else if (areParanthes Balanced (expression)) { // parentheses, output 'Correct' if the order is correct
            cout << "Correct\n";
        }
        else { // parentheses, out of order 'InCorrect'
            cout << "InCorrect\n";
        }
    }
}

c++

2022-09-22 20:35

1 Answers

It seems to be solved by passing the return value as an enumeration, not a bowl.

// To pass the return value as an enumeration.
// In simple code, it's not bad to do this.
// However, later on, complex projects need to be more structured.
enum class ResultType
{
    WRONG_INPUT,
    CORRECT,
    INCORRECT,
};

// We recommend that you use a const reference to take over objects as parameters.
// That way, unnecessary copies won't happen.
ResultType areParanthesBalanced(const string&exp) { // Function that performs push, pop according to parentheses
    ArrayStack<char> S;
    for (int i = 0; i < exp.length(); i++) {
        // It is recommended that the frequently used reference values be made into variables.
        char curChar = exp[i];

        // It is recommended to use the brackets uniformly.
        if (curChar == '(' || curChar == '{' || curChar == '[') { // open parentheses push
            S.push(curChar);
        }
        else if (curChar == '') || curChar == '} || curChar == ']') { // Closed parentheses pop
            If (S.isEmpty() || !arePair(S.top(), curChar)) //False if empty or mismatched 
                return ResultType::INCORRECT;
            else
                S.pop();
        }
        else {
            return ResultType::WRONG_INPUT;
        }
    }
    return S.isEmpty() ? ResultType::CORRECT : ResultType::INCORRECT;
}

int main()
{
    int repeatcount = 0;
    sin >> repeat count; // Enter the number of iterations of the program

    // I think a for statement would be more appropriate here than while.
    // // while (repeatcount--) {
    for (int i = 0; i < repeatcount; i++) {
        string expression;
        cout << "Enter an expression: ";
        sin >> expression; // Enter parentheses and keywords

        // I didn't touch it here
        // It is best not to use exit().
        if (expression == string("X") || expression == string("x") // exit program when X is entered during execution
            exit(0);

        // Enumerations can be received by switch.
        switch (areParanthesesBalanced(expression)) {
        caseResultType:::WRONG_INPUT: // If there is an incorrect input
            cout << "Wrong Input\n";
            break; // break here means to exit the switch door. 
        caseResultType:::CORRECT: // parentheses, if the order is correct
            cout << "Correct\n";
            break;
        caseResultType:::INCORRECT: // parentheses, if the order is not correct
            cout << "InCorrect\n";
            break;
        }
    }
}

PS. If you look at the code you're asking, you always use exit(0), which is not a good habit.

In order for the program to exit safely, you have to do a number of tasks accordingly. For example, if you have made a dynamic assignment through new in the middle of a program, you need to delete it before it ends.

However, if you put a code like exit(0), it's forced to shut down at that point, so you can't do additional tasks.

Therefore, it is recommended to make a habit of writing code so that the final termination of the program is unconditionally a return of the main function.


2022-09-22 20:35

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.