【发布时间】:2014-03-25 23:41:01
【问题描述】:
我得到了一个带有 2D 数组的不错的小函数。我想在第一个数组中搜索收入,然后使用相同的 int 在二维数组中找到正确的数字。
我已将我的问题限制在一行代码中。 if Else 语句在 for 循环内不起作用。我不知道为什么。任何没有用锤子敲击它的想法。
float get_total_income( float income)
{
char status[10];
float over_income = 30001;
float final_rate = .35;
float adjusted_income = 0;
int check = -1;
int x = 0;
float rate;
float array_income[] = { 6000.00, 9000.00, 15000.00, 21000.00, 25000.00, 30000.00 };
float rates[][6] = { { .028, .075, .096, .135, .155, .174 }, { 0.0, .052, .083, .122, .146, .163 },
{ .023, .072, .089, .131, .152, .172 }, { 0.0, .038, .074, .110, .138, .154 } };
printf("\n\t YOUR INCOME AFTER DEPENDANTS: %.2f", income);
while (check < 0)
{
printf("\n\n\nEnter your Status (S, MJ, MS, SH)");
gets_s(status);
if (status[0] == 'S' && status[1] == '\0')
{
single_total = single_total + 1;
check = 0;
}
else if (status[0] == 'M' && status[1] == 'J')
{
mj_total = mj_total + 1;
check = 1;
}
else if (status[0] == 'M' && status[1] == 'S')
{
ms_total = ms_total + 1;
check = 2;
}
else if (status[0] == 'S' && status[1] == 'H')
{
sh_total = sh_total + 1;
check = 3;
}
else
{
printf("\n\n INCORRECT STATUS. Enter (S, MJ, MS, or SH) \n\n");
}
}
if (income > over_income)
{
rate = final_rate;
}
else
{
for (x = 0; x<6; x++)
{
printf("num %i# \n", x);
if (income <= array_income[x])
{
rate = rates[check][x];
printf("\t\nworks, %i# in loop answer: %.3f\n",x, rate);
}
}//end for loop
}//end if
printf("\n\t YOUR TAX RATE: %.3f", rate);
return rate;
}
在这部分:
for (x = 0; x<6; x++)
{
printf("num %i# \n", x);
if (income <= array_income[x])
{
rate = rates[check][x];
printf("\t\nworks, %i# in loop answer: %.3f\n",x, rate);
}
不是停下来打印一次,而是打印整个表格。 出于某种原因,If 语句在这里没有权力,编译器会忽略它。
应该发生的是 如果收入 = 2000 并且
【问题讨论】:
-
over_income声明在哪里?它的价值是什么? -
等一下,我完全忘了添加实际输出。我的错。
-
另外,请准确说明您的期望以及会发生什么。 “不工作”不是很具体..
-
The if Else statement is not working inside the for loopfor 循环中没有其他内容。 -
int check = 0;不进入循环。gets_s需要大小。
标签: c++ c if-statement for-loop