调整C++程序proDayoff数组赋值逻辑后输出异常求助
C++数组赋值逻辑错误排查
我是C++初学者,编写程序用于判断数组dayoffType每行的有效元素数量是否等于用户指定的dayoffLim值。初始代码输入8 31时,输出为0,0,0,3,4,5,0,符合预期。为得到3,4,5,0,0,0,0的输出,我修改了proDayoff数组的赋值索引逻辑,结果输入8 31时输出变为3,4,6,0,0,0,0,与预期不符,恳请帮忙排查问题。
初始代码
int dayoffLim = 0; cin >> dayoffLim; int dayoffType[7][10] = { {1, 2, 8, 9, 15, 16, 22, 23, 29, 30}, {2, 3, 9, 10, 16, 17, 23, 24, 30, 31}, {3, 4, 10, 11, 17, 18, 24, 25, 31, 0}, {4, 5, 11, 12, 18, 19, 25, 26, 0, 0}, {5, 6, 12, 13, 19, 20, 26, 27, 0, 0}, {6, 7, 13, 14, 20, 21, 27, 28, 0, 0}, {1, 7, 8, 14, 15, 21, 23, 28, 29, 0} }; if(days == 28){ int z = -1; for(int i=0; i<7; i++){ int isDayoff = 0; for(int j=0; j<10; j++){ if(dayoffType[i][j] <=28 && dayoffType[i][j] != 0){ isDayoff ++; } } if(isDayoff == dayoffLim){ proDayoff[i] = i; } } }else if(days == 29){ int z = -1; for(int i=0; i<7; i++){ int isDayoff = 0; for(int j=0; j<10; j++){ if(dayoffType[i][j] <=29 && dayoffType[i][j] != 0){ isDayoff ++; } } if(isDayoff == dayoffLim){ proDayoff[i] = i; } } }else if(days == 30){ int z = -1; for(int i=0; i<7; i++){ int isDayoff = 0; for(int j=0; j<10; j++){ if(dayoffType[i][j] <=30 && dayoffType[i][j] != 0){ isDayoff ++; } } if(isDayoff == dayoffLim){ proDayoff[i] = i; } } }else if(days == 31){ int z = -1; for(int i=0; i<7; i++){ int isDayoff = 0; for(int j=0; j<10; j++){ if(dayoffType[i][j] <=31 && dayoffType[i][j] != 0){ isDayoff ++; } } if(isDayoff == dayoffLim){ proDayoff[i] = i; } } } for(int i=0; i<7; i++){ cout << proDayoff[i] << " "; }
修改后的代码
int dayoffLim = 0, days = 0; cin >> dayoffLim >> days; int dayoffType[7][10] = { {1, 2, 8, 9, 15, 16, 22, 23, 29, 30}, {2, 3, 9, 10, 16, 17, 23, 24, 30, 31}, {3, 4, 10, 11, 17, 18, 24, 25, 31, 0}, {4, 5, 11, 12, 18, 19, 25, 26, 0, 0}, {5, 6, 12, 13, 19, 20, 26, 27, 0, 0}, {6, 7, 13, 14, 20, 21, 27, 28, 0, 0}, {1, 7, 8, 14, 15, 21, 23, 28, 29, 0} }; if(days == 28){ int z = -1; for(int i=0; i<7; i++){ int isDayoff = 0; for(int j=0; j<10; j++){ if(dayoffType[i][j] <=28 && dayoffType[i][j] != 0){ isDayoff ++; } } //Where I adjust if(isDayoff == dayoffLim){ z++; } proDayoff[z] = i; } }else if(days == 29){ int z = -1; for(int i=0; i<7; i++){ int isDayoff = 0; for(int j=0; j<10; j++){ if(dayoffType[i][j] <=29 && dayoffType[i][j] != 0){ isDayoff ++; } } //Where I adjust if(isDayoff == dayoffLim){ z++; } proDayoff[z] = i; } }else if(days == 30){ int z = -1; for(int i=0; i<7; i++){ int isDayoff = 0; for(int j=0; j<10; j++){ if(dayoffType[i][j] <=30 && dayoffType[i][j] != 0){ isDayoff ++; } } //Where I adjust if(isDayoff == dayoffLim){ z++; } proDayoff[z] = i; } }else if(days == 31){ int z = -1; for(int i=0; i<7; i++){ int isDayoff = 0; for(int j=0; j<10; j++){ if(dayoffType[i][j] <=31 && dayoffType[i][j] != 0){ isDayoff ++; } } //Where I adjust if(isDayoff == dayoffLim){ z++; } proDayoff[z] = i; } }
问题分析与解决
错误根源
修改后的代码中,无论当前行是否满足isDayoff == dayoffLim的条件,都会执行proDayoff[z] = i。以输入8 31为例:
- 遍历到第3行(i=3)时,有效元素数为8,符合条件,z从-1递增到0,
proDayoff[0] = 3,正确。 - 遍历到第4行(i=4)时,有效元素数为8,符合条件,z递增到1,
proDayoff[1] =4,正确。 - 遍历到第5行(i=5)时,有效元素数为8,符合条件,z递增到2,
proDayoff[2] =5,此时结果正确。 - 遍历到第6行(i=6)时,有效元素数为9,不符合条件,z保持2不变,但代码依然执行
proDayoff[2] =6,直接覆盖了之前的5,导致最终输出的第三个值变成6。
修复方案
将赋值操作移入if条件块内,仅当行符合条件时才更新proDayoff:
if(isDayoff == dayoffLim){ z++; proDayoff[z] = i; }
同时,建议初始化proDayoff数组为0,避免出现未定义的垃圾值:
int proDayoff[7] = {0};
修复后,输入8 31时,只有符合条件的第3、4、5行会被写入proDayoff的前三个位置,其余位置保持0,输出即为预期的3,4,5,0,0,0,0。
内容的提问来源于stack exchange,提问作者Chil
相关产品推荐
相关产品推荐

