Question

Hi I need to print out some elements in an array but only those have actually been assigned values. So far I have this:

for(int h = 0; h < max; h++)
{
    ofile <<  v[h].getDay() << '/' << v[h].getMonth() << '/' << v[h].getYear() << ", "
          << v2[h].getHour() << ':' << v2[h].getMinute() << ':' << v2[h].getSecond() << v2[h].getAMPM() << ", "
          << v3[h].getPrice() << ", " << v3[h].getVolume() << ", " << v3[h].getValue() << endl; ///outputs the data to an output file

}

Where max = 40

However, my output will be:

 10/10/2013, 4:57:27 PM, 5.81, 5000, 29050
 10/10/2013, 4:48:5 PM, 5.81, 62728, 364450
 10/10/2013, 4:10:33 PM, 0, 0, 0
 10/10/2013, 4:10:33 PM, 0, 0, 0
 10/10/2013, 4:10:33 PM, 0, 0, 0
 10/10/2013, 4:10:33 PM, 5.55, 451, 2620.31
 10/10/2013, 4:10:33 PM, 5.81, 5000, 29050
 10/10/2013, 4:10:33 PM, 5.81, 145, 842.45
 10/10/2013, 4:10:33 PM, 5.81, 9241, 53690.2
 10/10/2013, 4:10:33 PM, 5.81, 8759, 50889.8
 10/10/2013, 4:10:33 PM, 5.81, 1875, 10893.8
 10/10/2013, 4:10:33 PM, 5.81, 58, 336.98
 10/10/2013, 4:10:33 PM, 5.81, 1370, 7959.7
 10/10/2013, 4:10:33 PM, 5.81, 90000, 522900
 10/10/2013, 4:10:33 PM, 5.81, 638, 3706.78
 10/10/2013, 4:10:33 PM, 5.81, 4231, 24582.1
 10/10/2013, 4:10:33 PM, 5.81, 71191, 413620
 10/10/2013, 4:10:33 PM, 5.81, 21878, 127111
 10/10/2013, 4:10:33 PM, 5.81, 6760, 39275.6
 10/10/2013, 4:10:33 PM, 5.81, 21340, 123985
 10/10/2013, 4:10:33 PM, 5.81, 4000, 23240
 10/10/2013, 4:10:33 PM, 5.81, 4750, 27597.5
 0/0/0, 0:0:0, 0, 0, 0
 0/0/0, 0:0:0, 0, 0, 0
 0/0/0, 0:0:0, 0, 0, 0
 0/0/0, 0:0:0, 0, 0, 0
 0/0/0, 0:0:0, 0, 0, 0
 0/0/0, 0:0:0, 0, 0, 0
 0/0/0, 0:0:0, 0, 0, 0
 0/0/0, 0:0:0, 0, 0, 0
 0/0/0, 0:0:0, 0, 0, 0
 0/0/0, 0:0:0, 0, 0, 0
 0/0/0, 0:0:0, 0, 0, 0
 0/0/0, 0:0:0, 0, 0, 0
 0/0/0, 0:0:0, 0, 0, 0
 0/0/0, 0:0:0, 0, 0, 0
 0/0/0, 0:0:0, 0, 0, 0
 0/0/0, 0:0:0, 0, 0, 0
 0/0/0, 0:0:0, 0, 0, 0
 0/0/0, 0:0:0, 0, 0, 0

These 0's are unassigned values but I only want the assigned ones to be printed how can I do this?

Thanks

Was it helpful?

Solution

Just modify your loop condition so the loop will stop as soon as the first zero is encountered:

for(int h = 0; (h < max) && (v[h].getDay() > 0); h++)

OTHER TIPS

You could add anif-elseclause and test if some value is 0, for example v3[h].getPrice() or maybev[h].getDay()depending on if you want the assigned days with zero values or not..

Something like:

for(int h = 0; h < max; h++)
{
    if(v3[h].getPrice()==0){
        continue;
    } else {
    ofile <<  v[h].getDay() << '/' << v[h].getMonth() << '/' << v[h].getYear() << ", "
          << v2[h].getHour() << ':' << v2[h].getMinute() << ':' << v2[h].getSecond() << v2[h].getAMPM() << ", "
          << v3[h].getPrice() << ", " << v3[h].getVolume() << ", " << v3[h].getValue() << endl; ///outputs the data to an output file
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top