简体   繁体   English

如何验证输入文件中的.txt数据? C ++

[英]How to validate .txt data from an input file? c++

The goal is to get an Item Name - string - (.txt) and price - double - (.txt) from a .txt. 目标是从.txt中获取商品名称-字符串-(.txt)和价格-双-(.txt)。 I am required to check this input and make sure it is valid, and if not, return 1, and exit the program. 我需要检查此输入并确保其有效,如果无效,请返回1并退出程序。 I was able to check to make sure the input file was opened. 我能够检查以确保输入文件已打开。 But I am drawing a blank on how to go about checking the other data. 但是,我在如何检查其他数据方面处于空白。 The validation was explained to be done in groups of two (item/price) at a time. 验证被解释为一次以两个(项目/价格)为一组进行。

For the string values I just need to make sure the line isn't blank. 对于字符串值,我只需要确保该行不是空白即可。 For the double values I need to make sure they are numeric. 对于double值,我需要确保它们是数字。

int getData(int& listSize, menuItemType*& menuList, int*& orderList)
{
    //-----Declare inFile
    ifstream inFile;
    string   price, size;

    //-----Open inFile
    inFile.open("Ch9_Ex5Data.txt");

    //-----Check inFile
    if (inFile.is_open())
    {
        //-----Get Amount of Items, Convert to int 
        getline(inFile, size);
        listSize = stoi(size);      <---This needs to be positive int 

        //-----Set Array Size
        menuList  = new menuItemType[listSize];
        orderList = new int[listSize];

        //-----Get Menu
        for (int x = 0; x < listSize; x++)
        {
            //-----Get menuItem
            getline(inFile, menuList[x].menuItem);//-make sure data recieved

            //-----Get menuPrice convert to double
            getline(inFile, price);
            menuList[x].menuPrice = stod(price);//make sure double < 99

            orderList[x] = 0;
        }                      //teacher explained i should validate in 
                                 groups of two
        return 0;
    }
    else
    {
        return 1;
    }
}

//This is the .txt.
8
Plain Egg
1.45
Bacon and Egg
2.45
Muffin
0.99
French Toast
1.99
Fruit Basket
2.49
Cereal
0.69
Coffee
0.50
Tea
0.75

For example, you can create a function that tells you whether a pair of values ​​is valid (when menu != empty and 0 < price < 99 . This function will be called each time each pair is read. 例如,您可以创建一个函数来告诉您一对值是否有效(当menu != empty0 < price < 99 。每次读取每对值时都会调用此函数。

bool validation(const string& menu, const double& price){ 
    if(menu.empty() || price < 0 || price > 99)
        return 1 ;
    else
        return 0 ;
}

This function returns 0 when the pair is correct and 1 otherwise (as you indicated). 当配对正确时,此函数返回0,否则返回1(如您所指示)。

Now it is only it necessary to invoke the function every time the new pair of values ​​is created. 现在只需要在每次创建新的一对值时调用函数。 At the moment one of these is invalid, it will exit the program. 当其中之一无效时,它将退出程序。

for (int x = 0; x < listSize ; x++)
{
    //-----Get menuItem
    getline(inFile, menuList[x].menuItem);//-make sure data recieved

    //-----Get menuPrice convert to double
    getline(inFile, price);//-make sure data recieved
    menuList[x].menuPrice = atof(price.c_str());//make sure double < 99

    //teacher explained i should validate in groups of two
    if(validation(menuList[x].menuItem, menuList[x].menuPrice))
        return 1 ;

    orderList[x] = 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM