Thread: Input error handling

  1. #1
    Registered User
    Join Date
    Dec 2014
    Posts
    2

    Input error handling

    Good day,

    I'm new in programming, and trying to write a code in C. The requirement is following - to write a text, and to find frequency of 1 chosen character in it. The main thing is that program should check user input (for example, I want to check if user entered "char" --> then correct, or if entered "int" --> not correct). But the program still doesn't check. So I have:

    Code:
    #include <stdio.h>
    int main(){
       char c[1000], ch;
       char i, count=0;
       printf("Enter a text: ");
       gets(c);
       printf("Enter a letter to find it's frequency: ");
       if (scanf("%c",&ch) == 1)
             {                                                
          for(i=0;c[i]!='\0';++i)
             {
             if(ch==c[i])
             ++count;
             }
             
             printf("Frequency of %c = %d", ch, count);    }     
       else
       {
              printf("It's not a letter!");
       }
           
       getch();
    }


    Am I on my right way using if/else?

    Thank you in advance for your help.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    That's pretty good for a first attempt.

    But you need to stop using gets()
    FAQ > Why gets() is bad / Buffer Overflows - Cprogramming.com

    Apart from that, try this
    if (scanf("%c",&ch) == 1 && isalpha(ch) )
    Look in ctype.h for other 'is' functions.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2014
    Posts
    2
    @Salem, thank You.

    Your offered 'is' function helped now.

    About gets(). Well, yes, my sentences were not so big till this moment I will learn more about using fgets(), not pointers.

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Quote Originally Posted by annik View Post
    About gets(). Well, yes, my sentences were not so big till this moment I will learn more about using fgets(), not pointers.
    You should use better variable names. Instead of 'char c[1000]', 'char line[1000]' or something more descriptive. Single letters should be used only for an index or something else trivial.

    As for gets(), it should never be used, no matter what the size of the string. Buffer overflow is always possible with gets()! Please use fgets() instead.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. handling user input
    By rodrigorules in forum C++ Programming
    Replies: 4
    Last Post: 11-12-2009, 06:21 AM
  2. Wrong input handling with cin
    By ymstandard in forum C++ Programming
    Replies: 15
    Last Post: 08-24-2006, 04:35 PM
  3. Handling User Input
    By wcfbv in forum C Programming
    Replies: 3
    Last Post: 11-20-2002, 07:35 AM
  4. Help with input handling.
    By Will in forum C++ Programming
    Replies: 5
    Last Post: 05-09-2002, 01:00 PM
  5. Handling input from cin
    By Zarkon in forum C++ Programming
    Replies: 1
    Last Post: 12-17-2001, 08:03 AM