Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Oct 25, 2009, 03:03 AM   #1
xyzmax
macrumors newbie
 
Join Date: Aug 2009
C++ Programming... adding student scores

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,
xyzmax is offline   0 Reply With Quote
Old Oct 25, 2009, 01:07 PM   #2
lloyddean
macrumors 6502a
 
Join Date: May 2009
Location: Des Moines, WA
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.



Code:
#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;
}

Last edited by lloyddean; Oct 25, 2009 at 05:09 PM. Reason: Changed to use stream iterators
lloyddean is offline   0 Reply With Quote
Old Oct 26, 2009, 03:54 PM   #3
xyzmax
Thread Starter
macrumors newbie
 
Join Date: Aug 2009
Thanks,

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


xyzmax is offline   0 Reply With Quote
Old Oct 26, 2009, 04:21 PM   #4
iSee
macrumors 68030
 
iSee's Avatar
 
Join Date: Oct 2004
Your main loop:
Code:
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.
__________________
"Nobody ever reads these things so I can write anything. I'd eat bananas every day if they were crunchy."
iSee is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
Logic Studio 9- help with score? eliotschreiner Mac Applications and Mac App Store 0 Nov 26, 2010 02:35 PM
Programs For Students Ruffian829 Mac Basics and Help 12 Apr 13, 2009 01:27 PM
Student dev program & student discount... SilentPanda Buying Tips, Advice and Discussion (archive) 3 Jul 13, 2004 01:06 AM
Program For Students To Manage Stress wdlove Current Events 6 Dec 9, 2003 05:15 AM


All times are GMT -5. The time now is 02:24 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC