阵列和I / O文件问题[关闭](Trouble with arrays and I/O files [closed])

系统教程 行业动态 更新时间:2024-06-14 16:58:30
阵列和I / O文件问题[关闭](Trouble with arrays and I/O files [closed])

这是我遇到麻烦的作业。 我的作业代码如下。 我不知道为什么我不读取txt文件中的数据。 任何人都可以看看作业和我的代码,并指出我正确的方向?

分配

服务电话公司报告

OK-Service Handlers Company处理来自客户的每日服务电话,电话。 该公司通过电话处理民间问题。 需要对某个月内的电话进行汇总的权力。

收集的数据是每天进入的服务调用,并记录在名为SericeCalls.txt的文件中。 我将把文件放在分配下的Blackboard上。

数据表明所进行的服务呼叫类型以及服务呼叫持续的分钟数。 该公司处理几种不同类型的呼叫,并且每天对于给定日期的给定呼叫将存在若干不同的条目。 输入将是每行两个数字,其中第一个数字是服务呼叫的类型,第二个是呼叫持续的分钟数。 每个输入行是一个服务调用的记录。 提供了25种不同类型的服务,编号为1到25.例如:3 30服务号3,持续30分钟。 21 45服务号码21持续45分钟。 6 28服务号码6持续28分钟。 等等..

该公司可以处理多达25种不同的服务。 输入文件是一个月的数据。 您将计算每种类型的服务句柄的服务呼叫数以及呼叫所花费的分钟数。

报告中应包含以下信息。 该报告应有标题和标题。 每种类型的服务调用的输出, 该月的服务呼叫句柄总数, 在该类型的服务呼叫上花费的总分钟数, 公司处理的服务电话总数, 每种服务类型需要处理的平均分钟数, 本月服务电话的总体平均值。 我还需要知道是否使用了哪些服务调用类型。 还告诉我哪个服务电话花了最多的时间来处理。 标记所有输出并使其成为可读的报告,表格格式。 您必须使用数组,传递数组和使用函数。

#include "stdafx.h" #include<iostream> #include<fstream> using namespace std; const int ROWS= 25; const int COLS = 2; double input; ofstream OutFile; //function prototype void ReadFile(int[ROWS][2]); void printArray(int[ROWS][2]); int main() { int ary[ROWS][2]; //open-creates file to print to OutFile.open ("ServiceCallOutFile.txt"); // Title and Heading OutFile << "\nMy\n"; OutFile << "\nMonthly Service Call Report \n"; OutFile << "Service call report generated for September 2013\n\n"; cout << "\nMy \n"; cout << "\nMonthly Service Call Report \n"; cout << "Service call reprot generated for Oct. \n\n"; // Call Function 1 ReadFile(ary); // Call Function 2 printArray(ary); OutFile<<"\n-----------------------------"<<endl; cout<<"\n-----------------------------"<<endl; //closes .txt file OutFile.close(); cin.get(); cin.get(); return 0; } // 1) Open and ReadFile .txt file for array void ReadFile(int ary[ROWS][2]) { ifstream infile("ServiceCalls.txt"); for(ROWS;ROWS<25;ROWS+1) { cout<<ary[ROWS][COLS]; for (COLS;ROWS<2;COLS+1) { infile>>ary[ROWS][COLS]; } } infile.close(); } // 2) Print out all the values in the array with no more than 10 numbers per output line. void printArray(int ary[ROWS][2]) { OutFile<< "The numbers in the array are: \n"; cout<< "The numbers in the array are: \n"; for(ROWS;ROWS<25;ROWS+1) { cout<<ary[ROWS][COLS]; for (COLS;ROWS<2;COLS+1) { OutFile<<ary[ROWS][COLS]<<" "" "; OutFile<<endl; cout<<ary[ROWS][COLS]<<" "" "; cout<<endl; } } }

Assignment

Service Calls Company Report

The OK-Service Handlers Company handles daily service calls from customers, phone calls. The company handles folk’s problems over the phone. The powers to be need a summary on the calls made in a given month.

The data collected is each day’s service calls that come in and is recorded in a file named SericeCalls.txt. I will put the file out on Blackboard under Assignments.

