Thread: validating with a char.

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    7

    validating with a char.

    im having problems with strings or char's in c.

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    #include <stdlib.h>
    
    void main (void)
    {
    	int mark = 0;
    	char grade;
    do
    	{
    printf("what was your exam mark: ");
    scanf("%d",&mark);
    	}
    while( (mark < 0 ) || (mark >100));
    
    do
    	{
    	printf("what was your grade : ");
    	scanf("%c",&grade);
    	}
    	while((grade !='a' )||(grade !='b')||(grade !='c'));
    	clrscr(); 
    	gotoxy(14,14);
    	printf("your mark was %d and your grade was %c",mark , grade);
    	fflush(stdin);
    	getchar();
    }
    What i am wondering is am i declaring the char right to be able to test it. Im really looking to Validate the input so people can only enter a,b or c.
    does anyone have a better way?
    im pretty new to this and have read the faq and could not find anything there to help.

    any help would be greatly appreciated.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    fflush(stdin);
    getchar();
    }


    What i am wondering is am i declaring the char right to be able to test it. Im really looking to Validate the input so people can only enter a,b or c.
    does anyone have a better way?
    Looks like you've already answered your own question.

    On a side note, don't flush input streams. Theoreticly it could crash your system. Realisticly, it's undefined behaviour, and as such, you should never use it.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User FCF's Avatar
    Join Date
    Dec 2001
    Posts
    40
    Hi
    Pls examine this:
    while( (mark < 0 ) || (mark >100));
    How abt if the user enter 25? Pls think.

    while((grade !='a' )||(grade !='b')||(grade !='c'));
    Lets say the user enters a? a is not b and a is not c(both TRUE), pls re-consider the condition...U can use various methods to validate, eg: &&...
    Life is difficult...

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    7
    n a side note, don't flush input streams. Theoreticly it could crash your system. Realisticly, it's undefined behaviour, and as such, you should never use it.
    I was reading that in the faq. I have to include it because all the work has to be submitted and that is waht they wanna see.

    im gonna have a good read and ask my lecturer about it.

    and back to my original question. so when you say i have answered my own question do you mean there is nothing wrong with the code because i keep getting errors when i actually try to validate something.

    thanks for the help as well.

    edit: sorry im very confused know i though if i entered 25 it would stop the loop which is what im after. it is just to validate the input to make sure it is between 0 and 100.
    Last edited by 4play; 05-22-2003 at 07:58 PM.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by 4play
    so when you say i have answered my own question do you mean there is nothing wrong with the code because i keep getting errors when i actually try to validate something.

    thanks for the help as well.
    No. I mean, if you don't want to use scanf, why don't you just use getchar? After all, that's what it's for; reading a character.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    20
    Try this out:

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <ctype.h>          //you need this for the tolower function
    
    int main () //start with int main NOT void main 
                //(don't really know why, just ask the experts here!)
    {
    	int mark = 0;
    	char grade;
    do
    	{
    printf("what was your exam mark: ");
    scanf("%d",&mark);
    	}
    while( (mark < 0 ) || (mark >100));   //this is OK, don't know y FCF  said otherwise
    
    while(1)                //infinite loop
    	{
    	printf("what was your grade : ");
    	grade=getchar();
    	
                    if(tolower(c)=='a' )||(tolower(c)=='b')||(tolower(c)=='c'))
                    {
                     break;
                    }
             }
    	clrscr(); 
    	gotoxy(14,14);
    	printf("your mark was %d and your grade was %c",mark , grade);
    	fflush(stdin);
    	getchar();
    }

  7. #7
    Registered User
    Join Date
    May 2003
    Posts
    7
    @ James00 you are a star mate. i was not expecting somone to take the time out to actually write the changes for me but you did.

    Big thanks to everyone that helped. this forum seems a excellent place to learn i think im gonna stick around.

  8. #8
    Registered User FCF's Avatar
    Join Date
    Dec 2001
    Posts
    40


    Sorry for the mistake..

    Pls examine this:
    while( (mark < 0 ) || (mark >100));
    How abt if the user enter 25? Pls think.


    I feel sleepy...
    Life is difficult...

  9. #9
    Registered User
    Join Date
    May 2003
    Posts
    7
    Originally posted by FCF


    Sorry for the mistake..

    Pls examine this:
    while( (mark < 0 ) || (mark >100));
    How abt if the user enter 25? Pls think.


    I feel sleepy...
    lol same here is is 3:30 in the morning and im all out of coke.

  10. #10
    Registered User FCF's Avatar
    Join Date
    Dec 2001
    Posts
    40
    Im here 11am. But i woke up 5:45am to rush for a lecture class, just after i finished the class, i realised i should not have attended the class...
    Life is difficult...

  11. #11
    Registered User
    Join Date
    Apr 2003
    Posts
    20
    Glad that I could help you out 4play!!! Anyway, I really recommend that you visit here often, and ask for help here when needed, it really is a great forum with lots of guys with a lot of experience. I have also started programming C a couple of months ago, and I have found answers to all my questions here. Hope that you do come here often!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM
  3. Conversion Char To Char * Problem
    By ltanusaputra in forum Windows Programming
    Replies: 3
    Last Post: 03-01-2008, 02:06 PM
  4. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 05:59 AM
  5. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM