c++ - 无论我输入什么字符,我的程序都会循环

标签 c++ visual-c++ visual-studio-2015

我一直在用 C++ 开发一个非常非常基础的计算程序。它计算数字的平方根,如果用户需要,还可以对其进行平方。这是我目前所拥有的(我知道这可能是垃圾代码,但我只是一个初学者,只是想看看它是如何工作的。任何建议都非常感谢):

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

using namespace std;

int number;         // Global variables to be used in void functions as well as main.
int squaredNumber;
double sqrtResult;
char input;
char useAgain;

void squareNum();       // Prototypes for the void functions
void sqrtNum();
void useAgainQuery();

int main()
{
retry:                      // Establishing somewhere to send user if their input is invalid.
    system("cls");
    cout << "Square Calcualtions" << endl;
    cout << "******************" << endl;
    cout << endl;
    cout << "Do you want to square a number or find the square root of a number?" << endl;
    cout << "Please select 1 or 2 respectively." << endl;
    cout << endl;
    cin >> input;
    if (input == '1')
    {
        cout << "Please press ENTER to continue." << endl;
        cin.ignore().get();
        squareNum();                // If the input is 1, run the void to square a number.
    }
    else if (input == '2')
    {
        cout << "Please press ENTER to continue." << endl;
        cin.ignore().get();
        sqrtNum();                  // If the input is 2, run the void to sqrt a number.
    }
    else if (input != '1' || '2')
    {
        system("cls");
        cout << "Square Calcualtions" << endl;
        cout << "******************" << endl;
        cout << endl;
        cout << "Your selection was invalid, please enter 1 or 2." << endl;
        cin.ignore().get();
        goto retry;             // If the input isn't either 1 or 2, send back to the start of program.
    }
    return 0;
}

void squareNum()  // function to square the inputted number.
{
    system("cls");
    cout << "Square Calcualtions" << endl;
    cout << "******************" << endl;
    cout << endl;
    cout << "Enter the number you want to square." << endl;
    cin >> number;
    cout << "You have chosen: " << number << endl;
    cout << "Press ENTER to calculate." << endl;
    cin.ignore().get();
    system("cls");
    squaredNumber = number * number;        // Simple maths to find the square number
    cout << "You have squared " << number << "." << endl;
    cout << "The result was " << squaredNumber << "." << endl;
    cout << "Press ENTER to continue." << endl;
    cin.get();
    useAgainQuery();
    return;
}

void sqrtNum()
{
    system("cls");
    cout << "Square Calcualtions" << endl;
    cout << "******************" << endl;
    cout << endl;
    cout << "Enter the number you would like the square root of." << endl;
    cin >> number;
    cout << "You have chosen: " << number << "." << endl;
    cout << "Press ENTER to calculate." << endl;
    cin.ignore().get();
    system("cls");
    sqrtResult = sqrt(number);
    cout << "You have found the square root of " << number << "." << endl;
    cout << "The result was: " << sqrtResult << "." << endl;
    cout << "Press ENTER to continue." << endl;
    cin.get();
    useAgainQuery();
    return;
}

void useAgainQuery()
{
    system("cls");
    cout << "Square Calcualtions" << endl;
    cout << "******************" << endl;
    cout << endl;
    cout << "Would you like to make another calculation?" << endl;
    cout << "Y for Yes and N for No." << endl;
    cout << endl;
    cin >> useAgain;
    if (useAgain == 'Y' || 'y')
    {
    retry2:                     // Establishing somewhere to send user if their input is invalid.
        system("cls");
        cout << "Square Calcualtions" << endl;
        cout << "******************" << endl;
        cout << endl;
        cout << "Do you want to square a number or find the square root of a number?" << endl;
        cout << "Please select 1 or 2 respectively." << endl;
        cout << endl;
        cin >> input;
        if (input == '1')
        {
            cout << "Please press ENTER to continue." << endl;
            cin.ignore().get();
            squareNum();                // If the input is 1, run the void to square a number.
        }
        else if (input == '2')
        {
            cout << "Please press ENTER to continue." << endl;
            cin.ignore().get();
            sqrtNum();                  // If the input is 2, run the void to sqrt a number.
        }
        else if (input != '1' || '2')
        {
            system("cls");
            cout << "Square Calcualtions" << endl;
            cout << "******************" << endl;
            cout << endl;
            cout << "Your selection was invalid, please enter 1 or 2." << endl;
            cin.ignore().get();
            goto retry2;
        }
    }
    else if (useAgain != 'Y' || 'y')
        return;
    return;
}  

是的,当我通过并询问“你想再玩一次吗”时,它一遍又一遍地进行。我按什么键并不重要,但它会循环。任何帮助将不胜感激!

最佳答案

在这里更改您的条件:

    if (useAgain == 'Y' || 'y')

    if (useAgain == 'Y' || useAgain=='y')

另外,改变这个:

 else if (useAgain != 'Y' || 'y')
{
    return;
}

为此:

else if (useAgain != 'Y' && useAgain!='y')
{
    return;
}

关于c++ - 无论我输入什么字符,我的程序都会循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30242821/

相关文章:

wpf - 在客户端上不安装任何东西的情况下显示报告的最佳方式是什么

c++ - C++ 对象的生命周期是什么?

c++ - 尝试从 c 执行任何二进制文件

C++ 初始化静态数组

windows - 学习C++(Linux+QtCreator)我应该升级到Windows+VisualC+VisualAssist吗?比较如何?

C++ 终端中先前的文本被删除并被新文本替换

c++ - 升级后内循环性能下降是什么原因?

asp.net - 如何在 Visual Studio 2015 中运行 index.html 文件

c++ - 什么是 undefined reference /未解析的外部符号错误,我该如何解决?

c++ - 使用 VS 2015 的编译器警告 4456