gpt4 book ai didi

c++ - 我的计算器有问题

转载 作者:太空宇宙 更新时间:2023-11-04 14:55:13 24 4
gpt4 key购买 nike

我是 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();
}

最佳答案

需要解决一些问题。

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()
{

}

关于c++ - 我的计算器有问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18669918/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com