Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › C++ read from file then output.
New Posts  All Forums:Forum Nav:

C++ read from file then output.

post #1 of 7
Thread Starter 
Ok well I need to be able to input grades from a file so I can alulate each lines avergae then output the same lines into a file with the average at the end of the line.

the text file looks like this:
Code:
test_ line_ 10 20 30 40 50 60 70 80 90 100
Price Betty 40 50 60 70 60 50 40 30 60 90
Good John 60 70 80 90 50 60 90 90 100 90
Smith Charles 70 80 90 60 70 60 80 90 90 90
Spangenberg Ward 70 70 80 90 70 80 90 80 70 60

For now I would just like someone to explain as I want to do it myself I just need help understanding how.

Here is my code, I realize the void statement at the end is not being used currently, I was just trying to see if it would work at first.
code (Click to show)
Code:
#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

void calc(ifstream& , ofstream&, double sum, double average);

int main ()
{
ifstream inpu;
ofstream outp;
double sum = 0, avg = 0;

inpu.open("gradeBook.txt");
if (inpu.fail())
{
cout << "Input file opening failed.\n";
exit(1);
}

outp.open("gradebooknew.txt");
if (outp.fail())
{
cout << "Output file creation failed.\n";
exit (1);
}

double next, summ = 0;
int count = 0;
while (inpu >> next)
{
summ = summ + next;
count++;
}

cout << summ << endl;




system ("pause");
return 0;
}


void calc(ifstream& inpu , ofstream& outfile, double sum, double avg)
{
double next, summ = 0;
int count = 0;
while (inpu >> next)
{
summ = summ + next;
count++;
}

cout << sum << endl;
}


Thank you
    
CPUMotherboardGraphicsRAM
Intel Core i7 930 [4.33 Ghz] EVGA X58 SLI LE EVGA GTS 450 [SLI] 6 GB DDR 3 OCZ 1600 [1T-7-8-7-18 @ 1474] 
Hard DriveOptical DriveOSMonitor
Vertex 2 [120 GB] + 4x WD Green 2 TB [Raid1] Samsung BD-R [12x] Microsoft Windows 7 Pro [x64] [3] Samsung SyncMaster 2233sw 
KeyboardPowerCaseMouse
I-rocks Corsair HX650 [650W] Lian Li PC K62 Logi 
Mouse Pad
Metallica 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
Intel Core i7 930 [4.33 Ghz] EVGA X58 SLI LE EVGA GTS 450 [SLI] 6 GB DDR 3 OCZ 1600 [1T-7-8-7-18 @ 1474] 
Hard DriveOptical DriveOSMonitor
Vertex 2 [120 GB] + 4x WD Green 2 TB [Raid1] Samsung BD-R [12x] Microsoft Windows 7 Pro [x64] [3] Samsung SyncMaster 2233sw 
KeyboardPowerCaseMouse
I-rocks Corsair HX650 [650W] Lian Li PC K62 Logi 
Mouse Pad
Metallica 
  hide details  
Reply
post #2 of 7
I am not sure what that next is suppose to be doing. Plus I think iostream will automatically include fstream.

I also don't see where you are reading or writing lines, is that what you are wanting help with?
I Love Cheese
(13 items)
 
  
CPUMotherboardGraphicsRAM
2600k @ 3.4 Asus P8P67 Basic XFX 5970 2 gig Black Edition 2x 4 gig DDR 1600 G-Skill 
Hard DriveOSMonitorPower
2x500 gig WD Vista Ultimate 64-bit Dual 25" Hannspree LCD CM 750 Watt 
CaseMouse
Rosewill THOR Logitech Trackball 
  hide details  
Reply
I Love Cheese
(13 items)
 
  
CPUMotherboardGraphicsRAM
2600k @ 3.4 Asus P8P67 Basic XFX 5970 2 gig Black Edition 2x 4 gig DDR 1600 G-Skill 
Hard DriveOSMonitorPower
2x500 gig WD Vista Ultimate 64-bit Dual 25" Hannspree LCD CM 750 Watt 
CaseMouse
Rosewill THOR Logitech Trackball 
  hide details  
Reply
post #3 of 7
First you have to read the file.
Second since there is gonna be string and int/double in the same line you have to make a function or something to separate them.

I did something similar in class.
I have to input into a file then read from the file.
Hope it help.
Code:
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int main()
{
fstream studentFile;
studentFile.open("C:\\students.txt",ios::out);
char name[40];
char more;
int grade1,grade2,grade3;
double avg;
do
{
cout<<"Enter name"<<endl;
cin.getline(name,39,'\n');
studentFile<<name<<'\n';
cout<<"Enter three grades"<<endl;
cin>>grade1>>grade2>>grade3;
studentFile<<grade1<<' '<<grade2<<' '<<grade3<<'\n';
cout<<"Do you want to enter another name?(Y/N)"<<endl;
cin>>more;
cin.ignore();
} while(more=='y' || more=='Y');
studentFile.close();
cout<<left<<setw(15)<<"Name";
cout<<left<<setw(15)<<"Grade 1";
cout<<left<<setw(15)<<"Grade 2";
cout<<left<<setw(15)<<"Grade 3";
cout<<left<<setw(15)<<"Average";
cout<<endl<<endl;
studentFile.open("C:\\students.txt",ios::in);
while(studentFile.getline(name,39,'\n') && studentFile>>grade1>>grade2>>grade3)
{
avg=(double)(grade1+grade2+grade3)/3.0;
cout<<left<<setw(15)<<name;
cout<<left<<setw(15)<<grade1;
cout<<left<<setw(15)<<grade2;
cout<<left<<setw(15)<<grade3;
cout<<left<<setw(15)<<setprecision(1)<<fixed<<avg;
cout<<endl;
studentFile.ignore(10000,'\n');
}
studentFile.close();
system ("pause");
}

