read txt file / print / records / arrays

Hi C++ folks. I'm stuck here trying to save data from a txt file in a 2D array in order to use this in a bigger program. If I read the txt file without using records and array is printing exactly what is in the file. But now the output is compose of only numbers. I'll post the txt file on a comment.

Thanks in advance!!

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
#include <iostream>
#include <fstream>
#include <string>

using namespace std ;

struct myRecord
{
	int OrdNum ;
	int day ;
	int month ;
	int year ;

	string color ;
	string garbage1 ;
	string garbage2 ;
	string garbage3 ; 
	string garbage4 ; 
	string garbage5 ;

	double SqMt ;
	double price ;

	friend ostream & operator<< ( ostream & output, myRecord & v)
	{
		cout	<< v.garbage1 << "     "	<< v.OrdNum << endl 
				<< v.garbage2 << "      "	<< v.day << " " << v.month << " " << v.year << endl
				<< v.garbage3 << "     "	<< v.color << endl
				<< v.garbage4 << "  "		<< v.SqMt << endl
				<< v.garbage5 << "     "	<< v.price  << endl << endl ;

		return output;
	}
};

int main ()
{
	
	myRecord ordArray[1][10];

	int indexX = 0 ;
	int indexY = 0 ;
	
	ifstream orderFile ;
	orderFile.open ("F:/School/C++/00 AA MagicCarpetCompany/orders.txt") ;
	
	while(!orderFile.eof())
	{
	orderFile	>> ordArray[indexX][indexY].garbage1 >> ordArray[indexX][indexY].OrdNum 
				>> ordArray[indexX][indexY].garbage2 >> ordArray[indexX][indexY].day >> ordArray[indexX][indexY].month >> ordArray[indexX][indexY].year 
				>> ordArray[indexX][indexY].garbage3 >> ordArray[indexX][indexY].color 
				>> ordArray[indexX][indexY].garbage4 >> ordArray[indexX][indexY].SqMt 
				>> ordArray[indexX][indexY].garbage5 >> ordArray[indexX][indexY].price ;

	indexY ++ ;

	cout << ordArray[indexX][indexY] ;

	}

	orderFile.close();

	system ( "pause" ) ;
	return 0 ;
}




The .txt file is this one:

Order: 1
Date: 17 3 2017
Color: Red
SqMeters: 12
Price: 1200

Order: 2
Date: 17 3 2017
Color: Blue
SqMeters: 15
Price: 2250

Order: 3
Date: 17 3 2017
Color: Red
SqMeters: 145
Price: 14500

Order: 4
Date: 17 3 2017
Color: Red
SqMeters: 65
Price: 6500

Order: 5
Date: 17 3 2017
Color: Green
SqMeters: 15
Price: 3000

Order: 6
Date: 17 3 2017
Color: Green
SqMeters: 153
Price: 30600

Order: 7
Date: 17 3 2017
Color: Red
SqMeters: 356
Price: 35600

Last edited on
I've just tried to use only records and its working. So the problem is somewhere on the arrays. I post the working code here.

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
#include <iostream>
#include <fstream>
#include <string>
#include <windows.h> 
#include <conio.h>
#include <limits>
#include <ctime>

using namespace std ;

struct myRecord
{
	int OrdNum ;
	int day ;
	int month ;
	int year ;

	string color ;
	string garbage1 ;
	string garbage2 ;
	string garbage3 ; 
	string garbage4 ; 
	string garbage5 ;

	double SqMt ;
	double price ;

	friend ostream & operator<< ( ostream & output, myRecord & v)
	{
		cout	<< v.garbage1 << "     "	<< v.OrdNum << endl 
				<< v.garbage2 << "      "	<< v.day << " " << v.month << " " << v.year << endl
				<< v.garbage3 << "     "	<< v.color << endl
				<< v.garbage4 << "  "		<< v.SqMt << endl
				<< v.garbage5 << "     "	<< v.price  << endl << endl ;

		return output;
	}


};


int main ()
{

	struct myRecord REC ;
	
	/*myRecord ordArray[1][10];

	int indexX = 0 ;
	int indexY = 0 ;*/
	
	ifstream orderFile ;
	orderFile.open ("F:/School/C++/00 AA MagicCarpetCompany/orders.txt") ;
	
	orderFile	>> REC.garbage1 >> REC.OrdNum 
				>> REC.garbage2 >> REC.day >> REC.month >> REC.year 
				>> REC.garbage3 >> REC.color 
				>> REC.garbage4 >> REC.SqMt 
				>> REC.garbage5 >> REC.price ;


	cout << REC ;


	orderFile.close();

	system ( "pause" ) ;
	return 0 ;
}
The immediate problem is in these lines:
55
56
57
	indexY ++ ;

	cout << ordArray[indexX][indexY] ;

