Hi, I'm having trouble on transfering "input.txt" over to "output.txt" what command shall I use with C++ language? Anyways below is the question I needed to answer on monday, anyway help will be very very appreciated.

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 on espace, 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.

nm I know how to transfer files from input to output now.. but i'm still having problem output the average of every line from input file.. how is it being done? my codes are below

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

void copy(ifstream& in_stream, ofstream& out_stream);

int main( )
{
    ifstream fin;
    ofstream fout;

    cout << "Begin editing files.\n";

    fin.open("input.txt");
    if (fin.fail( ))
    {
        cout << "Input file opening failed.\n";
        exit(1);
    }

    fout.open("output.txt");
    if (fout.fail( ))
    {
        cout << "Output file opening failed.\n";
        exit(1);
    }

    copy(fin, fout);


    fin.close( );
    fout.close( );

    cout << "End of editing files.\n";
    return 0;
}

void copy(ifstream& in_stream, ofstream& out_stream)
{
    char next;

    in_stream.get(next);
    while (! in_stream.eof( ))
    {

        out_stream << next;

        in_stream.get(next);
    }
}
Member Avatar for Siersan

What you want is a variation of this.

#include <iostream>
#include <string>

using namespace std;

int main()
{
  string last, first;
  int sum = 0, grade;

  cin>> last >> first;
  for (int i = 0; i < 10; i++) {
    cin>> grade;
    sum += grade;
  }

  cout<<"The average is "<< sum / 10 <<endl;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.