Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,998 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,351 people online right now. Registration is fast and FREE... Join Now!




File Processing Help

 
Reply to this topicStart new topic

File Processing Help, Averaging Values in a File

codenovice
1 Dec, 2006 - 11:38 AM
Post #1

New D.I.C Head
*

Joined: 1 Dec, 2006
Posts: 3


My Contributions
I only have basic code started so far, and I have no idea how to create this program.

I need to read data from a file and average some values together and then store the data.

Here is a sampling of the text file that I am starting with:

100 George 10 22 50 39 44
101 Cindy 22 43 43 27 39
102 Steven 19 18 44 32 45

I need to read this data in and store it in a new file with the "average" of the 5 numbers that come after the person's name. My new file would look like this:

100 George 33.00
101 Cindy 34.80
102 Steven 31.60

I would attach my code, but beyond opening the file, I have no idea where to start.

Thanks to anyone willing to point me in the right direction!!!!!
User is offlineProfile CardPM
+Quote Post

ifoam
RE: File Processing Help
1 Dec, 2006 - 12:00 PM
Post #2

D.I.C Head
**

Joined: 26 Oct, 2006
Posts: 54



Thanked: 1 times
My Contributions
CODE
ofstream file("output.txt");
file << var;
file.close();

ifstream infile("input.txt");
infile >> int1;
infile.close();


you can use that as an example of opening files to write and read.

here is how you can read a line of code using the above code and output the line of code.

CODE

#include <iostream>
#include <fstream>

using namespace std;

void main()
{
int id;
string name;
double g1, g2, g3, g4, g5;

ifstream infile("input.txt");
ofstream file("output.txt");

infile >> id;
infile >> name;
infile >> g1 >> g2 >> g3 >> g4 >> g5;

double average=(g1+g2+g3+g4+g5)/5;

file << id << ' ' << name << ' ' << average;
}


that should work. its not tested. you can use this as a guideline for the whole file



User is offlineProfile CardPM
+Quote Post

codenovice
RE: File Processing Help
1 Dec, 2006 - 12:11 PM
Post #3

New D.I.C Head
*

Joined: 1 Dec, 2006
Posts: 3


My Contributions
Thanks ifoam!!!!

.....but the actual file that I am starting with has 40 values per person!!! Each entry (row) in the file looks like this:

100 George 11 22 33 44 55 55 44 33 22 66 77 88 77 66 55 44 44 ............. 66

Would I write the code like this:

int id;
string name;
double g1, g2, g3, g4, g5, g6, g7, g8 .........g40;

ifstream infile("input.txt");
ofstream file("output.txt");

infile >> id;
infile >> name;
infile >> g1 >> g2 >> g3 >> g4 >> g5 >> g6 >> g7 >> g8 ...................... >> g40;

double average=(g1+g2+g3+g4+g5+g6+g7+g8..........+g40)/40;

OR is there a simpler way to traverse trough the values?????

Thanks!!

User is offlineProfile CardPM
+Quote Post

ifoam
RE: File Processing Help
1 Dec, 2006 - 12:16 PM
Post #4

D.I.C Head
**

Joined: 26 Oct, 2006
Posts: 54



Thanked: 1 times
My Contributions
you can use an array then. there's a similar topic with this on the forum, its on the top. you could use

float grades[40];

for(int x=0; x <= 39; x++)
infile >> grades[x];

this reads the first grade and puts it in grades[0]. the x is incremented because the loop completed. so it reads the next value and stores it in grades[1] and so one. once x=39 (39 because we start at 0) it stops because it read all the grades.

then to average

float sum, average;

for (int x=0; x <= 39; x++)
sum +=grade[x];
average = sum/40;

something like that

This post has been edited by ifoam: 1 Dec, 2006 - 12:18 PM
User is offlineProfile CardPM
+Quote Post

ifoam
RE: File Processing Help
1 Dec, 2006 - 12:36 PM
Post #5

D.I.C Head
**

Joined: 26 Oct, 2006
Posts: 54



Thanked: 1 times
My Contributions
i forgot to initialize the variables. add sum=average=0;
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 08:19PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month