首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我的计算器出问题了

我的计算器出问题了
EN

Stack Overflow用户
提问于 2013-09-07 05:03:13
回答 4查看 549关注 0票数 0

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

代码语言:javascript
复制
cout << "Do you want to continue? N/Y" << endl;
cin  >> ny;

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

但不管是哪种结局都会持续下去。

这是“结束”的代码:

代码语言:javascript
复制
// 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();

最后,它也总是结束程序。

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

-Danish Humair

完整法典:

代码语言:javascript
复制
#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();
}
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2013-09-07 06:05:03

有几个问题需要解决。

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

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

代码语言:javascript
复制
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。

代码语言:javascript
复制
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语句是不正确的。改为:

代码语言:javascript
复制
return 0;  // Doesn't need parenthesis

作为一名专业程序员,我必须建议您不要使用goto语句,并将算法封装在函数中。下面是一个基于原始代码的示例。FYI,我已经证实它是在Visual 2010专业版上编译的

代码语言:javascript
复制
#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()
{

}
票数 4
EN

Stack Overflow用户

发布于 2013-09-07 05:09:23

如果(输入= 1)转到加法

我认为您应该检查(输入== 1),但是您将它赋值给input (input= 1)。

守则应为:

代码语言:javascript
复制
if (input == 1) goto addition;
if (input == 2) goto subtraction;
if (input == 3) goto multiplication;
if (input == 4) goto division;
票数 3
EN

Stack Overflow用户

发布于 2013-09-07 06:23:31

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

我已经添加了评论,无论我做了什么改变。我希望这能帮到你。

代码语言:javascript
复制
    // #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;
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18669918

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档