Thread: last part of program i hope to validate

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    33

    last part of program i hope to validate

    hi all,

    have been troubling people here these few days, im finishing my program soon and hope to get the documentation done by end of this week. I have tested my program and theres one last thing left to do,,, validating my yes and no option...i went thru the forum and couldnt get my program to validate Y/N, if i use strcmp, i will need to change to %s, is there anyway to add a code so that %c can be validate to y and n only, thank you all


    Code:
    void add_rec(void)
    {
     	char add_continue;
     	char ans;
     	add_continue = 'Y';
     	
    
     	do
     	{
    
     	gotoxy(26,23);
     	printf("Write to File (Y/N)?");
     	scanf("%c", &ans);
    	fflush(stdin);
    
    
    	if (toupper(ans) == 'Y');
     	{
    		mem[count] = mem_temp; /*transfer temp data to array*/
    		++count;
     	}
     		gotoxy(24,24);
     		printf("Enter next record (Y/N)?");
     		scanf("%c", &add_continue);
    		fflush(stdin);
    
    
     	}
     	while (toupper(add_continue) == 'Y');
    
    
    	}

  2. #2
    Addicted to the Internet netboy's Avatar
    Join Date
    Dec 2001
    Posts
    158
    How about this:

    Code:
    do{
         printf("Write to file? (Y/N) ");
         scanf("%c", &ans);
         if(ans='Y' || ans='y')
         {
              mem[count] = mem_temp;
              ++count;
              printf("Enter next record (Y/N)?");
              scanf("%c", &add_continue);
         }
         else
              break;
    }while(add_continue='Y' || add_continue='y');
    Last edited by netboy; 07-09-2002 at 09:29 AM.
    It's unfulfilled dreams that keep you alive.

    //netboy

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    33
    it seem like doesnt work
    i press h still able to continue

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    85
    #define CLEAN while (getchar() != '/n')

    void add_rec(void)
    {
    char add_continue;
    char ans;
    /*add_continue = 'Y'; */

    do
    {
    gotoxy(26,23);
    printf("Write to File (Y/N)?");
    scanf("%c", &ans);
    CLEAN; /*fflush(stdin);*/

    if (toupper(ans) == 'Y') /*you need remove the simicolon ';' here/*
    {
    mem[count] = mem_temp; /*transfer temp data to array*/
    ++count;
    }
    gotoxy(24,24);
    printf("Enter C(ontinue) for Next record?");
    scanf("%c", &add_continue);
    CLEAN; /*fflush(stdin}*/
    } while (toupper(add_continue) == 'C');

    }
    Hope it helps!
    DV007

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    33
    Actually i did try copy the whole code from netboy over to my program just now, it din seem to work, especially the y/n part, i din have time to work on it at the moment, its 3am in the morning, just found that i have not write the validation for the update case switch, hmmm time runnng short for me, maybe will skip a few validation to do documentation

    anyway i will try your code tomorrow, see if it works. i was wondering if the y/n is limited to y/n? what if the user enter other characters, will it still accept? coz from the way its coded, i did not see any error msg regarding if user enter the wrong character. what i hope to attain is to only limit user to enter y or n, if others are enter, error msg will appear

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Study this version... it contains a GetYesNo() function where the user is asked to enter Y or N.

    It also allows the user to enter any word starting with a Y or N (eg Yes). It'd be easy enough to put a check in for additional characters if you wanted to.
    Code:
    #include <stdio.h>
    
    #define YES 1
    #define NO  0
    
    int GetYesNo(char *);
    
    int main(void)
    {
        int Choice;
    
        Choice = GetYesNo("Would you like an icecream (y/n)? ");
    
        if (Choice == YES)
            printf("OK... here's chocolate one...\n");
        else
            printf("OK... bye then\n");
    
        return(0);
    }
    
    int GetYesNo(char *prompt)
    {
        /*
    	 *  This function will only return once the user
    	 *  has entered Y or N.
    	 */
        int c;
        int rc = -1;
    
        while (rc < 0)
        {
            printf("%s", prompt);
            c = getchar();
            switch (c)
            {
            case 'y':
            case 'Y':
                rc = YES;
                break;
            case 'n':
            case 'N':
                rc = NO;
                break;
            default:
                if (c != '\n')
                    while (getchar() != '\n');
                break;
            }
        }
    
        if (c != '\n')
            while (getchar() != '\n');
        return(rc);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Addicted to the Internet netboy's Avatar
    Join Date
    Dec 2001
    Posts
    158
    Actually I have been using the method that I posted earlier to validate any Y/N questions in my program...

    I'm not sure why it won't work in your program. Can you try adding fflush(stdin); after the scanf statement?

    Also if adding fflush statement still doesn't work, try using a string variable (%s) instead of single character variable (%c)... note that when scanning string to a %s variable, there will NOT be a '&' sign in front of the variable.

    Tell me your result.
    It's unfulfilled dreams that keep you alive.

    //netboy

  8. #8
    Registered User
    Join Date
    May 2002
    Posts
    33
    Thanks hammer, i look thru the code seem this may be what i want, will try to put it in use.

    Netboy: thanks for your help too, somehow the #define CLEAN while (getchar() != '/n') => i think is '\n' rite?

    coz both ways i tried, it will go to the next line when i did not key anything, and after that if i hit enter it will automatically accept it as a Yes,

    seem like i have more to test, i think my switch case have some slight problem, may need to modify again.

    thanks all

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >I'm not sure why it won't work in your program. Can you try adding fflush(stdin); after the scanf statement?
    No, don't that! fflush(stdin) is undefined, as the function is for out streams only, not input ones.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [Help] Program : How to validate credit card number
    By kingofdcp in forum C Programming
    Replies: 13
    Last Post: 10-31-2009, 12:58 AM
  2. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  3. why the control goes to else part in this program
    By vapanchamukhi in forum C Programming
    Replies: 2
    Last Post: 01-13-2009, 07:09 AM
  4. Replies: 2
    Last Post: 11-18-2006, 03:31 AM