【发布时间】:2018-09-23 14:39:52
【问题描述】:
//mvt 的代码。在这个 ch 中没有被编译器扫描。 ch 用作循环的条件。它的初始值为 'y' ,如果它变为 'n' ,则循环中断。在此代码中,我询问用户是否要继续,然后按 y else n 。但不知道为什么 gcc 不等待扫描它。这是一个简单的代码,但我无法理解错误。
int main(){
char ch;
temp=tm;
ch='y';
for(i=0;ch=='y';i++){
printf("enter memory size for process %d",i+1);
scanf("%d",&m);
if(m<temp)
{printf("memory allocated\n"); ms[n]=m; n++;
temp-=ms[i];
}
else
{printf("memory not allocated\n"); }
https://stackoverflow.com/users/10404087/rishabh-sharma
printf("do you want to continue");
scanf("%c",&ch);
}
printf("total memory : %d\n",tm);
printf("process \t occupied \n");
for(i=0;i<n;i++)
printf("%7d \t %8d \n",i+1,ms[i]);
printf("total externel fregment : %d \n",temp);
return 0;
}
【问题讨论】:
-
您将代码粘贴为问题中的文本而不是图像。
-
使用调试器。
scanf读入ch的内容是什么?然后你可以查找scanf is reading new line character -
请不要发代码图片,只发代码为文字。
-
草率的介绍对所有读者来说都是无礼的。