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

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

Thread Tools
 
Old Feb 21, 2004 | 12:15 AM
  #1  
aznd000d's Avatar
aznd000d
Thread Starter
yoooooooooooo
 
Joined: Oct 2003
Posts: 148
Likes: 0
From: Riverside, CA
Default anyone with C++ skills wanna help me out real quick??

I'm trying to do this assignment for CS10 (basic of programming with C++).

it's giving me a crap load of trouble. Just wondering if anyone can help me out.
Reply
Old Feb 21, 2004 | 12:22 AM
  #2  
mayonaise's Avatar
mayonaise
Senior Member
 
Joined: Aug 2002
Posts: 3,181
Likes: 0
From: CA
Default

Originally Posted by aznd000d
I'm trying to do this assignment for CS10 (basic of programming with C++).

it's giving me a crap load of trouble. Just wondering if anyone can help me out.
what problems are you having?
Reply
Old Feb 21, 2004 | 12:32 AM
  #3  
aznd000d's Avatar
aznd000d
Thread Starter
yoooooooooooo
 
Joined: Oct 2003
Posts: 148
Likes: 0
From: Riverside, CA
Default

well, I have to write a program that will take info from the input.txt file and output it to either groupA.txt or groupB.txt files.

the input.txt file looks like this:
A 4.45 3.56 4.12 3.78 4.24 3.96 4.13 4.05 3.68 4.34
B 4.10 4.24 3.78 3.94 4.37 4.06 4.28 3.99 4.00 3.85
B 3.69 3.82 4.56 4.89 4.67 4.04 4.37 3.88 3.89 4.56
A 4.61 4.50 4.63 4.28 3.71 3.75 3.93 4.62 4.48 4.04
A 3.96 4.13 4.05 3.68 4.34 3.69 3.82 4.56 4.89 4.67
A 4.06 4.28 3.99 4.00 3.85 3.72 4.18 4.25 3.58 4.38
B 3.75 3.93 4.62 4.48 4.04 4.45 3.56 4.12 3.78 4.24
A 4.04 4.37 3.88 3.89 4.56 4.61 4.50 4.63 4.28 3.71
B 3.72 4.18 4.25 3.58 4.38 4.13 3.96 4.37 4.49 3.62
A 4.45 3.56 4.12 3.78 4.24 3.75 3.93 4.62 4.48 4.04
B 3.75 3.93 4.62 4.48 4.04 3.96 4.13 4.05 3.68 4.34
B 4.04 4.37 3.88 3.89 4.56 3.78 4.24 3.96 4.13 4.05

The letters infront of each line represent each type of info.
lines staring with A should be gathered into groupA.txt file and calculate it's average, and the same thing for lines starting with B.

the output files should like this:
groupA.txt
4.45 3.56 4.12 3.78 4.24 3.96 4.13 4.05 3.68 4.34 AVG: 4.031 ounces
4.61 4.50 4.63 4.28 3.71 3.75 3.93 4.62 4.48 4.04 AVG: 4.255 ounces
3.96 4.13 4.05 3.68 4.34 3.69 3.82 4.56 4.89 4.67 AVG: 4.179 ounces
4.06 4.28 3.99 4.00 3.85 3.72 4.18 4.25 3.58 4.38 AVG: 4.029 ounces
4.04 4.37 3.88 3.89 4.56 4.61 4.50 4.63 4.28 3.71 AVG: 4.247 ounces
4.45 3.56 4.12 3.78 4.24 3.75 3.93 4.62 4.48 4.04 AVG: 4.097 ounces

groupB.txt
4.10 4.24 3.78 3.94 4.37 4.06 4.28 3.99 4.00 3.85 AVG: 4.061 ounces
3.69 3.82 4.56 4.89 4.67 4.04 4.37 3.88 3.89 4.56 AVG: 4.237 ounces
3.75 3.93 4.62 4.48 4.04 4.45 3.56 4.12 3.78 4.24 AVG: 4.097 ounces
3.72 4.18 4.25 3.58 4.38 4.13 3.96 4.37 4.49 3.62 AVG: 4.068 ounces
3.75 3.93 4.62 4.48 4.04 3.96 4.13 4.05 3.68 4.34 AVG: 4.098 ounces
4.04 4.37 3.88 3.89 4.56 3.78 4.24 3.96 4.13 4.05 AVG: 4.090 ounces

-----------------------------------------------------------------------------------
ok, this is what I wrote, it compiles, but it doesn't run correctly.

