我在函数中有一个For循环,如下所示:
int fnSearch(int arnSalaries [10][2], int nSalary, char cFound)
{
int nRow, nCol;
printf("Please enter the Salary to find the Employee ID : ");
scanf("%d", &nSalary);
for(nRow = 0; nRow < 10; nRow++)
{
if(nSalary == arnSalaries[nRow][1])
{
printf("\%d found - Employee ID matching that salary is: %d\n",
nSalary, arnSalaries[nRow][0]);
cFound = 'Y';
//nRow = 10; /* This is to break out of the loop */
}
}
if(cFound == 'N')
{
printf("Sorry, that salary does not match an employee\n");
}
return cFound;
}
当我输入“10000”等薪水时,输出如下:
10000 found - Employee ID matching that salary is: 21
10000 found - Employee ID matching that salary is: 23
如何更改代码,使其显示如下内容:
10000 found - Employee ID(s) matching that salary is/are: 21, 23
编辑: 我不是在寻找任何要编写的代码 - 只是在正确的方向上暗示我应该考虑解决这个问题
答案 0 :(得分:5)
在循环之前打印除id号之外的所有内容。 在循环内部,只打印id号。 在循环之后,打印换行符。
答案 1 :(得分:1)
希望这可以帮到你:
int fnSearch(int arnSalaries [10][2], int nSalary, char cFound)
{
int nRow, nCol, i = 0;
int foundIndex[] = int[10]; //10 or number of rows of arnSalaries
printf("Please enter the Salary to find the Employee ID : ");
scanf("%d", &nSalary);
for(nRow = 0; nRow < 10; nRow++)
{
if(nSalary == arnSalaries[nRow][1])
{
cFound = 'Y';
foundIndex[i++] = nRow;
}
}
if(cFound == 'N')
printf("Sorry, that salary does not match an employee\n");
else
{
printf("\%d found - Employee ID matching that salary is/are :",nSalary);
for(i = 0;i<sizeof(foundIndex);i++)
printf("%d,",arnSalaries[foundIndex[i]][0])
}
return cFound;
}
请更正一些语法错误。