Simple int -> char conversion failing...

This is a discussion on Simple int -> char conversion failing... within the C Programming forums, part of the General Programming Boards category; Hi, I'm trying to convert an int to a char, just to see if the value entered is a alphabetic ...

  1. #1
    null
    Join Date
    Dec 2005
    Posts
    18

    Simple int -> char conversion failing...

    Hi,
    I'm trying to convert an int to a char, just to see if the value entered is a alphabetic character. Here's what I have in my method:
    Code:
    void FillUserArray( int userArray[], int arraySize )
    {
        int i = 0;  //array indexer
        int j = 1;  //counter for output format
        int x;    //holders for user input
    
        while( i < arraySize )
        {
            printf( "#%d: ", j );
            scanf( "%d", &x );
    
            userArray(i) = x; //code pasting into forum doesn't allow [ ]
    
            if( isalpha( (char)x ) )
                printf( "no alpha" );
    
            if( x < 1 || x > 47 )
            {
                printf( "\nThe number you are entering cannot be less than one, and no greater than forty-seven.\n\n" );
            }
            else if( HasBeenAssigned( userArray, arraySize, x, i ) == x )
            {
                printf( "\nPlease make sure the number you are entering has not already been assigned.\n\n" );
            }
            else
            {
                i++;
                j++;
            }
        }
    }
    When scanf reads a letter into x, though, it just loops endlessly. How can I detect if the value entered is a character, and then print something if it is?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 02:01 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 10:42 PM
  3. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 04:42 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 12:58 PM

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