Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 正确清除键盘缓冲区_C - Fatal编程技术网

C 正确清除键盘缓冲区

C 正确清除键盘缓冲区,c,C,我正试图为我的计算机课做一道题,但我遇到了麻烦。问题是一艘长度为l的渡轮需要运载长度为l的汽车从河的左侧到右侧,我相信有人以前听说过。我唯一的问题(我知道如何编写大部分代码,只是遇到了一个障碍)是我要么陷入无限循环,要么我的嵌套循环退出它嵌套在最底部的for循环。我的代码如下: #include <stdio.h> #include <stdlib.h> int main(int argc, char* argv[]) { int testCases; int ferr

我正试图为我的计算机课做一道题,但我遇到了麻烦。问题是一艘长度为l的渡轮需要运载长度为l的汽车从河的左侧到右侧,我相信有人以前听说过。我唯一的问题(我知道如何编写大部分代码,只是遇到了一个障碍)是我要么陷入无限循环,要么我的嵌套循环退出它嵌套在最底部的for循环。我的代码如下:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char* argv[])
{
int testCases;
int ferrySize;
int numOfCars;
char riverSide; // what side of river is the car on?
char c; // eats unnecessary characters
int test=1;
int i,j=0;// counters for "for" and "while" loops
int carLength;
const int cm_to_m=100;

printf("How many tests would you like to have?: ");
scanf("%d", &testCases);
printf("how long is the ferry in meters and how many cars are there?: ");
scanf("%d", &ferrySize);
ferrySize=ferrySize*cm_to_m;
scanf("%d", &numOfCars);
printf("cases %d ferrysize %d cars %d\n", testCases, ferrySize, numOfCars);
printf("\nhow long is each car (in cm) and what side is it on?\n(seperate this info with a space)\nseperate each car by new lines.");


for(i=0; i<=numOfCars;i++);
{
    scanf("%d", &carLength);
    scanf(" %c", &riverSide);
    printf("%c", riverSide);
    do
    {
        scanf("%c", &c);
        printf("%c", c);
    }while(c!='t' && c!='T');
    printf("%c", riverSide);

}
return 0;
#包括
#包括
int main(int argc,char*argv[])
{
int测试用例;
内质网化;
int NUMOFARS;
char riverSide;//汽车在河的哪一边?
char c;//吃不必要的字符
int检验=1;
int i,j=0;//用于“for”和“while”循环的计数器
国际卡伦斯酒店;
常数int cm_至m=100;
printf(“您希望进行多少次测试?”:”;
scanf(“%d”和测试用例);
printf(“渡轮有多长,以米为单位,有多少辆车?:”;
scanf(“%d”和“铁素化”);
金属化=金属化*厘米到米;
scanf(“%d”&numOfCars);
printf(“案例%d修复%d辆车%d\n”,测试案例,修复,numOfCars);
printf(“\n每辆车的长度(单位为厘米)以及它在哪一侧?\n(用空格分隔此信息)\n用新行操作每辆车。”);

对于(i=0;i尝试使用fflush(),它将清空缓冲区,代码将正常运行。

检查for循环行末尾的额外分号。您还可以在循环中执行以下操作以读取值(忽略安全性等):


对于(i=0;i对于扫描字符,请使用
getchar()
而不是
scanf(“%c”,…)
scanf()
不擅长扫描字符。出于某种原因,我的教授教我们使用scanf。我想他出于某种原因希望我们忽略空白字符?此外,我尝试了getchar函数,以防修复它-它具有与scanf相同的效果。退出程序(尽管没有崩溃)就好像它运行了所有的东西一样,尽管for循环仍然应该运行。你能给出一个示例输入和预期输出吗?输入:testCases:2 ferrysize:20 numOfCars:3(进入循环)第一辆车:carLength:20 riverSide:l(左,只需要l来表示)do[]while()中的scanf循环清空键盘缓冲区,车2:30:riverSide:r(对,r也是所需的全部。)do while循环再次清除缓冲区,这一次它结束了循环。通常它在第一个循环之后退出for循环,即使它不应该退出。缓冲区的文件是否为stdin?我会试试。谢谢如果你不给它任何参数,它将刷新所有内容。尝试一下。它仍然会退出所有循环。我会猜一些事情我的for循环有问题…Edit:我发现了问题!这是我的for循环,检查条件…for(…;…;…);而不是for(…;…;)。我检查了你的代码。我真的看不出它的意义,但显然你应该在for循环之后删除分号:因此,而不是for(i=0;i)
for(i=0; i<numOfCars; i++) {
    scanf("%d %c", &carLength, &riverSide);
    printf("car is %d long and on the %c side\n", carLength, riverSide");
}