您好我需要在数组中打印出一些元素,但只有那些实际上已分配了值。到目前为止,我有这个:
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
}
max = 40
但是,我的输出将是:
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
这些0是未分配的值,但我只想打印指定的值 我怎么能这样做?
由于
答案 0 :(得分:1)
只需修改循环条件,一旦遇到第一个零,循环就会停止:
for(int h = 0; (h < max) && (v[h].getDay() > 0); h++)
答案 1 :(得分:1)
您可以添加if-else
子句并测试某个值是否为0,例如v3[h].getPrice()
或v[h].getDay()
,具体取决于您是否希望指定的天数为零值。
类似的东西:
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
}
}