我是一个非常新手的程序员,所以我对编写代码来保护应用程序并不是很了解。基本上,我创建了一个basicMath.h文件并创建了一个do while循环来制作一个非常基本的控制台计算器(只有两个浮点数通过函数)。我使用一系列if和else if语句来确定用户想要做什么。 (1.add,2.subtract,3.multiply,4.divide)我使用了其他{cout<< “无效输入”<< endl;}以防止任何其他值,但后来我试着写一个字母,程序进入无限循环。无论如何要防止意外击中角色而不是数字的用户?
`#include <iostream>
#include "basicMath.h"
using namespace std;
char tryAgain = 'y';
float numOne = 0, numTwo = 0;
int options = 0;
int main()
{
cout << "welcome to my calculator program." << endl;
cout << "This will be a basic calculator." << endl;
do{
cout << "What would you like to do?" << endl;
cout << "1. Addition." << endl;
cout << "2. Subtraction." << endl;
cout << "3. Multiplication" << endl;
cout << "4. Division." << endl;
cin >> options;
if (options == 1){
cout << "Enter your first number." << endl;
cin >> numOne;
cout << "Enter your second number." << endl;
cin >> numTwo;
cout << numOne << " + " << numTwo << " = " << add(numOne, numTwo) << endl;
}
else if (options == 2){
cout << "Enter your first number." << endl;
cin >> numOne;
cout << "Enter your second number." << endl;
cin >> numTwo;
cout << numOne << " - " << numTwo << " = " << subtract(numOne, numTwo) << endl;
}
else if (options == 3){
cout << "Enter your first number." << endl;
cin >> numOne;
cout << "Enter your second number." << endl;
cin >> numTwo;
cout << numOne << " * " << numTwo << " = " << multiply(numOne, numTwo) << endl;
}
else if (options == 4){
cout << "Enter your first number." << endl;
cin >> numOne;
cout << "Enter your second number." << endl;
cin >> numTwo;
cout << numOne << " / " << numTwo << " = " << divide(numOne, numTwo) << endl;
}
else {
cout << "Error, invalid option input." << endl;
}
cout << "Would you like to use this calculator again? (y/n)" << endl;
cin >> tryAgain;
}while (tryAgain == 'y');
cout << "Thank you for using my basic calculator!" << endl;
return 0;
}
`
答案 0 :(得分:4)
一种方法是使用异常处理,但作为一个新手,你可能远没有学习它。
而是使用在错误或意外输入后返回1的cin.fail()
。请注意,您需要清除&#34;坏&#34;状态使用cin.clear()
。
一种简单的方法是实现一个函数:
int GetNumber ()
{
int n;
cin >> n;
while (cin.fail())
{
cin.clear();
cin.ignore();
cout << "Not a valid number. Please reenter: ";
cin >> n;
}
return n;
}
现在,在您的主要功能中,无论您在哪里进行输入,只需调用GetNumber
并将返回的值存储在变量中。例如,代替cin >> numOne;
,执行numOne = GetNumber();
答案 1 :(得分:0)
当您输入cin
时,它需要特定的类型,例如整数。如果它收到了它不期望的内容,例如字母,则会设置bad标记。
答案 2 :(得分:0)
首先读取整行文本,然后将文本行转换为数字并处理字符串到数字转换中的任何错误。
使用std::cin
函数从std::getline
读取整行文本(不要与流的成员函数混淆):
std::string line;
std::getline(std::cin, line);
if (!std::cin) {
// some catastrophic failure
}
使用std::istringstream
(pre-C ++ 11)或std::stoi
(C ++ 11)完成字符串到数字的转换。这是pre-C ++ 11版本:
std::istringstream is(line);
int number = 0;
is >> number;
if (!is) {
// line is not a number, e.g. "abc" or "abc123", or the number is too big
// to fit in an int, e.g. "11111111111111111111111111111111111"
} else if (!is.eof()) {
// line is a number, but ends with a non-number, e.g. "123abc",
// whether that's an error depends on your requirements
} else {
// number is OK
}
这里是C ++ 11版本:
try {
std::cout << std::stoi(line) << "\n";
} catch (std::exception const &exc) {
// line is not a number, e.g. "abc" or "abc123", or the number is too big
// to fit in an int, e.g. "11111111111111111111111111111111111"
std::cout << exc.what() << "\n";
}