Code:
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

int main ()
{

     ifstream fin;
     ofstream fout;

     fin.open ("input.txt");
     if (fin.fail())
          {
          cout << "File 'input.dat' failed to open.";
          exit (1);
          }

     double weight;
     int count = 0;
     double sum = 0;
     double average;
     char group;


     while (!fin.eof())

          {
          fin >> group;

          if (group == 'A')

               {
               fout.open("groupA.txt");
                    while (count < 10)

                         {    
                         fin >> weight;

                         sum = sum + weight;
                         fout << weight << " ";
                         count = count + 1;
                         }
              average = sum / count;
              fout <<"average = " << average <<endl;

              count = 0;
              sum = 0;
              }




           if (group == 'B')
                {
                fout.open("groupB.txt");
                     while (count < 10)
                     {
                     fin >> weight;
                     fout << weight << " ";
                     sum = sum + weight;
                     count = count + 1;
                     }

                average = sum / count;
                fout  << "Average = " << average << endl;

                count = 0;
                sum = 0;
                }
            }


     fin.close();
     fout.close();
     fout.close();
}
Reply
Old Feb 21, 2004 | 12:47 AM
  #4  
aznd000d's Avatar
aznd000d
Thread Starter
yoooooooooooo
 
Joined: Oct 2003
Posts: 148
Likes: 0
From: Riverside, CA
Default

btw, the problem is..

it only output to "groupA.txt" file, no "groupB.txt"

and "groupA.txt" file only contain this line:

4.45 3.56 4.12 3.78 4.24 3.96 4.13 4.05 3.68 4.34 average = 4.031


fawk, good thing this is the only CS class I have to take.
Reply
Old Feb 21, 2004 | 01:09 AM
  #5  
brtecson's Avatar
brtecson
pukimonster
 
Joined: Jun 2002
Posts: 9,967
Likes: 2
From: Milwaukee, WI
Default

" while (!fin.eof()) "... i have no idea what that is. other than that, your logic looks correct to me:dunno:

it's been a while since i did c++, so i'm not much helph:
Reply
Old Feb 21, 2004 | 01:09 AM
  #6  
xX94aCcOrDXx's Avatar
xX94aCcOrDXx
leet member
 
Joined: Nov 2002
Posts: 787
Likes: 0
From: Nor*CAL
Default

took intro to comp sci 3 years ago, it was a bitch. involves too much logic....
Reply
Old Feb 21, 2004 | 01:18 AM
  #7  
aznd000d's Avatar
aznd000d
Thread Starter
yoooooooooooo
 
Joined: Oct 2003
Posts: 148
Likes: 0
From: Riverside, CA
Default

eof ........i think = to end of file
so !fin.eof is.... take in data until end of file.......then break out.

so while (!fin.eof()) is........as long as it's not at end of file....keep looping.

CS is kicking my ass right now. i've been trying to debug this program for 2 hrs now. Still can't seem to find out why it's not running correctly.
Reply
Old Feb 21, 2004 | 01:25 AM
  #8  
brtecson's Avatar
brtecson
pukimonster
 
Joined: Jun 2002
Posts: 9,967
Likes: 2
From: Milwaukee, WI
Default

i also thought you have to close your fout file before you open a different fout file :thinking:

when's it due?? i always find that a good nights sleep helps me troubleshoot
Reply
Old Feb 21, 2004 | 01:29 AM
  #9  
aznd000d's Avatar
aznd000d
Thread Starter
yoooooooooooo
 
Joined: Oct 2003
Posts: 148
Likes: 0
From: Riverside, CA
Default

really???

but if I close the fout after each time. wouldn't that mean the next time I open and fout again, wouldn't that overwrite what's currently in that file?

let me try it real quick.

oh btw, this assignment was due uhhhhhhh 2 hrs ago...hehehehe. just coudln't finish it in time.
Reply
Old Feb 21, 2004 | 01:33 AM
  #10  
aznd000d's Avatar
aznd000d
Thread Starter
yoooooooooooo
 
Joined: Oct 2003
Posts: 148
Likes: 0
From: Riverside, CA
Default

well damn.

now the program outputs both "groupA.txt" and "groupB.txt". But as I expected. it overwrites the file each time it opens up.

so now each output file only has one line of data from the last line of the sample output files.


so now the question is.......how's the command to tell the program to NOT overwrite what's in that file.
Reply



All times are GMT -8. The time now is 04:24 PM.