Thread: A simple program?

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    4

    A simple program?

    Hello Everybody.
    I'm a beginner programmer in c.
    I'm trying to write a program that will ask the user to input a number and if he does not input a number, it tells him that it's not a number.

    When i first thought of the idea it looked very easy. But when i tried to do it, I just couldn't find a way.

    Help anybody?

  2. #2
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    First read the number as a string, then check every character in the string and see if it's a digit. If you get to the end of the string, it's a number.

    Spoilers follow, be warned.
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    
    int isnumber( char *number ) {
      while ( *number ) {
        if ( !isdigit( *number++ ) ) {
          return 0;
        }
      }
    
      return 1;
    }
    
    
    int main( void ) {
      char number[256];
    
      fgets( number, sizeof number, stdin );
      number[strlen( number ) - 1] = '\0';
    
      if ( isnumber( number ) ) {
        printf( "It's a number\n" );
      } else {
        printf( "Not a number\n" );
      }
    
      return 0;
    }

  3. #3
    Registered User Joelito's Avatar
    Join Date
    Mar 2005
    Location
    Tijuana, BC, México
    Posts
    310
    * PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with xfce4.
    * Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If you weren't so lazy, you would have found the FAQ, instead of having someone do it for you. I wish we still had candy.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    You can also try to read numbers with a self-written function, only using getchar() and some simple logic:

    Code:
    int c;
    signed int number = 0;
    int neg = 0;
    
    while ((c = getchar()) != '\n' && c != ' ')
    {
        if (c == '-' && number == 0)
            neg = !neg;
        if (c >= '0' && c <= '9')
            if (neg)
                number = (number * 10) - (c - '0');
            else
                number = (number * 10) + (c - '0');
    }
    The function isn't very robust if you enter stuff like "-356-25" but it should just illustrate how you could do it.
    Last edited by KONI; 03-27-2007 at 02:48 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a very simple program
    By htdefiant in forum C++ Programming
    Replies: 13
    Last Post: 08-14-2007, 01:27 PM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. [Help] Simple Array/Pointer Program
    By sandwater in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 02:42 PM
  4. simple silly program
    By verbity in forum C Programming
    Replies: 5
    Last Post: 12-19-2006, 06:06 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM