如何防止字符串输入双变量并扰乱程序



只想知道如何防止用户在变量是双精度而不是字符串时输入错误的输入,比如输入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;
}

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

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

它将首先尝试从input_file读取double-如果失败,则会设置故障位,并且我们在操作后检查此标志-以便在出现问题时将变量分配给0.0(并最终为下一次操作重置故障位)。

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

#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;
}

相关内容

  • 没有找到相关文章

最新更新