Hey guys,
I know this is Mac Forums but I just hope someone can help me with a C++ program I am working on. The assignment is that the program need to take data from a input file e.g. "inputfile.txt" then copy it to an outputfile e.g. "outputfile.txt" The data has student name and 10 scores. The part I need help is that let say you open a file call "outputfile.txt". inside the file has student name and 10 quiz scores. So it'll look like: Joe Smith 10 4 9 8 10 7 4 2 8 8 ===> all on the same line.
I need to take the sum of 10 scores and average it then display it on the same line after last score. How do I skip over the name part and takes the number scores?
Following is what I have so far:
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <string>
#include <ifstream>
int main()
{
using namespace std;
ifstream in_stream;
ofstream out_stream;
in_stream.open("inputfile.txt");
if (in_stream.fail( ))
{
cout << "Input file opening failed.\n";
exit(1);
}
out_stream.open("outputfile.txt");
if (out_stream.fail( ))
{
cout << "Output file opening failed.\n";
exit(1);
}
char next;
in_stream.get(next);
while (! in_stream.eof())
{
out_stream << next;
in_stream.get(next);
}
**** need to add scores and average it then display it on the same line following the last score ******
in_stream.close( );
out_stream.close( );
system ("Pause");
return 0;
}
Please advice... thanks,
I know this is Mac Forums but I just hope someone can help me with a C++ program I am working on. The assignment is that the program need to take data from a input file e.g. "inputfile.txt" then copy it to an outputfile e.g. "outputfile.txt" The data has student name and 10 scores. The part I need help is that let say you open a file call "outputfile.txt". inside the file has student name and 10 quiz scores. So it'll look like: Joe Smith 10 4 9 8 10 7 4 2 8 8 ===> all on the same line.
I need to take the sum of 10 scores and average it then display it on the same line after last score. How do I skip over the name part and takes the number scores?
Following is what I have so far:
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <string>
#include <ifstream>
int main()
{
using namespace std;
ifstream in_stream;
ofstream out_stream;
in_stream.open("inputfile.txt");
if (in_stream.fail( ))
{
cout << "Input file opening failed.\n";
exit(1);
}
out_stream.open("outputfile.txt");
if (out_stream.fail( ))
{
cout << "Output file opening failed.\n";
exit(1);
}
char next;
in_stream.get(next);
while (! in_stream.eof())
{
out_stream << next;
in_stream.get(next);
}
**** need to add scores and average it then display it on the same line following the last score ******
in_stream.close( );
out_stream.close( );
system ("Pause");
return 0;
}
Please advice... thanks,