validate data

This is a discussion on validate data within the C Programming forums, part of the General Programming Boards category; I am working on a program that validates the entered data and when I have this part of the code.... ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    21

    validate data

    I am working on a program that validates the entered data and when I have this part of the code....

    ////////////////////////////////////////
    do{ //validate creditrating
    printf ("How is the applicant's credit rating?[G for good or B for bad]\n");
    scanf ("%s", &creditrating);
    if (creditrating[0] != 'g')
    {
    printf ("%s \n", creditrating);
    printf ("Only these letters are valid: g, G, b, B \n");
    printf ("How is the applicant's credit rating?[G for good or B for bad]\n");
    scanf ("%s", &creditrating);
    }
    }while (creditrating[0] != 'g');
    //////////////////////////////////////////////

    the program validates and only breaks out of loop and moves on when user enters a 'g'.
    but when i have this code...

    ///////////////////////////////////////////
    do{ //validate creditrating
    printf ("How is the applicant's credit rating?[G for good or B for bad]\n");
    scanf ("%s", &creditrating);
    if (creditrating[0] != 'g' || creditrating[0] != 'b')
    {
    printf ("%s \n", creditrating);
    printf ("Only these letters are valid: g, G, b, B \n");
    printf ("How is the applicant's credit rating?[G for good or B for bad]\n");
    scanf ("%s", &creditrating);
    }
    }while (creditrating[0] != 'g' || creditrating[0] != 'b');
    /////////////////////////////////////////////

    the program doesn't ever break out of the loop even when 'g' or 'b' are entered.

    I don't understand while the != will not compare when I add the or || operator to it????

    Can anyone help me out here, I need to only allow letters g, G, b, B????


    Jose

  2. #2
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    > while (creditrating[0] != 'g' || creditrating[0] != 'b');

    If I enter 'g', then it's not 'b', so it'll stay in the loop, and vice versa. Try and work from there.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    21

    how to validate more than one code

    That makes sense but how do I validate for more than one code?

    Jose

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    21

    I got it.

    I got it i changed the || to && and it worked fine.
    Thanks.

    Jose

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 03:27 AM
  2. Where's the EPIPE signal?
    By marc.andrysco in forum Networking/Device Communication
    Replies: 0
    Last Post: 12-23-2006, 08:04 PM
  3. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 11:28 PM
  4. Binary Tree, couple questions
    By scoobasean in forum C Programming
    Replies: 3
    Last Post: 03-12-2005, 09:09 PM
  5. Replies: 1
    Last Post: 07-31-2002, 12:35 PM

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