how to DON'T print empty array cells

Hi everyone! Is there any way to print out only the "full spaces" of the array?

Basically I have a txt file with some that looks like that:

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
Order:       163
Date:        19 3 2017
Color:       Red
SqMeters:    16.5
Price:       1650

Order:       164
Date:        19 3 2017
Color:       Blue
SqMeters:    16
Price:       2400

Order:       179
Date:        19 3 2017
Color:       Blue
SqMeters:    0.15
Price:       22.5

Order:       66
Date:        19 3 2017
Color:       Green
SqMeters:    21.5
Price:       4300

Order:       165
Date:        19 3 2017
Color:       Green
SqMeters:    165.15
Price:       33030

Order:       46
Date:        21 3 2017
Color:       Blue
SqMeters:    12.5
Price:       1875

Order:       12
Date:        21 3 2017
Color:       Red
SqMeters:    15
Price:       1500


Reading the txt file I create a 2D array (is mandatory for my assignment) like this one:

myRecordAll ordArrayAll[2][1000];

on the first column I put the orders from today, on the second one all of the other orders.
Everything is working perfectly but when it come to print the array there is a mistake. The problem is that I don't know how many "cells" of the array are full with data (order in this case) so I don't know how many cells to print.

At the same time both of the columns have different numbers of lines full of data. For example the position [1][14] is full with a order but the position [0][14] is empty.

So again my question is: is there any way to cout only the "cells" with data in it?

To print I use a simple loop.

1
2
3
4
5
for ( indexY = 0 ; indexY < 10 ; indexY++ ) // NotTodOrd
	{
		indexX = 1 ;
		cout << ordArrayAll[indexX][indexY] ;	
	}


and

1
2
3
4
5
for ( indexY = 0 ; indexY < 10 ; indexY++ ) // TodOrd
	{
		indexX = 0 ;
		cout << ordArrayAll[indexX][indexY] ;	
	}


Thanks in advance!!
Hello lapolis,

First you will need to change the "for" statements form 10 to 1000. Then use an "if" statement to check something in the array element to see if it is worth printing.

Just guessing here, but maybe something like:

1
2
3
4
5
6
7
for ( indexY = 0 ; indexY < 10 ; indexY++ ) // NotTodOrd
{
	indexX = 1 ;

        if (orderArrayAll[indexX][indexY].order > 0)
	    cout << ordArrayAll[indexX][indexY] ;	
}


Hope that helps,

Andy
Hello lapolis,

I tested this with a struct:

1
2
3
4
5
6
7
8
struct myRecordAll
{
	int order{ 0 };
	std::string date{ "" };
	std::string color{ "" };
	int sqMeters{ 0 };
	double price{ 0.0 };
};


As you can see I initialized every variable in the struct. When "ordArrayAll[2][1000]" was created every element in the array had a zero or an empty string. Which would allow the testing I suggested in my previous message. If you are using a class it would take a different approach to initialize the class before creating the array.

Hope that helps,

Andy
Actually I was trying with an "if" but wasn't working properly. Probably I did something wrong. I'll try again!

Thanks!!
Hello lapolis,

I have been playing with a struct and this is what I came up with:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
for (size_t indexY = 0; indexY < 1000; indexY++)
{
	int indexX = 0;

	if (ordArrayAll[indexX][indexY].order > 0)
	{
		std::cout << "Order: " << ordArrayAll[indexX][indexY].order << std::endl;
		std::cout << "Date: " << ordArrayAll[indexX][indexY].date << std::endl;
		std::cout << "Colour: " << ordArrayAll[indexX][indexY].color << std::endl;
		std::cout << "Square Meters: " << ordArrayAll[indexX][indexY].sqMeters << std::endl;
		std::cout << "Price: " << ordArrayAll[indexX][indexY].price << std::endl;
	}
	else
		break;
}


Hope this helps,

Andy
Topic archived. No new replies allowed.