尝试使用while循环从文件中读取数据.还必须检查以确保第一个和唯一的值不是-1

Trying to read data into an array from a file using a while loop. Also have to check to make sure the first and only value is not -1

本文关键字:第一个 确保 检查 唯一 while 循环 文件 数据 读取      更新时间:2023-10-16

我打算让它显示从文件中读取的值,但是在我输入文件路径后才结束。

输入文件包含:

5658845 40 8

4520125 25.25 9.25

7895122 30 10.50

8777541 10 12

8451277 50 10.20

1302850 35.20 7.50

-1

程序运行时的屏幕截图。是的,这是正确的文件路径

#include <string>
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
const int SIZE = 20;
int read(long int[], double[], double[]);

int main()
{
    int checkRead;              //Define int variable to check if read was successfully executed
    long int empId[SIZE];       //Define employee id array
    double hours[SIZE];         //Define hours worked array
    double payRate[SIZE];       //Define pay rate array
    double wages[SIZE];         //Define wages array
    ofstream outfile;           //Define output file

    cout << setw(30) << "PAYROLL PROCESSING" << endl << endl;      //Program header
    checkRead = read(empId, hours, payRate);
    if (checkRead = -1)
        return 0;
    for (int c = 0; c < SIZE; c++)
        cout << empId[c] << " " << hours[c] << " " << payRate[c] << endl;
    return 0;
}
int read(long int id[], double hrs[], double rate[])    //Function to read data from file
{
    int count = 0;
    long int tempId = 0;
    ifstream infile;                                    //Define input file
    string fileIn;                                      //Define file name
    cout << "Enter the name of the input file: ";       //Prompt for input file name
    cin >> fileIn;                                      //Read input file name
    infile.open(fileIn);                                //Open input file
    if (infile.fail())      //check to see if input file failed to open
    {
        cout << "Error. Invalid file path" << endl;     //if input file failed to open print error message
        return -1;      //return -1 to read
    }
    while (tempId >= 0 && count < SIZE)
    {
        infile >> id[count] >> hrs[count] >> rate[count];
        tempId = id[count];
        if (count = 0 && tempId < 0)
        {
            cout << "Error. This file contains no data" << endl;
            return -1;
        }
        count++;
    }
    infile.close();
    return 0;
}

它应该是 if (checkRead == -1),而不是 if (checkRead = -1)if (checkRead = -1)在您的情况下始终是正确的...

checkRead = read(empId, hours, payRate);
if (checkRead = -1) // should be if (checkRead == -1)
    return 0;
for (int c = 0; c < SIZE; c++)
    cout << empId[c] << " " << hours[c] << " " << payRate[c] << endl;

if (count = 0 && tempId < 0)应该是 if (count == 0 && tempId < 0)

while (tempId >= 0 && count < SIZE)
{
    infile >> id[count] >> hrs[count] >> rate[count];
    tempId = id[count];
    if (count = 0 && tempId < 0) // should be if (count == 0 && tempId < 0)
    {
        cout << "Error. This file contains no data" << endl;
        return -1;
    }
    count++;
}
相关文章: