我该如何解决这个无限循环以及代码中的其他问题?

How do I fix this infinite loop, and other problems in my code?

这是我的作业 link:http://prnt.sc/e9q619

switch 中的默认值如果满足会变成一个无限循环,而且我在做作业中的其他重要注释时也遇到了麻烦。当我去找另一位导师时,他试图告诉我以我们在 class 中还没有学到的方式使用向量和字符串,所以如果 'c' 和 'x' 到使用不固定。

    #include <iostream>
#include <stdlib.h>

using namespace std;


int main()
{
    int i = 1;
    int Num1 = 1;
    int Num2 = 1;
    char operation;
    int answer = 1;

    cout << "Here is a four function integer calculator." << endl;
    cout << "Please use 'x' to exit the calculator, and 'c' to clear the calculator in the operator space. E.G. ""1 x 3"" would exit the calculator." << endl;
    cout << "Enter the first number, operation, and second number in respective order, separated by a space each." << endl;


    while (i > 0)
    {
        cin >> Num1 >> operation >> Num2;
        switch (operation)
        {
        case '+':
            answer = Num1 + Num2;
            cout << "The solution is: " << answer << "." << endl;
            break;
        case '-':
            answer = Num1 - Num2;
            cout << "The solution is: " << answer << "." << endl;
            break;
        case '*':
            answer = Num1 * Num2;
            cout << "The solution is: " << answer << "." << endl;
            break;
        case '/':
            if (Num2 == 0 && operation != 'x') {
                break;
            }
            else {
                answer = Num1 / Num2;
                cout << "The solution is: " << answer << "." << endl;
            }
            break;
        case 'c':
            system("cls");
            break;
        case 'x':
            return 0;
            break;
        default:
            cout << "You entered a wrong number, operator, or form for the entire operation" << endl;
            break;
        }
    }
    cout << "The solution is: " << answer << "." << endl;
}

所以我所做的是分别检查每个输入并编写输入验证语句。

#include<iostream>
#include<stdlib.h>
using namespace std;

int main()
{
    int i = 1;
    int Num1 = 1;
    int Num2 = 1;
    char operation;
    int answer = 1;

    cout << "Here is a four function integer calculator." << endl;
    cout << "Please use 'x' to exit the calculator, and 'c' to clear the calculator in the operator space. E.G. ""1 x 3"" would exit the calculator." << endl;
    cout << "Enter the first number, operation, and second number in respective order, separated by a space each." << endl;


    while (i > 0)
    {
        cout << "Enter Num1: " << endl;
        cin >> Num1;
        cout << "Enter operation: " << endl;
        cin >> operation;
        cout << "Enter Num2: " << endl;
        cin >> Num2;

        while (operation != '+' && operation != '-' && operation != '*' && operation != '/')
        {
            cout << "You Suck. Try again." << endl;
            cout << "Enter Num1: "  << endl;
            cin >> Num1;
            cout << "Enter operation: " << endl;
            cin >> operation;
            cout << "Enter Num2: " << endl;
            cin >> Num2;
        }

        switch (operation)
        {
        case '+':
            answer = Num1 + Num2;
            cout << "The solution is: " << answer << "." << endl;
            break;
        case '-':
            answer = Num1 - Num2;
            cout << "The solution is: " << answer << "." << endl;
            break;
        case '*':
            answer = Num1 * Num2;
            cout << "The solution is: " << answer << "." << endl;
            break;
        case '/':
            if (Num2 == 0 && operation != 'x') {
                break;
            }
            else {
                answer = Num1 / Num2;
                cout << "The solution is: " << answer << "." << endl;
            }
            break;
        case 'c':
            system("cls");
            break;
        case 'x':
            return 0;
            break;
        default:
            cout << "You entered a wrong number, operator, or form for the entire operation" << endl;
            break;
        }
    }
    cout << "The solution is: " << answer << "." << endl;
}