我的计算器有问题
I am having trouble with my Calculator
我是C++新手。我的第一个目标是使一个成功的计算器程序成为 Win32 控制台应用程序,但我不断收到错误。我把这个代码:
cout << "Do you want to continue? N/Y" << endl;
cin >> ny;
if (ny == "Y") goto start;
if (ny == "N") goto end;
但无论哪种方式,它都会继续结束。
这是"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] = ' '; // 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 inputn";
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()
{
}
if (input = 1) goto add
我认为您应该检查(输入 == 1),但您将其分配给输入(输入 = 1)。
代码应该是:-
if (input == 1) goto addition;
if (input == 2) goto subtraction;
if (input == 3) goto multiplication;
if (input == 4) goto division;
我做了一些更改, 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;
}
做 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;
}
相关文章:
- 我似乎对if/else的基本语句有问题:/
- 我在范围内未声明的错误类有问题
- 我的C++语言蛮力算法有问题
- 我关于函数"Assert"的C++代码有问题
- 标准对此指向成员函数类型模板参数有何说明?是我的代码有误,还是 MSVS 16.6 有问题?
- C++类中的友元函数有问题?
- 我对数组即 0x6dfe78 有问题
- 我对 std::unique(算法)C++有问题
- 使用 fstream 库并在屏幕上打印的文件有问题?
- 这个模板为什么有问题?如何正确编译
- 我对 MyGraph 属性顶点名称和边权重有问题
- delete[]有问题,如何部分删除内存
- 在C++中使用exit()退出程序有问题吗
- 跳转到if(false)块有问题吗
- 查找有问题的宏
- 使用 rand() 和 srand() 有问题
- C++ 回文程序总是给出 0(假)作为输出问题;我的代码哪里有问题?
- C++序列计算器x_{n+1} = f(x_n),数学函数有问题
- 我的计算器有问题
- 前缀计算器编译有问题