Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2000-03-27
7
Medium Priority
?
209 Views
Last Modified: 2010-04-10
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?
0
Comment
Question by:katdude
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 1

Expert Comment

by:sunj
ID: 2662637
can you post your code and your datafile format here so that we can check for you?
0
 
LVL 5

Expert Comment

by:Jan Louwerens
ID: 2662641
post your code, so we can see where it's going wrong
0
 
LVL 2

Accepted Solution

by:
goltor earned 200 total points
ID: 2662649
// declare a stream to file
ifstream instream;

// open the file
instream.open(filename);

// declare variable for counter and temp
float temp = 0.0;
int i = 0;

// initialize two arrays, one for
// positive numbers and one for negative

float positive[some const],
      negative[some const];

// read numbers into appropriate arrays // until end of file reached

while ( !instream.eof() )
{
   instream >> temp;
   if (temp > 0)
   {
      positive[i] = temp;
   }
   else if (temp < 0)
   {
      negative[i] = temp;
   }
}
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 2

Expert Comment

by:goltor
ID: 2662653
sorry, you'll have to increment i at the end of the while loop as well.
0
 
LVL 1

Expert Comment

by:sunj
ID: 2662700
goltor: i guess even increment 'i' won't work. we need two variables here, one for each array.
0
 

Author Comment

by:katdude
ID: 2662735
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....
0
 
LVL 8

Expert Comment

by:stochastic
ID: 2663516
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
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question