2015-10-30 22 views
0

只是想知道如何防止用戶輸入錯誤的輸入,如輸入cookie時,該變量是一個雙精度而不是字符串。例如在我的代碼中,我正在使用一個文本文件,該文件將被我的程序讀取,該文件將從Data.txt中獲取所有信息並將其放入像RecWidth這樣的變量中。有來自試圖進入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

你應該張貼在威脅建模部這個問題。 – Nandu

+0

您應該在威脅建模部分發布此問題。 – Nandu

+1

我認爲你可以編輯你的問題,並做得更簡潔。基本上,據我所知,你想驗證輸入數據類型與聲明的類型匹配 – lrleon

回答

1

有一種方法來檢查,如果>> operator成功與否 - 的情況下:這不是什麼您應該將相關變量分配給0。考慮以下代碼:

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

它首先會試圖讀取一個doubleinput_file - 如果它失敗的failbit將置我們在手術後檢查該標誌 - 的變量分配給0.0在一些情況下,出錯了(最後重置下一個操作的失敗位)。

下面的代碼將這些必要的檢查的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; 
} 
相關問題