Checking for letters

This is a discussion on Checking for letters within the C Programming forums, part of the General Programming Boards category; I'm writing a program that involves numbers and I want to make sure that what the user enters are numbers. ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    2

    Checking for letters

    I'm writing a program that involves numbers and I want to make sure that what the user enters are numbers. Here is what I have so far but can't figure out how to do the check;
    Code:
    /*************
    #include <stdio.h>
    
    void main (void)
    
    {
    	
    	char name [20];
    	
    	int numbers [5];
    
    	int average;
    	
    	printf ("Please enter your name\n\n");
    	
    	gets(name);
    	
    	printf ("\n\nThank you %s, now enter 5 numbers pressing Enter after each\n\n", name);
    
    	scanf("%d%d%d%d%d", &numbers[0],&numbers[1],&numbers[2],&numbers[3],&numbers[4]);
    
    	average = (numbers[0] + numbers[1] + numbers[2] + numbers[3] + numbers[4]) / 5;
    
    	printf ("\n\n%s the average of those numbers is %d,\n\n", name, average);
    
    	printf ("\n\n%d%d%d%d%d\n\n", numbers[4], numbers[3], numbers[2], numbers[1], numbers[0]);
    
    	return ;
    
    }
    ************/
    I would really be thankful for any help.

    Code tags added by Hammer

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    read the input into a char buffer and then loop through it, rejecting it if a non-digit is encountered.
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >void main (void)
    No, main returns an int and nothing else.

    >gets(name);
    This is okay only for student programs that will be thrown away shortly, otherwise it's just too unsafe to be worth it. Consider using fgets instead.

    >I want to make sure that what the user enters are numbers.
    In this case you can simply report an error if scanf fails. This ensures that the numbers you add together are well defined:
    Code:
    #include <stdio.h>
    
    int main (void)
    {
      char name [20];
      int numbers [5];
      int average;
      
      printf ("Please enter your name\n\n");
    
      gets(name);
    
      printf ("\n\nThank you %s, now enter 5 numbers pressing Enter after each\n\n", name);
    
      if ( scanf("%d%d%d%d%d", &numbers[0],&numbers[1],&numbers[2],&numbers[3],&numbers[4]) == 5 ) {
        average = (numbers[0] + numbers[1] + numbers[2] + numbers[3] + numbers[4]) / 5;
    
        printf ("\n\n%s the average of those numbers is %d,\n\n", name, average);
        printf ("\n\n%d%d%d%d%d\n\n", numbers[4], numbers[3], numbers[2], numbers[1], numbers[0]);
      }
      else
        printf ("Only numbers please\n");
    
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  2. Overflow and range checking for mul/div
    By Elysia in forum C++ Programming
    Replies: 28
    Last Post: 06-06-2008, 02:09 PM
  3. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  4. Forced moves trouble!!
    By Zishaan in forum Game Programming
    Replies: 0
    Last Post: 03-27-2007, 06:57 PM
  5. Problems about gcc installation
    By kevin_cat in forum Linux Programming
    Replies: 4
    Last Post: 08-09-2005, 09:05 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21