c++ - 如何防止一个字符串进入一个double变量而把程序搞乱

标签 c++ validation user-input

只是想知道当变量是 double 而不是字符串时如何防止用户输入错误的输入,例如输入 cookie。例如,在我的代码中,我使用了一个文本文件,该文件将由我的程序读取,该文件将从 Data.txt 中获取所有信息并将其放入 RecWidth 等变量中。有没有一种方法可以使像 cookie 这样的字符串尝试进入 RecWidth 只会导致 RecWidth 为 0。

#include <cstring>
#include <fstream>
#include <iostream>
#include <cmath>


using namespace std;

int main() {
std::ifstream input_file("Data.txt", ios::in);
ofstream out_data("Output.txt", ios::out); 

int age = 0;
int totalAge = 0;
double RecLength, RecWidth, RecArea, RecPerimiter, CirRadius, CirCricumfrence = 0;
double totalWidth = 0;
double totalLength = 0;
double totalPerimiter = 0;
double totalSavings = 0;
double totalArea = 0;
double totalRadius = 0;
double totalCArea = 0;
double totalCircumfrence = 0;
double CirArea, Savings, NumberOfPeople = 0;
string NameFirst, NameLast;

while (!input_file.eof())
{

input_file >> RecLength;
input_file >> RecWidth;
totalLength = totalLength + RecLength;
totalWidth = totalWidth + RecWidth;
RecArea = RecLength * RecWidth;
totalArea = totalArea + RecArea;
RecPerimiter = (RecLength * 2) + (RecWidth * 2);
totalPerimiter = totalPerimiter + RecPerimiter;

input_file >> CirRadius;
totalRadius = totalRadius + CirRadius;
CirCricumfrence =  2 * M_PI * CirRadius;
totalCircumfrence = totalCircumfrence + CirCricumfrence;
CirArea = M_PI * pow(CirRadius, 2.0);
totalCArea = totalCArea + CirArea;

input_file >> NameFirst; 
input_file >> NameLast;
NumberOfPeople++;
input_file>> age;
totalAge = totalAge + age; 
input_file>> Savings;
totalSavings = totalSavings + Savings;

} 

out_data << "Rectangle:" << endl << "The total lengths= " << totalLength << ", width= " << totalWidth << ", area= " << totalArea << ", perimeter= " << totalPerimiter << endl << endl;
out_data << "Circle:" << endl << "The total Radius= " << totalRadius << ", area= " << CirArea << ", circumfrence= " << CirCricumfrence << endl << endl;
out_data << "Person:" << endl << "Total number of persons= " << NumberOfPeople << endl << "Total age= " << totalAge << endl << "The total saving= " << totalSavings << endl << endl;

 return 0;
}

最佳答案

有一种方法可以检查 >> operator是否成功 - 如果不是,您应该将有问题的变量分配给 0。考虑这段代码:

input_file >> RecLength;
if (input_file.fail()) {
  RecLength = 0.0;
  input_file.clear();
}

它将首先尝试从 input_file 中读取一个 double - 如果它失败了 failbit将被设置,我们在操作后检查这个标志 - 将变量分配给 0.0 以防出现问题(并最终为下一个操作重置 failbit)。

以下代码为 failbit 添加了这些必要的检查,并将防止出现意外值:

#include <string>
#include <fstream>
#include <iostream>
#include <cmath>
#define M_PI 3.14159265358979323846
using namespace std;

int main() {
  std::ifstream input_file("Data.txt", ios::in);
  ofstream out_data("Output.txt", ios::out);
  int age = 0, totalAge = 0;
  double RecLength, RecWidth, RecArea, RecPerimiter, CirRadius, CirCricumfrence = 0;
  double totalWidth = 0, totalLength = 0, totalPerimiter = 0, totalSavings = 0;
  double totalArea = 0, totalRadius = 0, totalCArea = 0, totalCircumfrence = 0, CirArea, Savings, NumberOfPeople = 0;
  string NameFirst, NameLast;
  while (!input_file.eof())
  {
    input_file >> RecLength;
    if (input_file.fail()) {
      RecLength = 0.0;
      input_file.clear();
    }

    input_file >> RecWidth;
    if (input_file.fail()) {
      RecWidth = 0.0;
      input_file.clear();
    }
    totalLength = totalLength + RecLength;
    totalWidth = totalWidth + RecWidth;
    RecArea = RecLength * RecWidth;
    totalArea = totalArea + RecArea;
    RecPerimiter = (RecLength * 2) + (RecWidth * 2);
    totalPerimiter = totalPerimiter + RecPerimiter;

    input_file >> CirRadius;
    if (input_file.fail()) {
      CirRadius = 0.0;
      input_file.clear();
    }
    totalRadius = totalRadius + CirRadius;
    CirCricumfrence = 2 * M_PI * CirRadius;
    totalCircumfrence = totalCircumfrence + CirCricumfrence;
    CirArea = M_PI * pow(CirRadius, 2.0);
    totalCArea = totalCArea + CirArea;

    input_file >> NameFirst;
    input_file >> NameLast;
    NumberOfPeople++;

    input_file >> age;
    if (input_file.fail()) {
      age = 0;
      input_file.clear();
    }
    totalAge = totalAge + age;

    input_file >> Savings;
    if (input_file.fail()) {
      Savings = 0.0;
      input_file.clear();
    }
    totalSavings = totalSavings + Savings;
  }
  out_data << "Rectangle:" << endl << "The total lengths= " << totalLength << ", width= " << totalWidth << ", area= " << totalArea << ", perimeter= " << totalPerimiter << endl << endl;
  out_data << "Circle:" << endl << "The total Radius= " << totalRadius << ", area= " << CirArea << ", circumfrence= " << CirCricumfrence << endl << endl;
  out_data << "Person:" << endl << "Total number of persons= " << NumberOfPeople << endl << "Total age= " << totalAge << endl << "The total saving= " << totalSavings << endl << endl;
  return 0;
}

关于c++ - 如何防止一个字符串进入一个double变量而把程序搞乱,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33427566/

相关文章:

c++ - 如何在 C++ 中使用 tagDEC(DECIMAL 类型)?

c++ - 在C++中快速将基对象的所有成员分配给派生对象

android - 如何将日期选择器文本框日期限制为 future 日期。它应该只允许以前的日期

c++ - 如何用用户输入变量制作二维数组?

php - 在php中提交表单后,如何在文本框中输入数据?

c++ - Thrift - 每个套接字都有不同的 Handler 实例

c++ - 如何将 std::sort 与对和引用一起使用

javascript - JS HTML5 验证 "display:none"所需的输入元素

php - 我的 AJAX 查询未正确验证

python - python3上的简单书店程序