Link to home
Start Free TrialLog in
Avatar of katdude
katdude

asked on

How do I fill an Array with contents of a file?

I am using Borland 5.02
I am learning from a book called c++ in 21 days, I cant find a very good example in the book, and I am having trouble initializing the array with the contents of the file.

I am trying to read data from a file with floating point variables, and fill; one array for numbers greater than Zero; and another array for numbers that are less than zero.

I get output, but it isnt correct, and I am not getting all of the file contents.  
 any suggestions?
Avatar of sunj
sunj

can you post your code and your datafile format here so that we can check for you?
Avatar of Jan Louwerens
post your code, so we can see where it's going wrong
ASKER CERTIFIED SOLUTION
Avatar of goltor
goltor
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
sorry, you'll have to increment i at the end of the while loop as well.
goltor: i guess even increment 'i' won't work. we need two variables here, one for each array.
Avatar of katdude

ASKER

here's my code
/////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
#include<iostream.h>
#include<fstream.h>
#include<iomanip.h>
#include<stdlib.h>
int main()
{  //-----------------------Declare array-----------------------------------
      const int MAXNUMS = 160;      //declare an array of intigers
                    int intArray[MAXNUMS];//up to the maximum #'s in length
   //-----------------------------------------------------------------------
         int j =0;         // count frequency of this group
         int i =0;         // count frequency of this group
         float num =0;     // number
         float posnum =0;  // positive numbers
         float negnum =0;  // negative numbers
         int ctr = 1;
   //---------set up arrays for positive and negitive groups----------------
            float PosArray[80]; //set up the positive group array...
         float NegArray[80]; //set up the negative group array....
   //----------------------read in the file here----------------------------
      ifstream infile("a:\\Lab4data.txt");

   if (!infile) //----------- if you aren't reading the correct file -------
           {      cerr << "Sorry! Could Not Open File!" <<endl;
      }//end if                                     // error  message when opening the file

     //-------------------------title---------------------------------------
                     for(i=0; i< 80; i++)
                                      cout << "*";   //creates a line of 80 asterisk's
                     for(i=0; i< 80; i++)
                                      cout << "*";   //creates a line of 80 asterisk's
         cout <<resetiosflags(ios:: adjustfield)
           << setw(35) << " Mark Hadsell " << endl
              << setw(34)<< " CIS - 32 A " << endl
           << setw(32)<< "   LAB 04 " << endl;

                          for( i=0; i< 80; i++)
                        cout << "*";   //creates a line of 80 asterisk's
            for(i=0; i< 80; i++)
                        cout << "*";   //creates a line of 80 asterisk's

   cout    << setw(14) << "POSITIVE"
           << setw(10) << "POS"
           << setw(16) << "NEGATIVE"
           << setw(10) << "NEG" << endl;

  while (infile) // if you are reading the file....


         { infile >> posnum >> negnum ;
       //------- SEPERATE POSITIVE NUMBERS FROM THE NEGATIVE NUMBERS---------

               if (  posnum > 1 ) //
                  {
                     for ( j = 0; j < MAXNUMS; j++)
                  PosArray[25] = posnum;  //positive numbers go in this one
               }

            if ( negnum < -1 )
               {  for( j = 0; j <  MAXNUMS; j++)
                     NegArray[25] = negnum;  //negs go in this one
               }
      //---------------------SET UP THE FORMATING---------------------------
      cout << setprecision(2)<< setiosflags( ios::fixed | ios::showpoint);
      cout << setw(12)<< PosArray[25];       // show positive array
      cout << setw(11)<< intArray[i] ;
      cout << setw(15)<< NegArray[25];  // show negative array
      cout << setw(11)<< intArray[i] <<endl ;
                                 //-------check for zero--------
                         { if (num == 0)                  //check for zero
                     ctr + 1;
                  if (ctr ==2)
                    { infile.eof();}
               }
       //-------------------------end check for zero------------------------
             }//end while

   return 0;
} //end main



---------------------------------------
here's my data file


52.7 -18.2 7.32 -11.1 2.22 52.7 -15.348 52.7 52.7
0 45.45 0 -99.97 -99.97 -98.23 -98.32
42.7 -15.348 -849.5 78.84 -18.2
56.84 0 52.7 -1.89 17.4 -1.9 0 45.45 -98.23 45.452
0 201.01 -32.1 56.0 -32.1
-1.35 -66.6 0
 56.22 -44.4
 0 -99.97 -33.56 0 11.34
-45.44
   -77.67 82.12 -112.67 -41.78
14.45 118.9 -451.2 -333.3 215.25
117.11 -51.74 -156.72 0 0
-111.23 45.451 201.01 0 -74.144
45.451 34.6 23.1 45.451 23.1 201.01
-25.52 34.6 23.1 23.1 -23.1 0
0 56.78 23.1 45.12 12.12 12.12 23.1 78.12 12.12
-12.34 0 78.12 34.01 -12.34 123.1 -23.78 -17.12
 -56.9
 56.78 23.1 78.12 90.56
0  0  101.5 124.6 0
-5.6 7.8 0 -8.9 4.6
0 2.999 7.89 -9.543 -123.4
-32.3 88.8 87.9 -43.12 88.8 -12.12
342.45 17.46 19.23 -87.1 -162.34
0 0  4 6 7
6 7 4 6 7 6 8
8 0 0 -2 -3 -5
-2 -3 -4 -8
0 5 0 0 -1
0 0 7.77 7.77 7.1 0
0




----------------I am totaly confused on this....
katdude,

it's not a good idea to have other people solve your class exercises. You would miss out on the learning opportunity if someone gave you a readymade solution.

The problem with your code is more to do with understanding logic and being able to express it, than with the C++ language.

Goltor's answer is headed in the right direction, although s/he did overlook some things. S/he should have used two different variables i and j for the positive and the negative arrays, and should have incremented either one of them for each input number, where the number is getting added to the appropriate array.

You will at least be able to understand the logic from goltor's code. The key is that you read the input value in a single common variable first. Then, after checking its sign, decide which array it should go into.


Also, in your code, there is loop which is quite uncalled for:

---------

          if (  posnum > 1 ) //
             {
                for ( j = 0; j < MAXNUMS; j++)
                  PosArray[25] = posnum;  //positive numbers go in this one
               }


At that stage, you should be just putting a value into a single array element - i'th or j'th element - whichever is the next element in the appropriate array (positive or negative).

This part of the code suggests that you have some basic gaps in the understanding which you must correct, maybe with the help of books.

Hundreds of 'experts' could give you a ready-made working code. But is that really what you want? You want to learn, right? Not just get this particular class exercise solved. So please spend the time it takes to figure out how things work. Goltor's answer is a good starting point, provided you understood the logic.

If you did not, you could ask people to explain the logic in plain english (like in pseudocode or something) and then try converting it into runnable code yourself! It will take longer; but in the end, the satisfaction you will get will make it all worthwhile.

best wishes
- stochastic