0

我是 C++ 新手。我的第一个目标是让一个成功的计算器程序成为 Win32 控制台应用程序,但我不断收到错误消息。我把这段代码:

cout << "Do you want to continue? N/Y" << endl;
cin  >> ny;

if (ny == "Y") goto start;
if (ny == "N") goto end;

但无论哪种方式,它都会继续结束。

这是“结束”的代码:

// End - Properties
system("cls");
system("title Basic Calculator -  End");
system("color 4F");

// End - Start
ny == "0";
cout << "Are you sure you want to end? N/Y" << endl;
cin  >> ny;

if (ny == "N") goto start;

cin.get();

return 0();

最后它也总是结束程序。

如果您发现错误,请告诉我。

-丹麦胡梅尔

完整代码:

#include <iostream>

using namespace std;

int main()
{
start:
    // Program - Properties
    system("cls");
    system("title Basic Calculator - Main Screen");
    system("color 1F");

    // Program - Setup
    int input;
    int x;
    int y;
    char ny [10];

    // Program - Start
    cout << "Please choose an operation from the following." << endl << endl;
    cout << "1. Addition \n2. Subtraction \n3. Multiplication \n4. Division" <<endl << endl;
    cin  >> input;
    if (input = 1) goto addition;
    if (input = 2) goto subtraction;
    if (input = 3) goto multiplication;
    if (input = 4) goto division;
    cin.get();

addition:

    // Addition - Properties
    system("cls");
    system("title Basic Calculator - Addition");
    system("color 2F");

    // Addition - Start
    cout << "Please input your first number." << endl;
    cin  >> x;
    cout <<endl << "Please input your second number."<< endl << endl;
    cin  >> y;
    cout <<endl <<endl << "The answer is " << x+y << ".\a" << endl << endl;
    cout << "Do you want to continue? N/Y" << endl;
    cin  >> ny;

    if (ny == "Y") goto start;
    if (ny == "N") goto end;

    cin.get();

subtraction:

multiplication:

division:

end:
    // End - Properties
    system("cls");
    system("title Basic Calculator -  End");
    system("color 4F");

    // End - Start
    ny == "0";
    cout << "Are you sure you want to end? N/Y" << endl;
    cin  >> ny;

    if (ny == "N") goto start;

    cin.get();

    return 0();
}
4

4 回答 4

4

有几个问题需要解决。

1)声明 ny 因为std::string ny;你需要添加#include <string>. 这将避免缓冲区溢出。

2)如前所述,您需要更改 if 语句。

if (input == 1) goto addition;  // Use '==' for comparison
if (input == 2) goto subtraction;
if (input == 3) goto multiplication;
if (input == 4) goto division;    

3) 确保检查小写的 y 和 n

if (ny[0] == 'Y' || ny[0] == 'y') goto start;  // notice the single quotes
if (ny[0] == 'N' || ny[0] == 'n') goto end;  

// ...
// Also change the following
ny[0] = '\0';  // Not really necessary since you assign it immediately after
// ...
if (ny[0] == 'N' | ny[0] == 'n')

4) 你的return说法不正确。将其更改为:

return 0;  // Doesn't need parenthesis

作为一名专业程序员,我必须建议您不要使用 goto 语句并将您的算法封装在函数中。以下是基于您的原始代码的示例。仅供参考,我已经验证它可以在 Visual Studio 2010 Professional 上编译

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

// Forward declarations
void addition();
void subtraction();
void multiplication();
void division();

int main()
{
    bool again = true;

    // Program - Setup
    int input;
    std::string ny;

    while(again)
    {
        // Program - Properties
        system("cls");
        system("title Basic Calculator - Main Screen");
        system("color 1F");

        // Program - Start
        cout << "Please choose an operation from the following." << endl << endl;
        cout << "1. Addition \n2. Subtraction \n3. Multiplication \n4. Division" <<endl << endl;
        cin  >> input;
        cin.get();

        if (input == 1) {addition();}
        else if (input == 2) {subtraction();}
        else if (input == 3) {multiplication();}
        else if (input == 4) {division();}
        else 
        {
            cout << "Invalid input\n"; 
            again = false;
        }

        cout << "Do you want to continue? N/Y" << endl;
        cin  >> ny;
        cin.get();

        if (ny[0] == 'Y' || ny[0] == 'y')
        {
            again = true;    
        }
        else
        {
            // Ask if they are sure
            system("cls");
            system("title Basic Calculator -  End");
            system("color 4F");

            cout << "Are you sure you want to end? N/Y" << endl;
            cin  >> ny;
            cin.get();

            if (ny[0] == 'Y' || ny[0] == 'y')
            {
                again = false; 
            }
            else
            {
                again = true;
            }
        }
    }

    return 0;
}

