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 に答える
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

1) と 3) を除いて、jmstoker が言ったことをすべて実行します。代わりに、コードを次のように変更します

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 に答える