为什么这段代码不会读取数据文件? [英] Why won't this code read the data file?

查看:66
本文介绍了为什么这段代码不会读取数据文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个任务,我必须使用数组和输入文件中的输出数据。



它用于运行,但我做了更改,现在它将运行,但它不会从数据文件中读取。我不确定我做了什么改变,所以我迷失了解决它。有人告诉我重新安排计算,税收和毛钱,这是我所做的改变。但现在,即使我回到以前的状态,它也无法正常运行。



我正在使用2015年的视觉工作室





So I have an assignment where I have to use arrays and output data from an input file.

It use to run, but I made changes and now it will run, but it won't read from the data file. I'm not sure what changes I made, so I am lost as to fix it. Someone told me to rearrange the calculations and taxes and grosspays, which is the change I made. But now, even if I go back to what I had before, it will not run properly.

I am using visual studio 2015


#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int main() {

	ifstream fin("employee.txt");
	char SMH; //Single, Married, or Head of Household
	string firstname[10], lastname[15];
	int employeeid[100], i = 0;
	int hoursworked[100], overtimehours[100];
	double hourlyrate[100], regularpay[100];
	double overtimepay[100], grosspay[100], taxrate, taxamount, netpay;
	int counter = 0;

	cout << "                                      EBRAHIMI PAYROLL INSTITUTE" << endl;
	cout << "                                              106 Easy Ways" << endl;
	cout << "                                       PLEASENTVILLE, N.Y. 11068" << endl;

	while (fin >> employeeid[i] >> firstname[10] >> lastname[15] >> hoursworked[i] >> hourlyrate[i] >> SMH) {
		counter = counter + 1;
		for (i = 0; i < counter; i++) {
			if (hoursworked[i] > 40) {
				overtimehours[i] = hoursworked[i] - 40;
				overtimepay[i] = overtimehours[i] * hourlyrate[i] * 1.5;
				regularpay[i] = 40 * hourlyrate[i];
			}//if 
			else {
				overtimehours[i] = 0;
				overtimepay[i] = 0;
				regularpay[i] = hoursworked[i] * hourlyrate[i];
			}//else
		}//for
		for (i = 0, i < counter; i++;) {
			grosspay[i] = hourlyrate[i] + overtimepay[i];
		}//end grosspay for overtimepay loop

		for (i = 0; i < counter; i++) {
			if (grosspay[i] > 1000)   taxrate = 0.30;
			else if (grosspay[i] > 800)   taxrate = 0.20;
			else if (grosspay[i] > 500)   taxrate = 0.10;
			else taxrate = 0.0;
		}//for end of grosspay and set taxrate loop

		switch (SMH) {

		case'S': taxrate += 0.05;
			break;
		case'M': taxrate += 0.10;
			break;
		case'H': taxrate -= 0.05;
			break;
		}//switch

		for (i = 0; i < counter; i++) {
			taxamount = grosspay[i] * taxrate;
		}// end of taxamount loop
		for (i = 0; i < counter; i++) {
			netpay = taxamount - grosspay[i];
		}//end of netpay loop


		cout << setw(14) << "Employee Id" << setw(16) << "First Name" << setw(15)
			<< "Last Name" << setw(6) << "Stat" << setw(6) << "HW" << setw(6) << "HR" << setw(8) << "Gross" << setw(6)
			<< "Tax" << setw(9) << "RegP" << setw(6) << "OTP" << setw(9) << "Netpay" << endl << endl;

		for (i = 0; i < counter; i++) {
			cout << setw(14) << employeeid[i] << setw(16) << firstname[10] << setw(15) << lastname[15] << setw(6) << SMH
				<< setw(6) << hoursworked[i] << setw(6) << hourlyrate[i] << setw(8) << grosspay[i] << setw(6)
				<< taxrate << setw(9) << regularpay[i] << setw(6) << overtimepay[i] << setw(9) << netpay << endl;
			i = i + 1;
		}//for
	}//WHILE
	fin.close();
	system("pause");
	return 0;
}//MAIN







如果我的问题不能很好地传达上下文,这是作业:



DR 。 EBRAHIMI's PAYROLL INSTITUTE






This is the assignment in case my question doesn't deliver the context well:

DR. EBRAHIMI'S PAYROLL INSTITUTE

FIRST NAME	LAST NAME	STAT	SSN HW HR OTH OTP REGP GROSS TAX NET


 John	Smith	M	 113 50 20 10 300 800 1100 385 715

 Jane	Dow 	M	 223 40 15 5 112.5 675 787.5 275 512.5





(以上应该是一种表格格式。它没有很好地复制)



B)通过打破利用数组程序分成单独的单位。每个单元应该有一个单独的循环。 (不要使用功能。)



将所有数据读入数组

计算所有超时费用

计算全部the grosspays

计算所有的税收

计算所有的netpays



显示所有阵列

C)包括单独的函数来读取数据,计算所有员工的总工资,税率,净工资和加班工资,并显示。



(The above is supposed to be a table format. It didn't copy well)

B) Take advantage of arrays by breaking programs into separate units. Each unit should have a separate loop. (Do not use functions.)

Read all data into arrays
Compute all the overtimepays
Compute all the grosspays
Compute all the taxrates
Compute all the netpays

Display all the arrays
C) Include separate functions to read in the data, compute the gross pay, tax rate, net pay, and overtime pay for all employees, and display.

推荐答案

首先,无法正常工作不是一个有用的错误描述。如果你甚至没有告诉我们什么是错的,你有什么期望我们应该如何帮助?



其次,你应该总是检查你的流的状态正在阅读,最好是在你阅读之前。 E. g。调用 is_open() good()(或 bad())和 eof()在程序中的适当位置,以确保流处于您期望的状态。这样你可以确定什么时候南下。请参阅 http://www.cplusplus.com/doc/tutorial/files/ [ ^ ]



第三,您的输入流读取的内容完全取决于输入文件的完全内容和格式。如果你混淆了值的顺序或从列标题开始而不是第一行数据,这当然不会按预期工作!因此,不是直接将值流式传输到局部变量,最好只需使用 getline()读取每一行,检查它是否包含任何数据,然后使用 std :: strstream 将行转换为值。
First of all, "doesn't work properly" is not a useful error description. How are we supposed to help if you don't even tell us what's wrong, and what you expect?

Second, you should always check the state of a stream you are reading from, preferably before you read from it. E. g. call is_open(), good() (or bad()), and eof() at the appropriate points in your program to ensure the stream is in the state you expect. That way you can be sure when things go south. See http://www.cplusplus.com/doc/tutorial/files/[^]

Third, what your input stream reads depends entirely on the exact content and format of the input file. If you mix up the order of values or start at the column headings rather tahn the first line of data, this of course won't work as expected! Therefore, rather than directly streaming values into your local variables it may be better to simply read each line with getline(), check whether it contains any data, and then use std::strstream to convert the line into values.


我的解决方案是在调试器模式下运行程序并观察变量。 />
您应该很快看到您的程序正在做什么。



从我看到的代码中,问题肯定与您的数据文件有关正在阅读,以及你阅读它的方式。因为你是唯一一个拥有该文件的人,所以我们无法检查这个。

如果你的阅读表彰在时为<$ code>留下0,它会中止程序。
My solution would be to run the program in debugger mode and watch variables.
You should see very quickly what your program is really doing.

From the code I see, the problem is certainly related to the data file you are reading, and the way you are reading it. Since you are the only one to have that file, we can't check this.
If your reading commends leave a 0 to the while, it will abort the program.


这篇关于为什么这段代码不会读取数据文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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