anyone with C++ skills wanna help me out real quick??
i got it working with these fixes:
first i'd advise you to use separate file descriptors for groupA.txt and groupB.txt, so that they don't just share the same one, fout. this could be the reason you're only getting one file of output.
you're closing your file descriptors at the end of the program, not at the end of the while loop. in this way, you just keep opening them over and over again. the data you wrote to the file will only be commited once you close the file descriptor.
once you've fixed that, you will still probably get one line of output in each file, but that's because the open() function for an ofstream object is set to truncate by default. meaning each time you open a file, if the file exists already, all of its contents will be cleared. you have to figure out how to set an append flag, so that it'll append whatever you want to write to the file, not truncate it.
HOWEVER, if you choose to do it this way, every time you run the program you'll keep appending on to the same groupA.txt and groupB.txt files that already exist from previous executions of the program. so to get rid of this, it's probably a better idea to open the two output files at the beginning of the program (before the while loop), set to truncate, run thru the while loop, and then close the files at the end. so anything that happens in between will just get written to the output files, and you don't really have to worry about opening and closing them a bunch of times - also makes the program more efficient. make sense?
and this is just a suggestion; work on your indentation and bracket placing. your code is impossible to read
first i'd advise you to use separate file descriptors for groupA.txt and groupB.txt, so that they don't just share the same one, fout. this could be the reason you're only getting one file of output.
you're closing your file descriptors at the end of the program, not at the end of the while loop. in this way, you just keep opening them over and over again. the data you wrote to the file will only be commited once you close the file descriptor.
once you've fixed that, you will still probably get one line of output in each file, but that's because the open() function for an ofstream object is set to truncate by default. meaning each time you open a file, if the file exists already, all of its contents will be cleared. you have to figure out how to set an append flag, so that it'll append whatever you want to write to the file, not truncate it.
HOWEVER, if you choose to do it this way, every time you run the program you'll keep appending on to the same groupA.txt and groupB.txt files that already exist from previous executions of the program. so to get rid of this, it's probably a better idea to open the two output files at the beginning of the program (before the while loop), set to truncate, run thru the while loop, and then close the files at the end. so anything that happens in between will just get written to the output files, and you don't really have to worry about opening and closing them a bunch of times - also makes the program more efficient. make sense?
and this is just a suggestion; work on your indentation and bracket placing. your code is impossible to read
ok, I just tried the suggestion of opening both output files b4 the main while-loop, but that doesn't work.
I did indent and put brackets in my code.....does it not show correctly in the 1st post?
btw, can I uhhhhhhhhhh see the code after you uhhhhhhhhh fixed it???
h:
I did indent and put brackets in my code.....does it not show correctly in the 1st post?
btw, can I uhhhhhhhhhh see the code after you uhhhhhhhhh fixed it???
h:
Originally Posted by aznd000d
ok, I just tried the suggestion of opening both output files b4 the main while-loop, but that doesn't work.
I did indent and put brackets in my code.....does it not show correctly in the 1st post?
btw, can I uhhhhhhhhhh see the code after you uhhhhhhhhh fixed it???
h:
I did indent and put brackets in my code.....does it not show correctly in the 1st post?
btw, can I uhhhhhhhhhh see the code after you uhhhhhhhhh fixed it???
h:it's a really easy fix, so maybe in the morning if you haven't gotten it i'll give you the code. but i'm going to sleep now. good luck
the way you have things indented (at least the way it appears on this page) is pretty confusing. i can see how you're doing it for the most part, but it's not consistent throughout the code.
awwww meng. I wanna go to sleeeep too........hahaha
it's already 3AM, and I gotta be up at 8 tomorrow.
but yeah, i'm messing with it right now, but no luck yet.
btw, can you get it to RUN correctly? or just compile correctly???
I can compile without any errors too, but it doesn't run correctly.
it's already 3AM, and I gotta be up at 8 tomorrow.
but yeah, i'm messing with it right now, but no luck yet.
btw, can you get it to RUN correctly? or just compile correctly???
I can compile without any errors too, but it doesn't run correctly.
han isn't letting me send PMs - keeps telling me i'm not logged in. anyways yeah, it compiles and runs fine. the only exception being that it seems to cut off trailing zeros, whereas the sample solution you posted doesn't. eg, "4.10" becomes "4.1" in the output files. anyways, i did tell you exactly what i did, but since you still can't figure it out, here it is.
Code:
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main() {
ifstream fin;
ofstream a;
ofstream b;
fin.open("input.txt");
if (fin.fail())
{
cout << "file 'input.txt' failed to open.";
exit(1);
}
double weight;
int count = 0;
double sum = 0;
double average;
char group;
a.open("groupA.txt");
b.open("groupB.txt");
while (!fin.eof())
{
fin >> group;
if (group == 'A')
{
while (count < 10)
{
fin >> weight;
sum = sum + weight;
a << weight << " ";
count++;
}
average = sum / count;
a << "average = " << average << endl;
count = 0;
sum = 0;
}
if (group == 'B')
{
while (count < 10)
{
fin >> weight;
sum = sum + weight;
b << weight << " ";
count++;
}
average = sum / count;
b << "average = " << average << endl;
count = 0;
sum = 0;
}
}
a.close();
b.close();
fin.close();
}


