函数自动循环?
Function automatically looping?
好的,所以这个函数应该从用户那里获取结果,然后给他们一个选项来输入更多结果或在完成后返回主菜单main((。
一切正常。 唯一的问题是在切换前的最后一个scanf
语句。
而不是等待用户输入userChoice
,它会自动重新启动该功能。它只应该在userChoice
是"a"或"A"时才这样做,但它似乎也完全忽略了 scanf 语句和开关。我做错了什么?向下滚动以查看 im 引用的行。
void inputResults(int gameNumber, int i, int j)
{
int Results[ROWS][COLS];
char userChoice = ' ';
if (j >= COLS)
{
j = 0;
i = i + 1;
}
if (gameNumber > ROWS)
{
printf("nJk, Max number of games reachedn");
main();
}
gameNumber = gameNumber + 1;
printf("nHow many points did the home team score in game %d?n", gameNumber);
scanf_s("%d", &Results[i][j]);
j = j + 1;
printf("nHow many points did the opponents score in game %d?n", gameNumber);
scanf_s("%d", &Results[i][j]);
printf("nHome: %d Opponents: %dn", Results[i][j-1], Results[i][j]);
printf("nA) Enter results for game %dnB) Main menu n", (gameNumber + 1));
*************************PROBLEM***********************************
scanf_s("%c", &userChoice); /*it ignores THIS it just loops the
function after it prints the above*/
switch (userChoice) //it doesnt even get to this.
{
case 'A':
inputResults(gameNumber, i, j);
case 'a':
inputResults(gameNumber, i, j);
case 'b':
main();
case 'B':
main();
default:
printf("nThats still not a valid choice duden");
}
}
如果您想知道,传递给 gameNumber、i 和 j 的值都是 0。它们是从主函数传递的
当你读到一个带有类似scanf_s
的字符时
char ch;
scanf_s("%c", &ch, 1);
并输入例如字母"A"并按回车键,"A"以ch
结尾,但按回车键的""仍保留在标准输入中。在下次调用scanf_s()
时,此""将被读取并存储在ch
中,而不会给您任何输入的机会。
用
scanf_s("%c[^n]", &ch, 1);
相反,它将读取并丢弃仍在stdin
中的所有额外字符,直到下一个换行符。
相关文章:
- C++中的高效循环缓冲区,它将被传递给C样式数组函数参数
- 循环中的随机函数
- 当调用switch语句中的函数时(即使函数不包含循环),似乎是永不结束的循环的问题
- 未在函数C++中执行的循环
- 如何在没有可变参数函数的情况下循环函数的参数
- 循环函数中的并行数组
- 如何循环函数,直到新的 mqtt 消息到达
- 循环C++ R 函数
- 为什么我的 while 循环函数只返回"1"?
- Pthread 循环函数永远不会被调用
- 循环函数调用未正确循环!(C++)
- 对于循环函数参数在C++中的奇数
- 具有双重调度的C++多态循环函数
- 循环函数给出"return’ with a value, in function returning void"误差
- 如何用循环函数求和
- 如何使c++中的for each循环函数与自定义类一起工作
- c++分割故障及循环函数
- C++ |需要帮助从 for 循环函数转换 - >递归函数
- 为什么要在 levelDB 的缓存中使用 while 循环(函数 Resize)?
- odeint中的循环函数