结合之前几个版本的计算器,现在我将计算器进行了又一次升级,当数字输入的为其它字符的时候我们会要求重新输入,具体代码如下:
#include <iostream>
#include <limits> // 包含用于处理输入流的 std::numeric_limits
#include <cstdlib> // 包含用于清屏的 std::system
#include <cmath> // 包含用于平方根的 std::sqrt
using namespace std;
// 函数声明
void sssm();
void show_results();
// 全局变量
float result_arr[2] = { 0 }; // 存放两次结果的数组
int arr_index = 0; // 当前存放结果的数组索引
int main() {
float num1 = 0, num2 = 0, final;
char fh = 0;
char select = 0;
cout << "欢迎使用简易计算器" << endl;
cout << "-----------------" << endl;
cout << "| 1、进入 |" << endl;
cout << "-----------------" << endl;
cout << "| 2、说明 |" << endl;
cout << "-----------------" << endl;
cin >> select;
if (select == '1') {
system("cls");
cout << "请输入" << endl;
sssm();
goto Z;
}
else
sssm();
Z:cin >> num1;
while (true) {
if (cin.fail()) {
cin.clear(); // 清除错误标志
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // 忽略输入缓冲区中的内容
cout << "输入无效,请重新输入一个数字:" << endl;