jeudi 9 avril 2015

C - Program only doing 1 part of a if else statement

Hey so i am trying to create a program that will ask the user for a letter then count how many times that letter appears in a .txt document. I have the code 99% done its just that whatever if statement i put first is the only one that gives back the correct amount.



#include<stdio.h>
#include<string.h>
#include<ctype.h>
#define n 61 // Maximum size of file name

FILE* open_file(void) {
char file_name[n]; FILE* input_file;
printf("Enter name of the input file: "); scanf("%s", file_name);
input_file = fopen(file_name, "r");
while (input_file == NULL) {
printf("\nERROR: File Not found \"%s\"\n", file_name);
printf("Re-enter file name: ");
scanf("%s", file_name);
input_file = fopen(file_name, "r");}
return input_file;}

void main() {
char inputLetter;
char lowerLetter;
char upperLetter;
char chr;
int upper;
int lower;
FILE* input_file = open_file();
printf("Enter letter: ");
scanf(" %c", &inputLetter);

lowerLetter = tolower(inputLetter);
upperLetter = toupper(inputLetter);
while(1){
chr = fgetc(input_file);
if (chr == EOF){
break;
}
else if(chr ==lowerLetter){
lower++;
}
else if(chr == upperLetter){
upper++;
}
}


printf(" %c", inputLetter);
printf(" %c %d ", lowerLetter, lower);
printf(" %c %d ", upperLetter, upper);
}


When reading a .txt document with 22 lower case e and 2 capital 2 it will print



e e 22 E 258


If i switch the two if statements with a counter it will print



e e 278 E 2


What am i doing wrong that it wont count both correctly?


Aucun commentaire:

Enregistrer un commentaire