PDA

View Full Version : i/o woes



mol
October 12th, 2001, 04:59 PM
Check it out:




#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

void main()
{
int count;
float num;
float total;
float avg;

ifstream inData;
ofstream outData;

// lets get them all on one line
inData.open("datfile1.txt");
outData.open("outdata.txt");

while(inData >> num)
{
outData << num << " ";
}

outData << endl;
inData.close();
inData.open("datfile1.txt");

count = 0;
avg = 0;
total = 0;

outData << fixed << showpoint
<< setprecision(3);

while(inData >> num)
{
total += num;
cout << total << " " << num << endl;
count++;
}

cout << total << endl << count;

avg = total / count;
outData << avg << endl;

inData.close();
}



This is school crapola. I have to use two loops (if I could do it the proper way I wouldnt be having a problem) anyway, it just doesnt perform the second loop. If you comment out the first loop, then the second one works.

I just dont get it.

Myst
October 12th, 2001, 05:29 PM
Originally posted by mol
Check it out:



...
while(inData >> num)
{
outData << num << " ";
}




while(inData >> num)
{
cout << outData << num << " ";
}

??

I thought the greater then operator was > and the less then < not >> or << ... ?
(says she who has no idea how to do c/c++)

mol
October 15th, 2001, 03:08 PM
Originally posted by Myst



while(inData >> num)
{
cout << outData << num << " ";
}

??

I thought the greater then operator was > and the less then < not >> or << ... ?
(says she who has no idea how to do c/c++)

outData is a filestream, that is why you can direct output to it. In c++, think i/o in different terms. Meaning, those >> and << are kind of like arrows directing where output/input is going.

For example:




cout << "Hello World!";

cin >> SomeInput;



Hello world is directed out the standard output. And cin reads IN input and puts it in the var SomeInput.

Myst
October 16th, 2001, 05:14 AM
Originally posted by mol
Hello world is directed out the standard output. And cin reads IN input and puts it in the var SomeInput.

yeah that'd be like the only part of C++ I know. Oh that and we spent like 3 2 hour classes on commenting code (this in second semester, after people had a semester of java), and I'm not kidding :rolleyes:

Ok ok so I just didn't go back to the class.. now I should dig out my textbook and actually read it I guess :)

Have you figured out the problem now?

mol
October 16th, 2001, 12:42 PM
Yup. The filestream had dropped into a failstate which means I have to clear it before reopening...or moving the file pointer. Here is the corrected code. Note: I am moving the file pointer now, not opening and then closing and then reopening.



#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

void main()
{
int count;
float num;
float total;
float avg;

ifstream inData;
ofstream outData;

// lets get them all on one line
inData.open("datfile1.txt");
outData.open("outdata.txt");

while(inData >> num)
{
outData << num << " ";
}

outData << endl;

inData.clear();
inData.seekg(0);

count = 0;
avg = 0;
total = 0;

outData << fixed << showpoint
<< setprecision(3);

while(inData >> num)
{
total += num;
count++;
}

avg = total / count;
outData << avg << endl;

inData.close();
}