增强程序-完全失败 [英] enhancing a program - complete failure

查看:60
本文介绍了增强程序-完全失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好. 我在尝试弄清楚如何增强我的程序时遇到了麻烦.

good day everyone. im having some trouble trying to figure out how to enhance my program.

这是问题:

编写程序以计算课程的数字成绩.课程记录在一个文件中,该文件将用作输入文件.输入文件的格式完全相同:每行包含一个学生的姓氏,然后是一个空格,然后是该学生的名字,然后是一个空格,然后是十行测验分数.测验分数是整数,并且用一个空格分隔.您的程序将从该文件中获取输入,并将其输出发送到第二个文件.输出文件中的数据将与输入文件中的数据相同,除了每行末尾会有一个额外的数字(double类型).该数字将是该学生的十个测验分数的平均值.如果这是作为课程分配完成的,请从您的教师那里获取文件名.使用至少一个具有文件流的函数作为其全部或某些参数.

Write a program to compute numeric grades for a course. The course records are in a file that will serve as the input file. The input file is in exactly the following format: Each line contains a student’s last name, then one space, then the student’s first name, then one space, then ten quiz scores all on one line. The quiz scores are whole numbers and are separated by one space. Your program will take its input from this file and send its output to a second file. The data in the output file will be the same as the data in the input file except that there will be one additional number (of type double) at the end of each line. This number will be the average of the student’s ten quiz scores. If this is being done as a class assignment, obtain the file names from your instructor. Use at least one function that has file streams as all or some of its arguments.

我成功地完成了第一部分.下面是代码:

i managed to do the first part successfully. below is the code:

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
using namespace std;

int main()
{
    fstream infile("grades.txt",ios::in);
    if(!infile){cerr<<"file could not be found!";exit(1);}

    fstream outfile("average.txt",ios::out);
    if(!outfile){cerr<<"file could not be created!";exit(1);}


    char fname[20];
    char lname[20];
    int grades[10];
    char c;
    int x;
    cout<<"how many students?";
    cin>>x;

        for(int k=0;k<x;k++)
        {
            infile>>fname;
            infile>>lname;
            for(int i=0;i<10;i++)
            infile>>grades[i];
            outfile<<fname<<" "<<lname<<" ";
            double sum=0;
            for(int j=0;j<10;j++) 
            {

                outfile<<grades[j]<<" ";
                sum+=grades[j];

            }

            double avg=0;
            avg=sum/10;
            outfile<<avg<<endl;
        }


    system("pause");
    return 0;
}

我无法执行第二部分的(a)部分.我试图将grades [10]数组初始化为零,但是我没有得到任何正确的输出.有什么帮助吗?谢谢.

im not able to do part (a) of the second part. i tried initializing the grades[10] array to zeros, but im not getting any correct output. any help? thank you.

在以下所有方面增强为您编写的程序(问题10) 方法.

Enhance the program you wrote for (Problem 10) in all the following ways.

a-每行测验分数列表将包含少于10的测验 分数. (如果测验分数少于十,则表示 学生错过了一个或多个测验.)平均分数为 仍然是测验分数的总和除以10.这等于 给学生任何未参加的测验的分数为0.

a-The list of quiz scores on each line will contain ten of fewer quiz scores. (If there are fewer than ten quiz scores, that means that the student missed one or more quizzes.) The average score is still the sum of the quiz scores divided by 10. This amounts to giving the student a 0 for any missed quiz.

b-输出文件的开头将包含一行(或多行) 解释输出的文件.使用格式说明来 使布局整洁且易于阅读. c-放置所需的 在输出文件中输出,您的程序将关闭所有文件,然后 将输出"文件的内容复制到输入"文件,以便 最终结果是更改输入文件的内容.
使用至少两个具有文件流作为全部或部分功能的函数 他们的论点.如果这是作为课程分配完成的, 从您的指令中获取文件名.

b-The output file will contain a line (or lines) at the beginning of the file explaining the output. Use formatting instructions to make the layout neat and easy to read. c- After placing the desired output in an output file, your program will close all files and then copy the contents of the "output" file to the "input" file so that the net effect is to change the contents of the input file.
Use at least two functions that have file streams as all or some of their arguments. If this is being done as a class assignment, obtain the file names from your instruction.


这是我的代码现在的样子


here's how my code looks now

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
using namespace std;

int main()
{
    fstream infile("grades.txt",ios::in);
    if(!infile){cerr<<"file could not be found!";exit(1);}

    fstream outfile("average.txt",ios::out);
    if(!outfile){cerr<<"file could not be created!";exit(1);}


    char fname[20];
    char lname[20];
    int grades;
    int sum=0;
    int linecount=0;
    char c;

    while(!infile.eof())
    {
            infile>>lname;
            infile>>fname;
            outfile<<lname<<" "<<fname<<" ";
            for(int i=0;i<10;i++){if(infile>>grades)outfile<<grades<<" ";else {outfile<<"0 ";break;} sum+=grades;}
            outfile<<double(sum/10.0);
    }
            system("pause");
            return 0;
}

但是我在运行程序时得到的只是空白.我无法修复循环以从文件的所有行读取的内容.

but im getting just a black space when i run the program. im not able to fix the loop to read from all the lines of the file.

推荐答案

从第一部分开始:您的代码不能完全解决给定的问题.给出的问题并不是说您输入了多少学生,但是您应该处理文件中的所有学生,无论他们有多少.另外,您忽略了这一部分:至少使用一个具有文件流作为其全部或某些参数的函数."

As of the first part: Your code doesn't exactly solve the problem as given. The problem as given doesn't say you enter a number of students, but you should process all students in the file, no matter how many they are. Also, you neglected the part: "Use at least one function that has file streams as all or some of its arguments."

无论如何,我建议您逐行读取文件,然后使用ostringstream分别处理每一行.这样一来,检测到不再有任何成绩就会与第1部分中检测到没有更多的学生一样.

Anyways, I'd advise you to read the file line by line, and then process each line individually using ostringstream. That way, detecting that no more grades follow works the same way as detecting that no more students follow in part 1.

提示:查看流错误状态,尤其是fail,并在第1部分中使用while循环,在第2部分中使用break.

Hint: Look at the stream error status, especially fail, and use a while loop in part 1, and break in part 2.

这篇关于增强程序-完全失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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