indexY is incremented to the next array position before the cout statement, hence the garbage data from the uninitialised array content. Move the increment after the cout.

Other problems, at line 26 in the overloaded << operator function, cout << should be output <<. It's also good practice to make the second parameter const, since it will not be changed by the function.

The 2-D array doesn't seem necessary, it could be a simple 1-D array. (Well, in this example no array at all is needed, but I expect it will be used later).

And, it is not a good idea to loop on eof(), it can give rise to errors, such as repeating the last line of the file twice. Instead, put the input operation inside the loop condition. Also check that the array size has not been exceeded.

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
#include <iostream>
#include <fstream>
#include <string>

using namespace std ;

struct myRecord
{
    int OrdNum ;
    int day ;
    int month ;
    int year ;

    string color ;
    string garbage1 ;
    string garbage2 ;
    string garbage3 ; 
    string garbage4 ; 
    string garbage5 ;

    double SqMt ;
    double price ;

    friend ostream & operator<< ( ostream & output, const myRecord & v)
    {
        output  << v.garbage1 << "     "    << v.OrdNum << endl 
                << v.garbage2 << "      "   << v.day << " " << v.month << " " << v.year << endl
                << v.garbage3 << "     "    << v.color << endl
                << v.garbage4 << "  "       << v.SqMt << endl
                << v.garbage5 << "     "    << v.price  << endl << endl ;

        return output;
    }
};

int main ()
{
    const int arraysize = 10;
    myRecord ordArray[arraysize];

    int indexY = 0 ;
    
    ifstream orderFile ;
    orderFile.open ("orders.txt") ;
    
    while ( (indexY < arraysize) &&
          orderFile >> ordArray[indexY].garbage1 >> ordArray[indexY].OrdNum 
                    >> ordArray[indexY].garbage2 >> ordArray[indexY].day >> ordArray[indexY].month >> ordArray[indexY].year 
                    >> ordArray[indexY].garbage3 >> ordArray[indexY].color 
                    >> ordArray[indexY].garbage4 >> ordArray[indexY].SqMt 
                    >> ordArray[indexY].garbage5 >> ordArray[indexY].price )
    {
//        cout << ordArray[indexY] ; // output deferred until later 
        indexY ++ ;
    }

    orderFile.close();
    
    // Now output array contents
    for (int i=0; i<indexY; ++i)
    {
        cout << ordArray[i];
    }

    system ( "pause" ) ;
    return 0 ;
}

Last edited on
Thank you so much Chervil!

I was just wondering why was printing twice the last order.
The main mistake was something really basic actually, what a shame. :D

About the array, yeah that's right, I need to put a 2D array later.

I'll get a little fix and finally will be working!!
Thanks again!!
Glad that's working ok now.

A couple of other comments. You could use setw() for formatting in the output function, it's better than having to customise the " " blank string spacers each time. As well, to make the input operation cleaner, you might overload the input operator >> too. Then, even if you use a 2-D array, the change to the code doesn't involve many separate places. My previous code now looks like this (still with 1-D array here).

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
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std ;

struct myRecord
{
    int OrdNum ;
    int day ;
    int month ;
    int year ;

    string color ;
    string garbage1 ;
    string garbage2 ;
    string garbage3 ; 
    string garbage4 ; 
    string garbage5 ;

    double SqMt ;
    double price ;

    friend istream & operator>> ( istream & input, myRecord & v)
    {
        input >> v.garbage1 >> v.OrdNum 
              >> v.garbage2 >> v.day >> v.month >> v.year 
              >> v.garbage3 >> v.color 
              >> v.garbage4 >> v.SqMt 
              >> v.garbage5 >> v.price;
                    
        return input;
    }
    
    friend ostream & operator<< ( ostream & output, const myRecord & v)
    {
        output  << left 
                << setw(11) << v.garbage1 << v.OrdNum << '\n' 
                << setw(11) << v.garbage2 << v.day << " " << v.month << " " << v.year << '\n'
                << setw(11) << v.garbage3 << v.color << '\n'
                << setw(11) << v.garbage4 << v.SqMt << '\n'
                << setw(11) << v.garbage5 << v.price  << "\n\n";

        return output;
    }
};

int main ()
{
    const int arraysize = 10;
    myRecord ordArray[arraysize];

    int indexY = 0 ;
    
    ifstream orderFile ;
    orderFile.open ("orders.txt") ;
    
    while ( (indexY < arraysize) && orderFile >> ordArray[indexY])
    {
        indexY ++ ;
    }

    orderFile.close();
    
    for (int i=0; i<indexY; ++i)
    {
        cout << ordArray[i];
    }

    system ( "pause" ) ;
    return 0 ;
}
Topic archived. No new replies allowed.