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

时间:2015-05-14 16:48:33

标签: 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;
}  

所以是的,当我经历并且它问“你想再玩一次”时,它会一遍又一遍地经历它。我按哪个键无关紧要但它循环。任何帮助将不胜感激!

4 个答案:

答案 0 :(得分:3)

在此更改您的情况:

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

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

另外,改变这个:

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

到此:

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

答案 1 :(得分:1)

  1. 不要使用goto,而是使用while(1)代替。
  2. 你的最后一句if语句错了,需要

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

答案 2 :(得分:1)

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

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

答案 3 :(得分:1)

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

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

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

您也可以在输入后转换案例:

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

还需要std::transformstd::string转换为所有较低或全部大写。