Reading from a file into an array

Hey guys, so i'm working on a program that has to read a shopping list from a file and then after I need to add tax to it. I've managed to get the first item and price out from the file, but the whole other list doesn't get extracted,i'm assuming I need to use two arrays to solve my issue but when I went and did them nothing would print out and my program would terminate. If i delete the arrays my heading function prints and also the item and price. Any ideas on why it doesnt print anything when I use the arrays? Ps this is my first time posting so if the format is wrong im very sorry.

This is how the file looks like:
Student's Shopping List
apple 0.89
book 135.00
eraser 0.59
flashdrive 7.99
folder 1.29
highlighter 0.98
laptop 500.00
mouse 14.98
notebook 5.00
paper 2.29
pencil 0.35
pen 1.98
water 1.00

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
  #include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;

//function prototypes
void headings();
float tax();




int main() {
	const int SIZE=13;
	string item[SIZE],dummy;
	float cost[SIZE];

	ifstream infile("studentList.txt");

	getline(infile,dummy); //reads first line, I use this since I dont need the first line in the file

	while (!infile.eof()) 
	{
		for(int count =0; count < SIZE; count ++)
		{
		infile >> item[count] >> cost[count]; // my attempt at making the array
		}
		if (infile.eof()) break;

		headings(); //prints out headings

		cout << item <<" " << cost << " " << endl;
		return 0;
	}


	return 0;
}

float tax() // dont worry about tax, i didnt really work on it since i couldnt get the numbers 
{
const int TAX_RATE= 0.09;


}

void headings()
{
	cout << "Item" << setw(16) << " Cost" << setw(10) <<"Tax" << setw(9)<<"Total" << setw(12) << endl; // these are the headings
}
Any ideas on why it doesnt print anything when I use the arrays?

Well, you have a nested loop:
1
2
while (something)
    for (something)

That is overdone. Use either a while loop or a for loop but not both - at least when reading from the file.


In any case using eof() as a loop condition is not a good idea - that is to say - don't do it. It can cause unexpected problems and is best avoided. Instead, put the input operation such as iinfile >> item[count] >> cost[count] inside the loop condition.


Here I also checked the array index in the loop condition, to avoid going over the boundary of the array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

//function prototypes
void headings();
double tax();

int main()
{
    const int SIZE = 30; // make the array larger than expected to be necessary
                         // because we don't know what will be in the file.
    string item[SIZE];
    double cost[SIZE];
    
    ifstream infile("studentList.txt");
    if (!infile)
    {
        cout << "Could not open input file\n";
        return 1;
    }

    // Ignore first line
    string dummy;
    getline(infile,dummy); 

    int count = 0;
    
    while ( count < SIZE &&  infile >> item[count] >> cost[count] )
    {
        count ++;
    }
    
    
    headings(); //prints out headings

    // print out whatever is stored in the arrays
    for (int i=0; i<count; ++i)
    {
        cout << item[i] << " " << cost[i] << endl;
    }


}

double tax() // dont worry about tax, i didnt really work on it since i couldnt get the numbers 
{
    const double TAX_RATE= 0.09;
    return 0.0;
}

void headings()
{
    // these are the headings
    cout << setw(16) << "Item" 
         << setw(10) << " Cost" 
         << setw(9)  << "Tax" 
         << setw(12) << "Total" 
         << endl;      
}

//void headings()
//{
//    // these are the headings
//    cout << "Item" 
//         << setw(16) << " Cost" 
//         << setw(10) << "Tax" 
//         << setw(9)  << "Total" 
//         << setw(12) << endl;      
//}


Note. Prefer to use double rather than float. setw() goes before each printed item, not after it.
Last edited on
Thank you chervil it worked! Also thanks for the tips!!
Registered users can post here. Sign in or register to post.