我是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();
最后它也总是结束程序。
如果您发现错误,请告诉我。
-Danish Humair
完整代码:
#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();
}
答案 0 :(得分: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专业版上编译
#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()
{
}
答案 1 :(得分:3)
if(input = 1)goto addition
我认为你应该检查(输入== 1),但是你将它分配给输入(输入= 1)。
代码应该是: -
if (input == 1) goto addition;
if (input == 2) goto subtraction;
if (input == 3) goto multiplication;
if (input == 4) goto division;
答案 2 :(得分: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;
}
答案 3 :(得分: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;
}