[英]First iteration is skipped in C
该程序应按字符分析字符串,并根据手指按下每个按钮以键入字符串的方式给出输出。 它在经过一个迭代后才起作用,但是会忽略第一个迭代。 有人可以发现我需要调整的内容吗?
#include<stdio.h>
#include<string.h>
int main(void){
char userInput[200];
int len;
int fingers[10] = {0,0,0,0,0,0,0,0,0,0 };
int i;
int total = 0;
int charCount = 0;
int strCount = 0;
int rightHand = 0;
int leftHand = 0;
int left = 0;
int right = 0;
printf("Enter some strings, end with cntrl-d: \n");
scanf("%s\n", userInput);
i = 0;
while( !feof(stdin) && userInput[i] != '\n' && userInput[i] != ' '){
len = strlen(userInput) - 1;
for(i = 0; i < len; i++){
if(userInput[i]=='q'||userInput[i]=='a'||userInput[i]=='z'||userInput[i]=='1'){
fingers[0]++;
total++;
charCount++;
left++;
}
else if(userInput[i]=='Q'||userInput[i]=='A'||userInput[i]=='Q'||userInput[i]=='!'){
fingers[0]++;
fingers[7]++;
total = total + 2;
charCount++;
left++;
right++;
}
else if(userInput[i]=='2'||userInput[i]=='w'||userInput[i]=='s'||userInput[i]=='x'){
fingers[1]++;
total++;
charCount++;
left++;
}
else if(userInput[i]=='@'||userInput[i]=='W'||userInput[i]=='S'||userInput[i]=='X'){
fingers[1]++;
fingers[7]++;
total = total + 2;
charCount++;
left++;
right++;
}
else if(userInput[i]=='3'||userInput[i]=='e'||userInput[i]=='d'||userInput[i]=='c'){
fingers[2]++;
total++;
charCount++;
left++;
}
else if(userInput[i]=='#'||userInput[i]=='E'||userInput[i]=='D'||userInput[i]=='C'){
fingers[2]++;
fingers[7]++;
total = total + 2;
charCount++;
left++;
right++;
}
else if(userInput[i]=='4'||userInput[i]=='r'||userInput[i]=='f'||userInput[i]=='v'
||userInput[i]=='5'||userInput[i]=='t'||userInput[i]=='g'||userInput[i]=='b'){
fingers[3]++;
total++;
charCount++;
left++;
}
else if(userInput[i]=='$'||userInput[i]=='R'||userInput[i]=='F'||userInput[i]=='V'
||userInput[i]=='%'||userInput[i]=='T'||userInput[i]=='G'||userInput[i]=='B'){
fingers[3]++;
fingers[7]++;
total = total + 2;
charCount++;
left++;
right++;
}
else if(userInput[i]=='6'||userInput[i]=='y'||userInput[i]=='h'||userInput[i]=='n'
||userInput[i]=='7'||userInput[i]=='u'||userInput[i]=='j'||userInput[i]=='m'){
fingers[4]++;
total++;
charCount++;
right++;
}
else if(userInput[i]=='^'||userInput[i]=='Y'||userInput[i]=='H'||userInput[i]=='N'
||userInput[i]=='&'||userInput[i]=='U'||userInput[i]=='J'||userInput[i]=='M'){
fingers[4]++;
fingers[0]++;
total = total + 2;
charCount++;
right++;
left++;
}
else if(userInput[i]=='8'||userInput[i]=='i'||userInput[i]=='k'||userInput[i]==','){
fingers[5]++;
total++;
charCount++;
right++;
}
else if(userInput[i]=='*'||userInput[i]=='I'||userInput[i]=='K'||userInput[i]=='<'){
fingers[5]++;
fingers[0]++;
total = total + 2;
charCount++;
right++;
left++;
}
else if(userInput[i]=='9'||userInput[i]=='o'||userInput[i]=='l'||userInput[i]=='.'){
fingers[6]++;
total++;
charCount++;
right++;
}
else if(userInput[i]=='('||userInput[i]=='O'||userInput[i]=='L'||userInput[i]=='>'){
fingers[6]++;
fingers[0]++;
total = total + 2;
charCount++;
right++;
left++;
}
else if(userInput[i]=='0'||userInput[i]=='p'||userInput[i]==';'||userInput[i]=='/'
||userInput[i]=='-'||userInput[i]=='='||userInput[i]=='['||userInput[i]==']'||userInput[i]=='\''){
fingers[7]++;
total++;
charCount++;
right++;
}
else if(userInput[i]=='*'||userInput[i]=='I'||userInput[i]=='K'||userInput[i]=='<'
||userInput[i]=='_'||userInput[i]=='+'||userInput[i]=='{'||userInput[i]=='}'||userInput[i]=='"'){
fingers[7]++;
fingers[0]++;
total = total + 2;
charCount++;
right++;
left++;
}
}
if(left == 0){
rightHand++;
}
else if(right == 0){
leftHand++;
}
left = 0;
right = 0;
strCount++;
scanf("%s\n", userInput);
}
printf("%d strings entered, %d total characters\n", strCount, charCount);
printf(" %d typed only using left hand\n", leftHand);
printf(" %d typed only using right hand\n", rightHand);
printf("%d total keystrokes\n", total);
printf(" %d - left index\n", fingers[3]);
printf(" %d - left middle\n", fingers[2]);
printf(" %d - left ring\n", fingers[1]);
printf(" %d - left pinky\n", fingers[0]);
printf(" %d - right index\n", fingers[4]);
printf(" %d - right middle\n", fingers[5]);
printf(" %d - right ring\n", fingers[6]);
printf(" %d - right pinky\n", fingers[7]);
return 0;
}
尝试从scanf
输入格式中删除\\n
。
我相信这可以防止scanf
在按下Enter键时返回,因为它正在寻找换行符作为返回输入的一部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.