Thread: Help needed in validation please.

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    18

    Help needed in validation please.

    Can someone please help me with this code:
    Code:
    int validateSectionLetter(char c)
    {
        
        if (!isalpha(c))
        {
            printf("\n** You entered an invalid section. Please enter a section from A to Y. **\n\n");    
            return 0;
        }
        else if ((c = 'Z') || (c= 'z'))
        {
            printf("\n** You entered an invalid section. Please enter a section from A to Y. **\n\n");    
            return 0;
        }
    
    
        return c;
    }
    
    
    char enterSectionLetter()
    {
        char  c = 0;
        
        do
        {
            printf("Enter Section [A - Y]: ");
            scanf("%d", &c);   
        }
    
    
        while(!validateSectionLetter(c));
        
        printf("The section is: %c", &c);
        return c;    
    }
    When I compile, if I enter a wrong character it will loop and writing the appropriate message, but when I enter a correct character like 'a' the .exe goes crazy.

    Thank you in advance.

  2. #2
    Registered User
    Join Date
    Apr 2012
    Posts
    13
    Attention, you're using the "=" sign to compare two chars, and using "%d" in the scanf.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    18
    Thank you dpitz! Didn't notice it there, the .exe stopped going crazy but I'm still not getting my intended output. Enter section [A-Y] is being printed twice, and the section is returns garbage. :/

  4. #4
    Registered User
    Join Date
    Apr 2012
    Posts
    13
    Your function must return an int, and you return a char.

    The "Enter section [A-Y]" is printed not only twice, but as many characters you type plus one (the '\n' character), so it means that each character is tested in your validateSectionLetter function (I don't know if this is what you want). Look what happens when you type "zzz" and "zaz", for example.

    You'd better use getchar() to scan a char, not scanf. Take a look at this link: www . gidnetwork . com/b-60 . html
    Last edited by dpitz; 04-25-2012 at 08:11 AM.

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    18
    Thank you very much, I did what you said but I'm still not getting the result I'm expecting. The program is asking me to enter section from A to Y as usual, when I enter A it says invalid choice and loops back , I enter A again and the program continues :s

    This is my code so far:

    Code:
    int validateSectionLetter(char c)
    {
    	
    	if ((!isalpha(c)) || (c == 'Z') || (c == 'z'))
    	{
    		printf("\n** You entered an invalid section. Please enter a section from A to Y. **\n\n");	
    		return 0;
    	}
    	
    	return c;
    }
    
    
    char enterSectionLetter()
    {
    	char  c = 0;
    	
    	do
    	{
    		printf("Enter Section [A - Y]: ");
    		c = getchar();
    		while (getchar() != '\n');   
    	}
    	
    	while(!validateSectionLetter(c));
    	return c;	
    }

  6. #6
    Registered User
    Join Date
    Apr 2012
    Posts
    13
    It works here, I typed this into main:

    Code:
    char a = enterSectionLetter();
    printf("\nChar is: %c", a);
    If I execute the program and type A it gives "Char is A".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. validation in c
    By thangarasu1988 in forum C Programming
    Replies: 4
    Last Post: 01-05-2012, 02:42 PM
  2. validation
    By chodmama in forum C Programming
    Replies: 3
    Last Post: 02-20-2006, 01:05 AM
  3. validation
    By blanny in forum C Programming
    Replies: 9
    Last Post: 03-05-2004, 07:43 PM
  4. need help in validation
    By dholman in forum C Programming
    Replies: 2
    Last Post: 01-08-2004, 09:27 AM
  5. Help with validation
    By boontune in forum C++ Programming
    Replies: 8
    Last Post: 01-10-2003, 09:44 AM