Data Validation problem

This is a discussion on Data Validation problem within the C Programming forums, part of the General Programming Boards category; Hi, I was wondering if I could get some advise. This is just a small part of the code. When ...

  1. #1
    Unregistered
    Guest

    Data Validation problem

    Hi, I was wondering if I could get some advise.

    This is just a small part of the code.

    When the user inputs the wrong data, he is prompted to enter the correct data, but the only problem is that the program ends and the user has to start all over.

    Is there a way I could get the user back to the beginning, where the user is prompted to enter two hex numbers without the program ending.

    Thanks

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

    int main(int argc, char *argv[])
    {
    int reply;

    puts("\n****************************************** ******************");
    puts("\nEnter a menu number selection between 1 and 4, 0 to exit: ");
    puts("\n****************************************** ******************");
    scanf("%d", &reply);

    switch (reply)
    {
    case 0:
    exit(0);
    case 1:
    {
    int a, b, c, r;

    printf ("Two numbers please.\n");

    r = scanf ("%x%x", &a, &b);
    if ( r == 2 )
    {
    c = a + b;
    printf ("%#x", c);
    }
    else
    printf("hex please");

    break;
    }
    default:
    puts("Between 1 and 4, please!\n");
    } /* end of switch */
    return 0;
    }

  2. #2
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    523
    You need a loop and a flag to check if input is correct, somethin like.
    Code:
    flag = 0;
    
    while(!flag)
    {
        /* code goes here */
        if(r == 2)
       {
            c = a + b; 
            printf ("%#x", c); 
            flag = 1;
       } 
       else 
           printf("hex please"); 
    }
    The loop only stops when flag is non-zero and flag is only changed if input is correct.
    Hope this gives you some help.

    You could even replace the flag with a break.
    Code:
    while(1)
    {
        /* code goes here */
        if(r == 2)
       {
            c = a + b; 
            printf ("%#x", c); 
            break;
       } 
       else 
           printf("hex please"); 
    }
    Last edited by C_Coder; 02-03-2002 at 01:46 PM.

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    /* a simple menu */
    Code:
    do
    {
         printf("\nPlease enter your choice for routine - 1 to 4\n");
         printf("Enter zero to exit");
         scanf("%d", choice);
         switch(choice)
        { 
              case 0: printf("\n\nGoodbye");
                         break;
              case 1: /*whatever*/
                         break;
              case 2: /*whatever*/
                         break;
              case 3: /*whatever*/
                         break;
              case 4: /*whatever*/
                         break;
              default:  printf("\n\nInvalid entry try again");
         }
    } while(choice);
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  4. #4
    Unregistered
    Guest
    It almost works. It's getting closer I think. What's happening now is when I enter a non hex number, I get an endless loop. I'm not sure why it's happening.

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

    int main(int argc, char *argv[])
    {
    int reply;

    puts("\n****************************************** ******************");
    puts("\nEnter a menu number selection between 1 and 4, 0 to exit: ");
    puts("\n****************************************** ******************");
    scanf("%d", &reply);

    switch (reply)
    {
    case 0:
    exit(0);
    case 1:
    {
    int a, b, c, r;
    int flag ;

    while (1)
    {
    printf ("Two numbers please.\n");

    r = scanf ("%x%x", &a, &b);
    if ( r == 2 )
    {
    c = a + b;
    printf ("%#x", c);
    flag = 0;
    }
    else
    printf("hex please");
    }
    break;
    }
    default:
    puts("Between 1 and 4, please!\n");
    } /* end of switch */
    return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 06-14-2005, 05:45 AM
  2. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. can't insert data into my B-Tree class structure
    By daluu in forum C++ Programming
    Replies: 0
    Last Post: 12-05-2002, 05:03 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

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