尝试使用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
我打算让它显示从文件中读取的值,但是在我输入文件路径后才结束。
输入文件包含:
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++;
}
相关文章:
- 获取向量C++中第一个值和最后一个值的和
- 有没有一种"cleaner"的方法可以在指向基的指针向量中找到派生类的第一个实例?
- 当我在第一个循环中使用"auto"时,它工作正常,但是使用"int"它会给出错误,为什么?
- 我有两个类需要在同一 cpp 文件中相互引用,但第一个类无法识别第二个类类型的对象
- 为什么有时我输入一个整数,程序将第一个输入的数字打印成十进制数?
- 如何在创建自定义迭代器时获得 std::p air 的第一个和第二个?
- 如何实现 Front() 方法以返回模板化双向链表C++的第一个元素?
- WinAPI 在单击第一个对话框上的按钮控件并销毁第一个对话框后创建第二个对话框
- 查找数组中第一个最小值和最后一个最大值元素之间的算术平均值
- "main"函数堆栈中的对象在第一个任务运行时被覆盖 (FreeRTOS)
- 对的排序向量 (std::vector<pair<int, int>>) 按对的第一个元素搜索并更新第二个元素值
- C++部分概念 id:显式模板规范顺序/第一个参数的特殊状态的原因是什么?
- 比较 2 个向量并从第二个向量中删除在第一个 - c++ 中找不到的元素
- 为什么当我尝试搜索双链表中第一个数据条目之外的数据时,程序崩溃了?
- 使第二个类的构造函数成为第一个类中的友元函数
- 为什么第二个代码给出了预期的结果,而第一个代码却没有?
- 使用哈希图打印第一个重复元素;
- 为什么第一个代码块产生垃圾值,而第二个代码块将类成员的值相加?
- 尝试使用while循环从文件中读取数据.还必须检查以确保第一个和唯一的值不是-1
- 我们如何确保传递的 2 个参数被视为第一个和第三个,而第二个 AD 第四个被视为默认值