无法理解的code的行为 [英] Cant understand the code's behaviour
本文介绍了无法理解的code的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我写这算没有。在给定的字孔(如A,D有1孔,B有2个孔,E有codechef没有holes..A问题)。
I wrote this to count the no. of holes in a given word(ex A,D have 1 holes, B has 2 holes, E has no holes..A question of codechef).
#include<stdio.h>
int func(char ch)
{
if((ch=='Q')||(ch=='R')||(ch=='O')||(ch=='P')||(ch=='A')||(ch=='D'))
return 1;
else if(ch=='B')
return 2;
else return 0;
}
int main()
{
int t,i=0,count=0;char ch;
scanf("%d",&t);
int val[t];
for(i=0;i<t;i++)
{
count=0;
while((ch=getchar())!='\n')
count+=func(ch);
printf("Value of %d word-%d\n",i+1,count);
val[i]=count;
}
for(i=0;i<t;i++)
printf("%d\n",val[i]);
return 0;
}
此程序将输入T编号。也就是说,直到它遇到的'\\ n'。我无法理解为什么程序不能输入第一个字和打印0字为先,但可以正常使用的话的其余部分。
this program will input t no. of words until it encounters '\n'. I cant understand why is the program not able to input the 1st word and prints 0 for the first word but works properly for the rest of the words.
推荐答案
scanf函数
将不能提取的'\\ n'
,所以 CH =的getchar()
会给你的的'\\ n'
这仍然在你的标准输入。更改 scanf函数
行:
scanf
won't extract '\n'
, therefore ch=getchar()
will give you the '\n'
that's still in your standard input. Change your scanf
line to:
scanf("%d\n",&t);
这篇关于无法理解的code的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文