Thread: New to C, gets problem

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    2

    New to C, gets problem

    I find that this function

    //letter counter
    void letter_counter(void) {
    int i = 0, counter = 0;
    char word[80];
    char letter, function;
    printf("Type in a word: ");
    gets(word);
    printf("Type a letter: ");
    scanf("%c", &letter);
    while(word[i] != '\0') {
    if(word[i] == letter) {
    counter++;
    }
    i++;
    }
    printf("\nThat letter appeared %d time(s)\n", counter);
    }

    is producing "Type in a word: Type in a letter: _" (the underscore resembles 'waiting for input').

    However having it in main with nothing else fixes the problem. ie:

    #include <string.h>
    #include <stdio.h>

    int main() {
    int i = 0, counter = 0;
    char word[80];
    char letter, function;
    printf("Type in a word: ");
    gets(word);
    printf("Type a letter: ");
    scanf("%c", &letter);
    while(word[i] != '\0') {
    if(word[i] == letter) {
    counter++;
    }
    i++;
    }
    printf("\nThat letter appeared %d time(s)\n", counter);
    }

    The full program:

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>

    void letter_counter(void);
    void calculator(void);

    int main() {
    char function;
    printf("Type in a function you would like to use: ");
    printf("\nFor letter counter type \"l\"\n");
    printf("For calculator type \"c\"");
    printf("\n\nEnter a function: ");
    while(function != 'e') {
    scanf("%c", &function);
    if(function == 'l') {
    letter_counter();
    printf("\nEnter another function or type \"e\" to exit: ");
    } else if(function == 'c') {
    calculator();
    printf("\n\nEnter another function or type \"e\" to exit: ");
    } else if(function == 'e') {
    exit(0);
    }
    }
    }

    //letter counter
    void letter_counter(void) {
    int i = 0, counter = 0;
    char word[80];
    char letter, function;
    printf("Type in a word: ");
    gets(word);
    printf("Type a letter: ");
    scanf("%c", &letter);
    while(word[i] != '\0') {
    if(word[i] == letter) {
    counter++;
    }
    i++;
    }
    printf("\nThat letter appeared %d time(s)\n", counter);
    }

    //calculator
    void calculator(void) {
    printf("For: Addition type \"a\"\n : Subtraction type \"s\"\n : Division type \"d\"");
    char type;
    printf("\n\nEnter calculation type: ");
    scanf("%s", &type);
    int i = 0, j = 0, k = 0, p = 0, g = 0, h = 0, u = 0, o = 0;
    //addition
    if(type == 'a') {
    printf("\nEnter two numbers to add separated by a space: ");
    scanf("%d %d", &i, &j);
    i = i + j;
    printf("Answer is: %d", i);
    //subtraction
    } else if(type == 's') {
    printf("\nEnter two numbers to subtract separated by a space: ");
    scanf("%d %d", &k, &p);
    k = k - p;
    printf("Answer is: %d", k);
    //division
    } else if(type == 'd') {
    printf("\nEnter two numbers to divide separated by a space: ");
    scanf("%d %d", &g, &h);
    g = g / h;
    printf("Answer is: %d", g);
    //multiplication
    } else if(type == 'm') {
    printf("\nEnter two numbers to multiply separated by a space: ");
    scanf("%d %d", &o, &u);
    o = o * u;
    printf("Answer is: %d", o);
    }
    }

    Hope anyone can help :P
    It's not a huge deal, but I really want to know why it's not working

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Thanks for posting your code, and welcome to the forum, Slave.

    Two things you need to know, right off:

    1) Always put code tags around your program. In the edit window, highlight your code, and click on the # icon (right below the smilies pull down thingy.

    2) Be sure you have some good indentation: compare these two snippets of your program

    while(function != 'e') {
    scanf("%c", &function);
    if(function == 'l') {
    letter_counter();
    printf("\nEnter another function or type \"e\" to exit: ");
    } else if(function == 'c') {
    calculator();
    printf("\n\nEnter another function or type \"e\" to exit: ");
    } else if(function == 'e') {
    exit(0);
    }
    }
    }

    and
    Code:
    while(function != 'e') {
       scanf("%c", &function);
       if(function == 'l') {
          letter_counter();
          printf("\nEnter another function or type \"e\" to exit: ");
       } 
       else if(function == 'c') {
          calculator();
          printf("\n\nEnter another function or type \"e\" to exit: ");
       } 
       else if(function == 'e') {
          exit(0);
       }
    }
    As you program more, your brain naturally becomes more skilled at reading code in the common indented formats. Lacking that, it's like walking through knee deep snow.

    I'm still trying to wade through your code for your answer.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Slave, you got ambushed by the "leftovers in the keyboard buffer" trap.

    You don't have it in main(), because you didn't go through the menu, and get the leftovers, built up.

    Here's the fix:

    Code:
    //letter counter
    void letter_counter(void) {
      int i = 0, counter = 0;
      char word[80];
      char letter, function;
      printf("Type in a word: ");
      /* this next line may be used for REALLY dirty keyboard input problems 
            while((letter = getchar()) != '\n');
      */
    
      letter = getchar(); //cleans out 1 char only but it's all you needed
      gets(word);
      printf("Type a letter: ");
      scanf("%c", &letter);
      while(word[i] != '\0') {
        if(word[i] == letter) {
          counter++;
        }
        i++;
      }
      printf("\nThat letter appeared %d time(s)\n", counter);
    }
    Before you ask, no, you can't use fflush(stdin). That works only when directed to outbound buffers, not inbound one's.

  4. #4
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Hey this is the modification of your code

    the scanf was not getting for letter it will work with this code

    Code:
    /*                                                                                                                                                                                                                
     * This function will work like fflush stdin to get                                                                                                                                                               
     * extra junk charachter into it                                                                                                                                                                                  
     */
    void clear_kb(void) {
      char junk[80];
      gets(junk);
    }
    
    
    //letter counter                                                                                                                                                                                                  
    void letter_counter(void) {
      int i = 0, counter = 0;
      char word[80] = {0};
      char letter;
      printf("Type in a word: \n");
      scanf( "%s", &word);
        clear_kb();
    
        printf("Type a letter: \n");
        scanf("%c", &letter);
    
        while(word[i] != '\0') {
          if(word[i] == letter) {
            counter++;
          }
          i++;
        }
        printf("\nThat letter appeared %d time(s)\n", counter);
    }

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    2
    Thanks guys for your replies.

    In regards to indentation I was lazy after I pasted it screwed up the formatting :P I'll take more care next time and put tags around it.

    These forums should be EXTREMELY helpful while I learn C ;D

    Cheers.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM