【问题标题】:My program will loop regardless of whatever char I enter无论我输入什么字符,我的程序都会循环
【发布时间】:2015-05-14 16:48:32
【问题描述】:

我一直在用 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;
}  

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

【问题讨论】:

  • 我建议将 goto retry 替换为 do-whilewhile 循环。
  • @ThomasMatthews 我该怎么做呢?我听说 goto 不好用,但是如何使用 while 循环?
  • 您在学习中是否覆盖了forwhiledo-while 循环?我喜欢:bool user_error = true; while (user_error) { /*...*/};。如果没有用户错误(在循环内),则将该变量设置为 false。如果没有用户错误,有些人使用while(true),然后使用break 语句。

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


【解决方案1】:

在此处更改您的条件:

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

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

另外,改变这个:

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

到这里:

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

【讨论】:

  • 谢谢!这非常有效。只是出于好奇,你为什么要使用 && 而不是仅仅重复与上面相同的过程?
  • @IainAnderson: 满足这两个条件,即只有当useAgain 既不是y 也不是Y 时它才应该为真。
【解决方案2】:

也许尝试创建一个控制整个主循环的布尔变量,如下所示:

#include <InsertLibrariesHere>
int main(){
    bool running=true;
    while(running){
        //calculations here
        //continue(Y/N)?
        if (input == N || input == n){running = false;}
    }
}

【讨论】:

    【解决方案3】:
    1. 不要使用 goto,而是使用 while(1)。
    2. 你的最后一个 if 语句是错误的,它应该是

      if (input != '1' &amp;&amp; input != '2')

    【讨论】:

    • 谢谢,添加 && 修复了它。 while(1) 是如何工作的?我将如何实现它以成为 goto 的更好替代方案?
    • 它会循环直到你在特定条件下使用“break”语句,比如输入'q'退出应用程序。
    【解决方案4】:

    您可以简化与字母的比较:

    if (std::toupper(useAgain) == 'Y')
    

    if (std::tolower(useAgain) == 'y')
    

    输入后也可以转换大小写:

    cin >> useAgain;
    useAgain = std::toupper(useAgain);
    if (useAgain == 'Y')
    

    还有std::transform 是您需要将std::string 转换为全部小写或全部大写。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-14
      • 2020-06-03
      • 1970-01-01
      • 2021-06-17
      • 2020-11-19
      • 2023-03-22
      • 2020-02-16
      • 1970-01-01
      相关资源
      最近更新 更多