PDA

View Full Version : C++ Programming... adding student scores




xyzmax
Oct 25, 2009, 03:03 AM
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,



lloyddean
Oct 25, 2009, 01:07 PM
Having used the word "assignment" I'm assuming this is homework for a programming related course so I include the following which will probably make use material not yet covered in class. As such you won't be able to turn it in as your own but it should still be useful as a source of inspiration.




#include <algorithm>
#include <fstream>
#include <iostream>
#include <numeric>
#include <sstream>
#include <string>
#include <vector>

int main()
{
using std::accumulate;
using std::getline;
using std::istream_iterator;
using std::ifstream;
using std::ofstream;
using std::string;
using std::istringstream;
using std::vector;

typedef vector<int> scores_t;
typedef vector<int>::iterator scores_itr;

ifstream ifs("inputfile.txt");
ofstream ofs("outputfile.txt");

if ( ifs.is_open() && ofs.is_open() )
{
while ( ifs )
{
string str;
getline(ifs, str);

// check if the line read was empty ...
if ( 0 == str.length() ) continue;

istringstream iss(str);

string strFirst, strLast;
istream_iterator<string, char> fitrString(iss);
strFirst = *fitrString++;
strLast = *fitrString++;

scores_t scores;
istream_iterator<int, char> fitrInt(iss), fitrEOF;
while ( fitrInt != fitrEOF )
{
scores.push_back(*fitrInt);
++fitrInt;
}

int average;
average = accumulate(scores.begin(), scores.end(), 0) / scores.size();

ofs << strFirst << " " << strLast;
for ( scores_itr itr = scores.begin(); itr != scores.end(); ++itr )
{
ofs << " " << *itr;
}

ofs << " " << average << "\n";
}
}

return 0;
}

xyzmax
Oct 26, 2009, 03:54 PM
Thanks,

those codes looks so foreign to me now. Guess we havent cover that subject yet. But nevertheless, thanks for your help.


:)

iSee
Oct 26, 2009, 04:21 PM
Your main loop:

char next;
in_stream.get(next);
while (! in_stream.eof())
{
out_stream << next;
in_stream.get(next);
}

doesn't do anything with the characters it reads.
It reads each character in and puts it back out. You need to do something with those characters.

In order to give you more of a hint, though, I need to ask: are you supposed to parse the input character by character for this assignment, or can you read whole strings or integers from the input stream?

By the way, putting your code in code tags if helpful.