Notices
The Basement Non-Honda/Acura discussion. Content should be tasteful and "primetime" safe.

anyone with C++ skills wanna help me out real quick??

Old Feb 21, 2004 | 01:41 AM
  #11  
mayonaise's Avatar
mayonaise
Senior Member
 
Joined: Aug 2002
Posts: 3,181
Likes: 0
From: CA
Default

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
Reply
Old Feb 21, 2004 | 01:52 AM
  #12  
aznd000d's Avatar
aznd000d
Thread Starter
yoooooooooooo
 
Joined: Oct 2003
Posts: 148
Likes: 0
From: Riverside, CA
Default

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:
Reply
Old Feb 21, 2004 | 02:03 AM
  #13  
mayonaise's Avatar
mayonaise
Senior Member
 
Joined: Aug 2002
Posts: 3,181
Likes: 0
From: CA
Default

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:
works for me (both ways i mentioned), so you're probably doing something wrong. make sure you aren't still closing or opening any file descriptors inside of the while loop, make sure you close both of them at the end of the program, and make sure you're using two separate file descriptors and using each one in the right place.

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.
Reply
Old Feb 21, 2004 | 02:08 AM
  #14  
aznd000d's Avatar
aznd000d
Thread Starter
yoooooooooooo
 
Joined: Oct 2003
Posts: 148
Likes: 0
From: Riverside, CA
Default

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.
Reply
Old Feb 21, 2004 | 02:51 AM
  #15  
aznd000d's Avatar
aznd000d
Thread Starter
yoooooooooooo
 
Joined: Oct 2003
Posts: 148
Likes: 0
From: Riverside, CA
Default

SOB.........still doesn't work.........FAWK!!!!!!!!!!!!!!!!!!!!!!
Reply
Old Feb 21, 2004 | 10:50 AM
  #16  
mayonaise's Avatar
mayonaise
Senior Member
 
Joined: Aug 2002
Posts: 3,181
Likes: 0
From: CA
Default

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();
}
Reply
Old Feb 21, 2004 | 10:51 AM
  #17  
axemansean's Avatar
axemansean
Senior Member
 
Joined: Jun 2002
Posts: 13,634
Likes: 0
Default

C > C++
Reply
Old Feb 21, 2004 | 10:59 AM
  #18  
mayonaise's Avatar
mayonaise
Senior Member
 
Joined: Aug 2002
Posts: 3,181
Likes: 0
From: CA
Default

Originally Posted by axemansean
C > C++
most people would disagree with you
Reply
Old Feb 21, 2004 | 11:01 AM
  #19  
axemansean's Avatar
axemansean
Senior Member
 
Joined: Jun 2002
Posts: 13,634
Likes: 0
Default

Originally Posted by mayonaise
most people would disagree with you
Most non programmers will, C is much closer to assembly language and allows a lot more freedom.
Reply
Old Feb 21, 2004 | 11:10 AM
  #20  
Civic2Scooby's Avatar
Civic2Scooby
 
Joined: Jul 2003
Posts: 28,282
Likes: 0
From: michigan
Default

Originally Posted by axemansean
Most non programmers will, C is much closer to assembly language and allows a lot more freedom.
Basic > *

:lmao:
Reply


All times are GMT -8. The time now is 12:22 AM.