Thread: Loop until enter correct value

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    5

    Question Loop until enter correct value

    Hi there,

    I would like to let the following code keep asking for correct value if the value enter cannot vaildate by myvaild_addr(). Could you give me some hint to make it work. Thanks in advance.


    #include<stdio.h>
    #include<string.h>

    myvaild_addr(input, a, b, c, d)
    {
    if ( strtok(NULL, ".") != NULL )
    printf("Invalid address! Please enter again: ");
    else if ( ( a < 1 ) || ( b < 1 ) || ( c < 1 ) || ( d < 1 ) || ( a > 254 ) || ( b > 254 ) || ( c > 254 )
    || ( d > 254 ) )
    printf("Invalid address! Please enter again: ");
    }

    myaddr_class(a, b, c, d)
    {
    if ( ( a > 0 ) && ( a < 127 ) )
    return "A";
    else if ( ( a > 127 ) && ( a < 192 ) )
    return "B";
    else if ( ( a > 191 ) && ( a < 224 ) )
    return "C";
    else if ( ( a > 223 ) && ( a < 240 ) )
    return "D";
    else if ( ( a > 239 ) && ( a < 256 ) )
    return "E";
    }

    main()
    {
    char input[100];
    int a, b, c, d, output;

    printf("Please enter a valid IP address: ");
    scanf("%s", &input);
    a = atoi(strtok(input, "."));
    b = atoi(strtok(NULL, "."));
    c = atoi(strtok(NULL, "."));
    d = atoi(strtok(NULL, "."));
    myvaild_addr(input, a, b, c, d);
    output = myaddr_class(a, b, c, d);
    printf("It is a class %s address!\n", output);
    }

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    23
    try this...

    char *f;
    scanf(f,"%s");//does this work, please check for me...

    while( 1 )
    {
    if(strcmp(f,"correct answer")==0)
    break;

    /* process f */


    scanf(f,"%s");
    }



    hope this works

  3. #3
    free(me);
    Join Date
    Oct 2001
    Location
    Santo Domingo, DN, Dominican Republic
    Posts
    98

    ...

    Code:
    do
    {
        getAddress();
    }
    while(!validateAddress());
    I think that'll give you the main idea of how to do it.

    adios,
    biterman.
    Do you know how contemptous they are of you?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help With a BlackJack Program in C
    By Jp2009 in forum C Programming
    Replies: 15
    Last Post: 03-30-2009, 10:06 AM
  2. Can a "switch" be inside a loop?
    By gmk0351 in forum C Programming
    Replies: 5
    Last Post: 03-28-2008, 05:47 PM
  3. trouble with creating a loop to read certain number of inputs
    By import tuner650 in forum C++ Programming
    Replies: 2
    Last Post: 03-20-2008, 07:28 PM
  4. Newbie Help: Currency Converter
    By Ashfury in forum C Programming
    Replies: 10
    Last Post: 11-06-2005, 01:21 PM
  5. terminate 0 - PLEASE HELP
    By Unregistered in forum C Programming
    Replies: 11
    Last Post: 11-21-2001, 07:30 AM