Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Oct 25, 2009, 04: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, 02: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 06:09 PM. Reason: Changed to use stream iterators
lloyddean is offline   0 Reply With Quote
Old Oct 26, 2009, 04: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, 05:21 PM   #4
iSee
macrumors 68040
 
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

Similar Threads
thread Thread Starter Forum Replies Last Post
Mac programming should be more like iOS programming. moonman239 Mac Programming 13 Feb 17, 2014 02:37 PM
Adding a Custom Phone Label when adding or Editing contacts Chris Kavanagh iOS 7 0 Oct 20, 2013 05:28 PM
Geekbench scores Matt8045 iPhone 3 Sep 21, 2013 02:45 AM
Geekbench Scores - Let's Get This Right DanteMann Alternatives to iOS and iOS Devices 43 Sep 22, 2012 01:37 PM
Student gift card offer if payer is not a student jrrrgoldie iPad 3 Jul 19, 2012 12:52 PM

Forum Jump

All times are GMT -5. The time now is 12:10 PM.

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

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