使用try-catch C ++进行文件处理 [英] File processing using try-catch C++

查看:214
本文介绍了使用try-catch C ++进行文件处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个有关文件处理的项目.用户输入ID,小时数和工资率.输出将是ID,工时,工资率和总薪水.我把那些部分做好了.我确实需要尝试捕获方面的帮助,在这种情况下,用户输入了非数字,项目拒绝了,并要求用户再次输入. 这是我到目前为止所得到的:

I'm working on a project about file processing. The users input ID, hours, and payrate. The output will be ID, hours, payrate and grosspay. I got those parts done. I really need help on try and catch, where users input a non-numeric, the project rejects and ask users to input again. Here what I got so far:

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <iomanip>
#include "File.h"
#include <exception>
using namespace std;

void File::Create()
{
    ofstream outClientFile("payroll.txt", ios::out);

    if (!outClientFile)
    {
        cerr << "File could not be opened" << endl;
        exit(EXIT_FAILURE);
    }

    cout << "Enter employee ID, hours and payrate" << endl
        << "Enter end-of-file to end input.\n? ";

    while (cin >> id >> hours >> payrate)
    {
        try
        {
            outClientFile << id << ' ' << hours << ' ' << payrate << endl;
            cout << "? ";
        }
        catch (exception elementException)
        {
            cerr << ("Invalid input. Try again") << endl;
        }
    }
}
void outputLine(int, float, float, float);
void File::Read()
{
    ifstream inClientFile("payroll.txt", ios::in);

    if (!inClientFile)
    {
        cerr << "File could not be opened" << endl;
        exit(EXIT_FAILURE);
    }

    cout << left << setw(15) << "Employee ID" << setw(15) << "Hours" << setw(15) << "Payrate" << setw(15) << "Grosspay" << endl << fixed << showpoint;

    while (inClientFile >> id >> hours >> payrate)

        outputLine(id, hours, payrate, grosspay = hours * payrate);
}
void outputLine(int id, float hours, float payrate, float grosspay)
{
    cout << left << setw(7) << id << setprecision(2) << setw(8) << " , " << setw(8) << hours << setprecision(2) << setw(7) 
        << " , " << "$"  << setw(7) << payrate << setprecision(2) << setw(7) << " , " << "$"  << grosspay << right << endl;
}

测试文件

#include "File.h"

int main()
{
    File myFile;
    myFile.Create();
    myFile.Read();
}

推荐答案

除非在例外"情况下,程序无法轻易恢复(例如错误的内存分配,打开文件错误等),否则不应使用异常.可以使用while循环更自然地完成输入验证,例如:

You should not use exceptions unless in "exceptional" cases, where the program cannot easily recover (like a bad memory allocation, error opening a file etc). Validating input can be done much more naturally with a while loop, like so:

while( ! (std::cin >> id >> hours >> payrate) ) // repeat until we read correctly
{
    std::cout << "Invalid input, try again..." << std::endl;
    std::cin.clear(); // clear the error flags
    // ignore the rest of the stream, must #include <limits>
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); 
}
// now we can write the result to the file, input was validated
outClientFile << id << ' ' << hours << ' ' << payrate << endl;

如果cin读取正确,则它将转换为bool true,并且不会执行循环.如果不是(即某些非数字输入),则cin >> ...的最终结果将是一个转换为false的流.在后一种情况下,您需要清除错误标志(std::cin.clear()部分),然后擦除流中剩余的其余字符(std::cin.ignore()部分)并重复.

If cin reads correctly, then it will convert to bool true and the loop will not be executed. If not (i.e. some non-numeric input), then the end result of cin >> ... will be a stream which converts to false. In this latter case, you need to clear the error flags (std::cin.clear() part), then erase the rest of the characters left in the stream (std::cin.ignore() part) and repeat.

这篇关于使用try-catch C ++进行文件处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