2013-12-19 19 views
0

我有一个for循环的函数,如下所示:输出2个值中的一个线路输入For循环 - Visual C

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

有几种方法。你有没有试图自己解决这个问题?这就是SO的首选,如果你碰到一堵砖墙,还会留下一个问题。 –

+0

将循环中另一个数组中的匹配结果保存起来,然后在循环结束后将这些值输出。 – OldProgrammer

+0

感谢您的评论 - 即时通讯新的C - 最近才开始,所以我不会很高级这种事情! –

回答

5

打印除循环前的id号码之外的所有内容。 在循环内部,只打印身份证号码。循环后,打印换行符 。

+0

啊!这实际上是一个很好的开箱思维!欢呼克里斯! –

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; 
} 

请纠正一些语法错误,如果有。