displaying arrays

This is a discussion on displaying arrays within the C++ Programming forums, part of the General Programming Boards category; i made a post similar to this but have updated my code and it is working much more functionally. so ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    58

    displaying arrays

    i made a post similar to this but have updated my code and it is working much more functionally. so the idea is to read from the data7.txt file, store the info into the arrays in the buildArrays function, and display them in the printArrays function. there's more to it but i'll get there when i get there. right now, i can sucessfully display the first row of numbers from the data file, but that's it, it's stopping there. i'm unsure of how to get the program to keep displaying all the info until everything is displayed. i'm using "i" so that ideally the array will start at i=0, then go to 1=1, 1=2, etc, until it's run through all the data. i hope that makes sense. here is what i have so far. the sortArrays function is there but I haven't gotten into working on that yet, so it's just there, not doing anything at the moment, so ignore that. (unless you want to help, heh) thanks.
    Code:
    #include <iomanip>
    #include <iostream>
    #include <fstream>
    #include <ctype.h>
    
    using namespace std;
    
    const int LENGTH = 30;
    
    int family[LENGTH];
    
    int item[LENGTH];
    
    int quantity[LENGTH];
    
    double price[LENGTH];
    
    int buildArrays (int family[], int item[], int quantity[], double price[])
    {
    double num;
    
    ifstream inFile;
    
    inFile.open( "data7.txt" );
    
    if( inFile.fail() )
    {
    	cout << "text failed to open";
    	exit( -1 );
    };
    int i=0;
    while (inFile)
    {
    
    inFile>>num;
    	family[i]=num;
    	inFile>>num;
    	item[i]=num;
    	inFile>>num;
    	quantity[i]=num;
    	inFile>>num;
    	price[i]=num;
    	i++;
    	}
    	
    	return (i);
    	
    }	 
    void printArrays (int family[], int item[], int quantity [], double price[])
    {
    	int i=0;
    	cout<<family[i]<<"\n"<<item[i]<<"\n"<<quantity[i]<<"\n"<<price[i];
    	i++;
    }	 
    void sortArrays (int family [], int item[], int quantity [], double price[])
    {
    }
    
    
    
    int main()
    {
         buildArrays(family, item, quantity, price);
         printArrays(family, item, quantity, price);
    
    
    	
    	
    	
    	
      
    return 0;
    }

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    You probably want your printArrays function to have a loop in it. Currently you are only ever going to print the first set of data by calling that function.

    This:
    Code:
    while (inFile)
    {
        inFile>>num;
        family[i]=num;
        inFile>>num;
        item[i]=num;
        inFile>>num;
        quantity[i]=num;
        inFile>>num;
        price[i]=num;
        i++;
    }
    Suffers from a common problem in that the status of the stream is not set to EOF until after an attempt to read past the end of file is performed. That is, after you've processed the last row of data from the file, the stream still indicates that everything is good even though you are at the end of the file physically. Once you enter the loop and try to read then the stream indicates end-of-file but you're already in the loop at that point and processing data. This typically results in an extra row of bad data in your array. You also want to make sure that this loop does not store more than what your arrays can handle by making sure i stays below LENGTH. A better way to do this would be:
    Code:
    while ( i < LENGTH && inFile >> family[i] >> item[i] >> quantity[i] >> price[i] ) ++i;


    Code:
    #include <ctype.h>
    There's a newer version of this header to use in C++:
    Code:
    #include <cctype>
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    Quote Originally Posted by joeman View Post
    i made a post similar to this but have updated my code and it is working much more functionally. so the idea is to read from the data7.txt file, store the info into the arrays in the buildArrays function, and display them in the printArrays function. there's more to it but i'll get there when i get there. right now, i can sucessfully display the first row of numbers from the data file, but that's it, it's stopping there. i'm unsure of how to get the program to keep displaying all the info until everything is displayed. i'm using "i" so that ideally the array will start at i=0, then go to 1=1, 1=2, etc, until it's run through all the data. i hope that makes sense. here is what i have so far. the sortArrays function is there but I haven't gotten into working on that yet, so it's just there, not doing anything at the moment, so ignore that. (unless you want to help, heh) thanks.
    Code:
    #include <iomanip>
    #include <iostream>
    #include <fstream>
    #include <ctype.h>
    
    using namespace std;
    
    const int LENGTH = 30;
    
    int family[LENGTH];
    
    int item[LENGTH];
    
    int quantity[LENGTH];
    
    double price[LENGTH];
    
    int buildArrays (int family[], int item[], int quantity[], double price[])
    {
    double num;
    
    ifstream inFile;
    
    inFile.open( "data7.txt" );
    
    if( inFile.fail() )
    {
        cout << "text failed to open";
        exit( -1 );
    };
    int i=0;
    while (inFile)
    {
    
    inFile>>num;
        family[i]=num;
        inFile>>num;
        item[i]=num;
        inFile>>num;
        quantity[i]=num;
        inFile>>num;
        price[i]=num;
        i++;
        }
        
        return (i);
        
    }     
    void printArrays (int family[], int item[], int quantity [], double price[])
    {
        int i=0;
        cout<<family[i]<<"\n"<<item[i]<<"\n"<<quantity[i]<<"\n"<<price[i];
        i++;
    }     
    void sortArrays (int family [], int item[], int quantity [], double price[])
    {
    }
    
    
    
    int main()
    {
         buildArrays(family, item, quantity, price);
         printArrays(family, item, quantity, price);
    
    
        
        
        
        
      
    return 0;
    }
    Wow. There are a number of issues with the code.

    First of all, you need to be mindful of unintended type conversions. For example, when you read a value from a file as a 'double' and then store it as an 'int' there is a possible loss of information. So use the correct type, and pay attention where conversions occur.

    Next, there is the issue of the underlying logic of the program. You need to store the number of items that were read into the array somewhere, and right now you are just discarding that value. Why? After all, how are you to meter the loop-control mechanisms without it? So store it and pass it as a parameter to your functions (later, think of using a structure or class to pass the array info around...much more modular). Also, don't forget to check those bounds. In your current code, for example, items are added to the array unchecked. A Very Bad Thing®.

    Now let's talk about scope. When you declare a variable inside a function, it is created and destroyed each and every time that the function is called. If you add the "static" qualifier or declare the variable outside of any function or class then it become a "global" variable which exists for the entire duration of the program (static variables in functions are not "created" until they are first invoked, however). So inside your print function maybe just set up a loop on 'i' (using the array length value you stored earlier).

    Eventually, you will need to improve your error handling, too. For instance, when you read a value from a file, it's a given that things can and do go wrong. The file could be corrupt or incorrectly edited. A disk error could occur. You have to check for these things.

    Finally, the indentation you are using is completely unacceptable. Each level of scope should be matched with a tab sequence. Always.

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    58
    thanks for the help, i've got it working. i don't know how i missed not putting a loop in...

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    1
    hey, did you figure out the void sortArrays part? I got it up to displaying the void printArrays. I just cant get them in ascending order. I been working on this crap for a good 7 or 8 hours. This is what I have for that part so far.






    PHP Code:
    void sortAr (int family[], int item [], int quantity [], double price[], double numSales)
    {

    int i,j,min,minat;

    for (
    i=0i<6i++)
     {
              
    minat=i;
              
    min=family[i];
              
              for (
    j=i+1j<5j++)
              {
                  if(
    min>family[j])
                  {
                   
    minat=j;
                  
    min=family[j];
                  }
               }
               
    int temp =  family[i];
               
    family[i]=family[minat];
               
    family[minat]=temp;
               
               
    temp item[i];
               
    item[i]= item[minat];
               
    item[minat]=temp;
               
               
    temp quantity [i];
               
    quantity[i] = quantity [minat];
               
    quantity [minat] = temp;
               
               
    temp price[i];
               
    price[i]=price[minat];
               
    price[minat]=temp;
               
               





    im sure we could help each other out.

    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  2. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 09:47 PM
  3. Displaying Arrays
    By mr_spanky202 in forum C Programming
    Replies: 3
    Last Post: 04-07-2003, 02:22 PM
  4. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 08:24 PM
  5. displaying data from a structure of arrays
    By Unregistered in forum C Programming
    Replies: 8
    Last Post: 03-14-2002, 11:35 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21