Skipping a character?

This is a discussion on Skipping a character? within the C Programming forums, part of the General Programming Boards category; Is there a way to prompt a user for a number, but if the user enters a character such as ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    26

    Skipping a character?

    Is there a way to prompt a user for a number, but if the user enters a character such as a letter, to just skip that entry? If it helps, here is the code I have so far:

    Code:
    #include <stdio.h>
    #include <math.h>
    
    #define Max_numbers 10
    
    int main(void)
    
    {        
             
             double x[Max_numbers];
    
                    
             int i;
             
             char str[20];
    
        printf( "Please enter your name:\t " );
        gets(str);
        printf("Hello, %s!\n", str);
        printf("Please Enter %d numbers separated by spaces or returns\n",
                       Max_numbers);
        for (i = 0; i < Max_numbers; ++i) {
            
            scanf("%lf", &x[i]);
            if (x[i] == 0)
         i +=10; }
     
    
    
    system("Pause");      
                    
    
    }

    I have it so the program will stop retrieving numbers if the user enters "0". Is an array the best way to do this? Thanks in advance!

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    FAQ.
    Last edited by Dave_Sinkula; 02-17-2006 at 08:40 PM. Reason: Changed to the whole FAQ.
    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.*

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    26
    I checked the FAQ but still couldn't find it. Very sorry, but does anyone have any specific help? It would be greatly appreciated. Thanks.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Sorry, I was thinking of the ones that were about How to Read a String, How to Read a Number (for ints, but the general information can also be applied to floats), Why Not to Use gets, Why using system is not preferred (although apparently the FAQ doesn't mention that). And other stuff like that.
    Last edited by Dave_Sinkula; 02-17-2006 at 08:53 PM.
    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.*

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    26
    I now understand how to test for a number. The problem I am having now is how to simply skip the input if a non-number is entered. Please help me out on this. Thanks.

    Edit:

    I figured this out, but I am wondering if there is a way to find the smallest value in an array, and then print it out? Basically I have to sort the numbers in the array from smallest to largest.
    Last edited by kabuatama; 02-17-2006 at 11:22 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 10-07-2008, 07:19 PM
  2. wide character (unicode) and multi-byte character
    By George2 in forum Windows Programming
    Replies: 6
    Last Post: 05-05-2007, 01:46 AM
  3. <string> to LPCSTR? Also, character encoding: UNICODE vs ?
    By Kurisu33 in forum C++ Programming
    Replies: 7
    Last Post: 10-09-2006, 01:48 AM
  4. Character handling help
    By vandalay in forum C Programming
    Replies: 18
    Last Post: 03-29-2004, 05:32 PM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 04:23 PM

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