0

This program is supposed to analyze a string character by character and give output based on which finger presses each button to type in the string. It works after one iteration is already passed, but ignores the first iteration. Can someone spot what I need to tweak?

#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;
}
Graham
  • 7,431
  • 18
  • 59
  • 84
NZ1999
  • 21
  • 5
  • What are you inputing and what's your expected output? – Matthew Kerian Feb 23 '18 at 02:36
  • `scanf("%s\n", userInput);` --> `scanf("%199s", userInput);`: it is almost always a mistake to use trailing whitespace characters in `scanf()` family format strings. This causes `scanf()` to wait for a _non_-whitespace character to be entered by the user before returning. In general this causes problems with interactive user input. Note that you should always specify a maximum width when using `%s` to read into a string to avoid potential buffer overflow. – ad absurdum Feb 23 '18 at 03:04
  • please read [Why is “while ( !feof (file) )” always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – Ed Heal Feb 23 '18 at 03:42
  • And check the return value from `scanf` – Ed Heal Feb 23 '18 at 03:43

1 Answers1

0

Try removing the \n from the scanf input format.

I believe this is preventing scanf from returning once enter is pressed as it is looking for the newline character as part of the input to return.