void addition()
{
    int x;
    int y;
    // Addition - Properties
    system("cls");
    system("title Basic Calculator - Addition");
    system("color 2F");

    // Addition - Start
    cout << "Please input your first number." << endl;
    cin  >> x;
    cout <<endl << "Please input your second number."<< endl << endl;
    cin  >> y;
    cout <<endl <<endl << "The answer is " << x+y << ".\a" << endl << endl;
}

void subtraction()
{

}

void multiplication()
{

}

void division()
{

}
于 2013-09-07T06:05:03.200 回答
3

if (input = 1) goto 加法

我认为您应该检查(输入== 1),但您将其分配给输入(输入= 1)。

代码应该是:-

if (input == 1) goto addition;
if (input == 2) goto subtraction;
if (input == 3) goto multiplication;
if (input == 4) goto division;
于 2013-09-07T05:09:23.140 回答
1

我做了一些更改,1)将 ny 声明为字符而不是数组。2) 检查小写字母和大写字母。

我在进行更改的地方添加了评论。我希望这有帮助。

    // #include "stdafx.h" //If you get error include this
    #include <iostream>

    using namespace std;

    int main()
    {
    start:
        // Program - Properties
        system("cls");
        system("title Basic Calculator - Main Screen");
        system("color 1F");

        // Program - Setup
        int input;
        int x;
        int y;
        char ny;  //Dont declare it as array ny[10]

        // Program - Start
        cout << "Please choose an operation from the following." << endl << endl;
        cout << "1. Addition \n2. Subtraction \n3. Multiplication \n4. Division" <<endl << endl;
        cin  >> input;
        if (input == 1) goto addition;
        if (input == 2) goto subtraction;
        if (input == 3) goto multiplication;
        if (input == 4) goto division;
        cin.get();

    addition:

        // Addition - Properties
        system("cls");
        system("title Basic Calculator - Addition");
        system("color 2F");

        // Addition - Start
        cout << "Please input your first number." << endl;
        cin  >> x;
        cout <<endl << "Please input your second number."<< endl << endl;
        cin  >> y;
        cout <<endl <<endl << "The answer is " << x+y << ".\a" << endl << endl;
        cout << "Do you want to continue? N/Y" << endl;
        cin  >> ny;

        if (ny == 'Y'|| ny == 'y') goto start;   //Check for both Y & y
        if (ny == 'N' || ny == 'n') goto end;    //Check for both N & n

        cin.get();

    subtraction:

    multiplication:

    division:

    end:
        // End - Properties
        system("cls");
        system("title Basic Calculator -  End");
        system("color 4F");

        // End - Start

        cout << "Are you sure you want to end? N/Y" << endl;
        cin  >> ny;

        if (ny == 'N' || ny == 'n') goto start;

        cin.get();

        return 0;
    }
于 2013-09-07T06:23:31.613 回答
1

做 jmstoker 所说的所有事情,除了 1) 和 3)。而是将您的代码更改为

if (ny[0] == 'Y') goto start;
if (ny[0] == 'N') goto end;

它至少在我的编译器中工作(Microsoft Visual C++ 2011 Express)

这是完整的代码

#include <iostream>

using namespace std;

int main()
{
start:
    // Program - Properties
    system("cls");
    system("title Basic Calculator - Main Screen");
    system("color 1F");

    // Program - Setup
    int input;
    int x;
    int y;
    char ny [10];

    // Program - Start
    cout << "Please choose an operation from the following." << endl << endl;
    cout << "1. Addition \n2. Subtraction \n3. Multiplication \n4. Division" <<endl << endl;
    cin  >> input;
    if (input == 1) goto addition;
    if (input == 2) goto subtraction;
    if (input == 3) goto multiplication;
    if (input == 4) goto division;
    cin.get();

addition:

    // Addition - Properties
    system("cls");
    system("title Basic Calculator - Addition");
    system("color 2F");

    // Addition - Start
    cout << "Please input your first number." << endl;
    cin  >> x;
    cout <<endl << "Please input your second number."<< endl << endl;
    cin  >> y;
    cout <<endl <<endl << "The answer is " << x+y << ".\a" << endl << endl;
    cout << "Do you want to continue? N/Y" << endl;
    cin  >> ny;

    if (ny[0] == 'Y') goto start;
    if (ny[0] == 'N') goto end;

    cin.get();

subtraction:

multiplication:

division:

end:
    // End - Properties
    system("cls");
    system("title Basic Calculator -  End");
    system("color 4F");

    // End - Start
    cout << "Are you sure you want to end? N/Y" << endl;
    cin  >> ny;

    if (ny[0] == 'N') goto start;

    cin.get();

    return 0;
}
于 2013-09-07T06:24:42.443 回答