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