1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

cant find mistake in code,code prints without meeting conditions

Discussion in 'C' started by SaKaMoToSaN15, Jan 13, 2018.

  1. SaKaMoToSaN15

    SaKaMoToSaN15 New Member

    Joined:
    Jan 7, 2018
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    1
    Gender:
    Male
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define WORDS 10
    #define WORDS_LENGHT 10
    #define ABC_LENGHT 25
    
    //defines for index in arrays:
    #define TWO 2
    #define THREE 3
    #define FOUR 4
    #define FIVE 5
    #define SIX 6
    #define SEVEN 7
    #define EIGHT 8
    #define NINE 9
    #define TEN 10
    #define ELEVEN 11
    #define TWELVE 12
    #define THREETEEN 13
    #define FOURTEEN 14
    #define FIVETEEN 15
    #define SIXTEEN 16
    #define SEVENTEEN 17
    #define EIGHTTEEN 18
    #define NINETEEN 19
    #define TWENTY 20
    #define TWENTY_ONE 21
    #define TWENTY_TWO 22
    #define TWENTY_THREE 23
    #define TWENTY_FOUR 24
    #define TWENTY_FIVE 25
    
    
    void welcome_AND_checkInput(char userSentence[][WORDS_LENGHT],int sentence_Counter[]);
    
    int main()
    {
        char userSentence[WORDS][WORDS_LENGHT] = {0};
        int sentence_Counter[ABC_LENGHT] = {0};
        welcome_AND_checkInput(userSentence,sentence_Counter);
    }
    /*
    This functions gets user input and checks if all the alphabetic letters have been inputed at least once.
        input:string array - char userSentence[][WORDS_LENGHT],counter array - int sentence_Counter[].
            output:none.
    */
    void welcome_AND_checkInput(char userSentence[][WORDS_LENGHT],int sentence_Counter[])
    {
        int i = 0;
        int j = 0;
        int k = 0;
        int counter = 0;
        printf("Please Enter Your Sentence:\n");
        for(i = 0; i < WORDS ; i++)
        {
            fgets(userSentence,WORDS_LENGHT,stdin);
          
            for(j = 0 ; j < WORDS_LENGHT ; j++)
            {
                switch (userSentence[j])
                {
                    case 'a':
                    sentence_Counter[0]++;
                    break;
                    case 'b':
                    sentence_Counter[1]++;
                    break;
                    case 'c':
                    sentence_Counter[TWO]++;
                    break;
                    case 'd':
                    sentence_Counter[THREE]++;
                    break;
                    case 'e':
                    sentence_Counter[FOUR]++;
                    break;
                    case 'f':
                    sentence_Counter[FIVE]++;
                    break;
                    case 'g':
                    sentence_Counter[SIX]++;
                    break;
                    case 'h':
                    sentence_Counter[SEVEN]++;
                    break;
                    case 'i':
                    sentence_Counter[EIGHT]++;
                    break;
                    case 'j':
                    sentence_Counter[NINE]++;
                    break;
                    case 'k':
                    sentence_Counter[TEN]++;
                    break;
                    case 'm':
                    sentence_Counter[ELEVEN]++;
                    break;
                    case 'n':
                    sentence_Counter[TWELVE]++;
                    break;
                    case 'l':
                    sentence_Counter[THREETEEN]++;
                    break;
                    case 'o':
                    sentence_Counter[FOURTEEN]++;
                    break;
                    case 'p':
                    sentence_Counter[FIVETEEN]++;
                    break;
                    case 'q':
                    sentence_Counter[SIXTEEN]++;
                    break;
                    case 'r':
                    sentence_Counter[SEVENTEEN]++;
                    break;
                    case 's':
                    sentence_Counter[EIGHTTEEN]++;
                    break;
                    case 't':
                    sentence_Counter[NINETEEN]++;
                    break;
                    case 'u':
                    sentence_Counter[TWENTY]++;
                    break;
                    case 'v':
                    sentence_Counter[TWENTY_ONE]++;
                    break;
                    case 'w':
                    sentence_Counter[TWENTY_TWO]++;
                    break;
                    case 'x':
                    sentence_Counter[TWENTY_THREE]++;
                    break;
                    case 'y':
                    sentence_Counter[TWENTY_FOUR]++;
                    break;
                    case 'z':
                    sentence_Counter[TWENTY_FIVE]++;
                    break;
                }
                    for(k = 0; k < ABC_LENGHT ; k++)
                    {
                        if(sentence_Counter[k] != 0)
                        {
                            counter++;
                        }
                        if(counter == 25)
                        {
                            printf("there is: %d counters",counter);
                        }
                    }
              
            }
        }
      
    }
    

    My code is doing a very simple thing:
    it gets a sentence and checks if the sentece is a panogram.
    my problem is simple i dont understand why the if condtion: if(counter == 25) is allmost always met,even if i input only one or two words.
     

Share This Page