The data indicates the type of service call made and the number of minutes that service call lasted. The company handles several different kinds of calls and each day there will be several different entries for a given call for a given day. The input will be two numbers per line where the first number is the type of service call and the second is the number of minutes that call lasted. Each input line is a record of one service call. There are 25 different types of service rendered and are numbered 1 to 25. For example: 3 30 service number 3 and lasted 30 min. 21 45 service number 21 lasted 45 min. 6 28 service number 6 lasted 28 min. etc..

The company can handle up to 25 different kinds of services. The input file is one month of data. You are to count the number of service calls for each type of service handle and the number of minutes the call took.

The report should have in it the following information. The report should have a title and headings. Output for each type of service call rendered, the total number of that service call handle for the month, the total number of minutes spent on that type of service call, the total number of service calls handle by the company, the average number of minutes each service type took to handle, the overall average a service calls took for the month. I also need to know if and which service call types were not used. Also tell me which service call took the most time to handle. Label all output and make it a nice readable report, table format. You must use arrays, pass arrays, and use functions.

Code

#include "stdafx.h" #include<iostream> #include<fstream> using namespace std; const int ROWS= 25; const int COLS = 2; double input; ofstream OutFile; //function prototype void ReadFile(int[ROWS][2]); void printArray(int[ROWS][2]); int main() { int ary[ROWS][2]; //open-creates file to print to OutFile.open ("ServiceCallOutFile.txt"); // Title and Heading OutFile << "\nMy\n"; OutFile << "\nMonthly Service Call Report \n"; OutFile << "Service call report generated for September 2013\n\n"; cout << "\nMy \n"; cout << "\nMonthly Service Call Report \n"; cout << "Service call reprot generated for Oct. \n\n"; // Call Function 1 ReadFile(ary); // Call Function 2 printArray(ary); OutFile<<"\n-----------------------------"<<endl; cout<<"\n-----------------------------"<<endl; //closes .txt file OutFile.close(); cin.get(); cin.get(); return 0; } // 1) Open and ReadFile .txt file for array void ReadFile(int ary[ROWS][2]) { ifstream infile("ServiceCalls.txt"); for(ROWS;ROWS<25;ROWS+1) { cout<<ary[ROWS][COLS]; for (COLS;ROWS<2;COLS+1) { infile>>ary[ROWS][COLS]; } } infile.close(); } // 2) Print out all the values in the array with no more than 10 numbers per output line. void printArray(int ary[ROWS][2]) { OutFile<< "The numbers in the array are: \n"; cout<< "The numbers in the array are: \n"; for(ROWS;ROWS<25;ROWS+1) { cout<<ary[ROWS][COLS]; for (COLS;ROWS<2;COLS+1) { OutFile<<ary[ROWS][COLS]<<" "" "; OutFile<<endl; cout<<ary[ROWS][COLS]<<" "" "; cout<<endl; } } }

Input numbers from my .txt file.

17 47 10 43 20 30 4 34 15 22 21 20 3 48 17 38 18 37 12 12 5 5 4 14 8 35 17 29 21 46 2 17

最满意答案

你需要在循环中改变一些东西来实际执行它。 您刚刚使用了边界所需的常量,并且对常量值进行了编码,这是一个坏主意。 for循环的一般形式是

for (initialization; condition; advance)

initialization可能涉及值的定义。 例如,要迭代值0到9,您可以使用循环

int const boundary(10); for (int i(0); i != boundary; ++i) { ... }

此外,您的代码没有检查您的流是否处于良好状态:您应该始终检查是否实际上已从流中成功读取了值。 例如:

if (file >> value) { use(value); } else { std::cout << "ERROR: failed to read a value from the file\n"; }

You need to have something changing in the loop to actually execute it. You just used the constants you need for the boundaries and harded coded the constant values which is a Bad Idea. The general form of a for-loop is

for (initialization; condition; advance)

where the initialization can involve the definition of a value. For example, to iterate over the values 0 to 9, you could use the loop

int const boundary(10); for (int i(0); i != boundary; ++i) { ... }

Also, your code didn't make any check if your stream is in a good state: you should always check that a value was was actually successfully read from a stream. For example:

if (file >> value) { use(value); } else { std::cout << "ERROR: failed to read a value from the file\n"; }

更多推荐

本文发布于:2023-04-15 03:43:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/431dae3db199cb37be1b3b4c6f8d36cb.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:阵列   文件   Trouble   closed   files

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!