printing arrays, reading from text file

This is a discussion on printing arrays, reading from text file within the C++ Programming forums, part of the General Programming Boards category; i'm trying to print arrays after building them from a text file. all of the rows of the text files ...

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

    printing arrays, reading from text file

    i'm trying to print arrays after building them from a text file. all of the rows of the text files are being displayed (ugly display at the moment, i know, i'll fix that) but then i get a bunch of rows of 0. i assume that's because of i set i<30 in the for loop in the printarrays function. but i was assigned to do that, i believe. so is there a way to make only what's in the file display without having to count up how many rows there are, and then setting i<less than that or whatever. so in this case, there are only 15 rows of data, so i only want to display those 15 rows, not 15 rows and then 15 rows of 0.
    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++;
    	
    	}
    	inFile.close();
    	return (i);
    	
    }	 
    void printArrays (int family[], int item[], int quantity [], double price[])
    {
    	ifstream inFile;
    
    	cout<<"Annual Garage Sale";
    	cout<<"\nFamily    Item Id #    Sale Price    Quantity Sold    Sale Amount";
    	cout<<"\n-------------------------------------------------------------------";
    	int i;
    	for(i=0;i<30;i++)
    	{
    		family[i];
    		item[i];
    		price[i];
    		quantity[i];
    
    		i++;
    	
    	
    		cout<<"\n"<<family[i]<<"     "<<item[i]<<"    "<<price[i]<<"    "<<quantity[i]<<"     ";
    	};
    	
    }	 
    
    
    
    
    
    int main()
    {
    	buildArrays(family, item, quantity, price);
    	printArrays(family, item, quantity, price);
    
    
    	
    	
    	
    	
      
    return 0;
    }
    this is what is in the text file, i need all of it to display, and only that

    Code:
    3 12345 1 15.95
    2 67800 3 50.00
    4 32145 2 4.50
    6 98765 1 75.00
    1 44496 2 11.30
    0 67356 4 3.50
    5 54862 1 52.50
    4 14541 1 4.25
    2 64488 2 1.00
    0 34945 3 7.40
    6 74322 1 6.50
    1 39240 10 0.75
    3 52525 15 1.00
    0 60002 1 12.00
    5 31579 2 2.50
    Last edited by joeman; 03-24-2010 at 12:25 PM.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    Code:
    	for(i=0;i<30;i++)
    	{
    		family[i];
    		item[i];
    		price[i];
    		quantity[i];
    
    		i++;
    	
    	
    		cout<<"\n"<<family[i]<<"     "<<item[i]<<"    "<<price[i]<<"    "<<quantity[i]<<"     ";
    	};
    You are incrementing i twice every time through the loop.
    "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
    Registered User
    Join Date
    Feb 2010
    Posts
    58
    thanks, i fixed that, but i'm still having a problem with it printing out an extra 15 rows of data that aren't in the text file.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you were assigned to print 30 rows, then you're going to have to print 30 rows.

    If you were assigned to only print the rows with data, then you'll have to keep that i from your read loop, and pass it to your print function so that it can use that as the stopping point to print.

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    58
    how would i pass a variable from one function to another?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by joeman View Post
    how would i pass a variable from one function to another?
    I don't know. How did you get family from one function to another? Or item? Or quantity? Or price?

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    Your buildArrays returns a value, the number of rows of data processed. Presumably you'd then pass that value to the printArrays function to control how many rows of data to print instead of always assuming 30 rows.

    Your buildArrays function also still has the issue of relying on the state of the stream to control the loop rather than the state of the read operations. I mentioned this in your other post and suggested an alternate way of reading data.
    "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

  8. #8
    Registered User
    Join Date
    Mar 2010
    Location
    Norway
    Posts
    25
    >>pass variable from one function to another

    Code:
    void Function1(int i)
    {
      cout << i;
    }
    
    int main()
    {
       int i = 1000;
       Function1(i);
    }
    Here you pass it from the function "main" into the "function1" and it prints out... You simply pass it as a arguement, whats that? Thats the thing within paranthesis of a function, ().

    If you write to a file with 30 lines, last 15 are empty, and then afterwards you print out the whole textfile, it will add those 15 empty lines.

    PLenty of ways to fix, assume char* s is a read word from the filefile:
    Code:
    char* s = new char;
    file >> s;
    if(strlen(s) > 1)//If it should be 1 or 0, I don't bother testing...
    {//print out the text of it
        cout << s;
    }
    else
    {
      //Do nothing, else is not needed, can be removed.
    }
    or you can add in a count of how many object/lines you just read into the file at the very top. Then read the number from the text file, then use ignore() to skip to the next line/row and do a loop as long as the loop-number is less or equal to the number you just read, then print out data.

    But, your 30 lines of outprint lies here:
    for(i=0;i<30;i++) // If the last 15 are empty, you print out empty lines, change it to 15?
    {

    Also: as hkmp5... said, you increment the int variable i twice, which you fixed, but which of the "i++" did you delete?
    If you deleted the bottom one, the one inside the loop, then it is fine, else it is not fine, because you will then skip [0] when you try to print out the arrays.
    Last edited by ManyTimes; 03-24-2010 at 01:41 PM.

  9. #9
    Registered User
    Join Date
    Feb 2010
    Posts
    58
    thanks a lot for the help man, much appreciated

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading a text file into multiple nodes
    By e2076w in forum C Programming
    Replies: 1
    Last Post: 10-14-2009, 09:21 PM
  2. Advice reading lines from a text file.
    By Fujitaka in forum C Programming
    Replies: 2
    Last Post: 08-11-2009, 09:43 PM
  3. Reading and printing a file
    By T1m in forum C Programming
    Replies: 1
    Last Post: 01-08-2009, 12:29 PM
  4. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  5. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM

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