@joemaniaci - u need to include the fstream. is for input/output for a file if I remember correctly.
post #4 of 7
Thread Starter 
Quote:
Originally Posted by joemaniaci;13271283 
I am not sure what that next is suppose to be doing. Plus I think iostream will automatically include fstream.

I also don't see where you are reading or writing lines, is that what you are wanting help with?

Well the book is extremely confusing when explaining it. So I am wondering how exactly do I just have it calculate one line then go to the next?

Do I need to make variables for each grade? and how does it know to just read one line and skip the name?

Changed my code to a very basic input style

Obviously I could get it to average them. but how do I automate the next steps in a loop.
Code:
#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

int main ()
{
ifstream instream;
ofstream outstream;
double sum, avg;
char fname[10];
char lname[10];
double g1, g2, g3, g4, g5, g6, g7, g8, g9, g10;

instream.open("gradeBook.txt");
if (instream.fail())
{
cout << "Input file opening failed.\n";
exit(1);
}

outstream.open("gradebooknew.txt");
if (outstream.fail())
{
cout << "Output file creation failed.\n";
exit (1);
}


instream >> fname >> lname >> g1 >> g2 >> g3 >> g4 >> g5 >> g6 >> g7 >> g8 >> g9 >> g10;

cout << fname << lname << g1 << g2 << g3 << g4 << g5 << g6 << g7 << g8 << g9 << g10 << endl;

outstream << fname << lname << g1 << g2 << g3 << g4 << g5 << g6 << g7 << g8 << g9 << g10 << endl;;

instream.close();
outstream.close();


system ("pause");
return 0;
}

Edited by sdla4ever - 4/25/11 at 6:17pm
    
CPUMotherboardGraphicsRAM
Intel Core i7 930 [4.33 Ghz] EVGA X58 SLI LE EVGA GTS 450 [SLI] 6 GB DDR 3 OCZ 1600 [1T-7-8-7-18 @ 1474] 
Hard DriveOptical DriveOSMonitor
Vertex 2 [120 GB] + 4x WD Green 2 TB [Raid1] Samsung BD-R [12x] Microsoft Windows 7 Pro [x64] [3] Samsung SyncMaster 2233sw 
KeyboardPowerCaseMouse
I-rocks Corsair HX650 [650W] Lian Li PC K62 Logi 
Mouse Pad
Metallica 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
Intel Core i7 930 [4.33 Ghz] EVGA X58 SLI LE EVGA GTS 450 [SLI] 6 GB DDR 3 OCZ 1600 [1T-7-8-7-18 @ 1474] 
Hard DriveOptical DriveOSMonitor
Vertex 2 [120 GB] + 4x WD Green 2 TB [Raid1] Samsung BD-R [12x] Microsoft Windows 7 Pro [x64] [3] Samsung SyncMaster 2233sw 
KeyboardPowerCaseMouse
I-rocks Corsair HX650 [650W] Lian Li PC K62 Logi 
Mouse Pad
Metallica 
  hide details  
Reply
post #5 of 7
Try this.
Code:
while(instream >> fname >> lname >> g1 >> g2 >> g3 >> g4 >> g5 >> g6 >> g7 >> g8 >> g9 >> g10;
{
cout << fname << lname << g1 << g2 << g3 << g4 << g5 << g6 << g7 << g8 << g9 << g10 << endl;
outstream << fname << lname << g1 << g2 << g3 << g4 << g5 << g6 << g7 << g8 << g9 << g10 << endl;
}
post #6 of 7
Thread Starter 
Quote:
Originally Posted by TLCH723;13271837 
Try this.
Code:
while(instream >> fname >> lname >> g1 >> g2 >> g3 >> g4 >> g5 >> g6 >> g7 >> g8 >> g9 >> g10;
{
cout << fname << lname << g1 << g2 << g3 << g4 << g5 << g6 << g7 << g8 << g9 << g10 << endl;
outstream << fname << lname << g1 << g2 << g3 << g4 << g5 << g6 << g7 << g8 << g9 << g10 << endl;
}

that just outputs the last line
    
CPUMotherboardGraphicsRAM
Intel Core i7 930 [4.33 Ghz] EVGA X58 SLI LE EVGA GTS 450 [SLI] 6 GB DDR 3 OCZ 1600 [1T-7-8-7-18 @ 1474] 
Hard DriveOptical DriveOSMonitor
Vertex 2 [120 GB] + 4x WD Green 2 TB [Raid1] Samsung BD-R [12x] Microsoft Windows 7 Pro [x64] [3] Samsung SyncMaster 2233sw 
KeyboardPowerCaseMouse
I-rocks Corsair HX650 [650W] Lian Li PC K62 Logi 
Mouse Pad
Metallica 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
Intel Core i7 930 [4.33 Ghz] EVGA X58 SLI LE EVGA GTS 450 [SLI] 6 GB DDR 3 OCZ 1600 [1T-7-8-7-18 @ 1474] 
Hard DriveOptical DriveOSMonitor
Vertex 2 [120 GB] + 4x WD Green 2 TB [Raid1] Samsung BD-R [12x] Microsoft Windows 7 Pro [x64] [3] Samsung SyncMaster 2233sw 
KeyboardPowerCaseMouse
I-rocks Corsair HX650 [650W] Lian Li PC K62 Logi 
Mouse Pad
Metallica 
  hide details  
Reply
post #7 of 7
You have to make it read from the beginning of the file again.
i think is "ios::beg"
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › C++ read from file then output.