Thread: help with understanding ascii chart

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    157

    help with understanding ascii chart

    hi i want to loop while a users input is not a lower case character and i was wondering how i could do this ,i tries using the ascii chart but its not working.what exactly am i doing wrong?

    Code:
    if (x < 97 || x > 122)
    	    {
    		 printf("invalid input\nplease input a lower chase character");
    		}

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    That check is correct. Your error must be elsewhere - how are you obtaining the input characters? Please show more of the code.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    157
    i input using the scanf function
    Code:
    getchar();
     printf("\n YOU GUESS : ");
     scanf("%c",&guess);

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    157
    guess = x btw

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by africanwizz View Post
    i input using the scanf function
    Code:
    getchar();
     printf("\n YOU GUESS : ");
     scanf("%c",&guess);
    Same old problem. Add one space before the %c, so newline chars left in the keyboard buffer, (input stream in this case), will be passed over.

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    157
    it still doesnt work

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by africanwizz View Post
    it still doesnt work
    You need to add some more logic to get the kind of user input you want.

    Consider:
    Code:
     pseudo code
    ok is an int.
    do   //start of do while loop
    
       ok=1;    //set a logic switch assume input will be ok
       get x from the user
       if(x <'a' || x >'z') {   //what you have but clearer
             printf("invalid input\nplease input a lower chase character\n");
             ok=0; //set logic switch to not ok
       }
    while(!ok);
    Try something like that.

  8. #8
    Registered User
    Join Date
    Nov 2012
    Posts
    157
    thanks ,that worked

  9. #9
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by africanwizz View Post
    thanks ,that worked
    You can also write

    if (!islower(x))
    // ... foo ...

    This function and others like it are in the header file ctype.h and they are guaranteed to work even if you port to a system that doesn't use ascii.

  10. #10
    Registered User
    Join Date
    Nov 2012
    Posts
    157
    thanks,ill be sure to try that out!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple Bar Chart.
    By lovelace in forum C Programming
    Replies: 18
    Last Post: 09-19-2011, 03:28 PM
  2. Flow Chart
    By strokebow in forum C++ Programming
    Replies: 2
    Last Post: 11-13-2008, 08:57 AM
  3. ASCII chart
    By arul in forum C Programming
    Replies: 2
    Last Post: 07-29-2008, 08:02 AM
  4. Chart!
    By 98dodgeneondohc in forum C Programming
    Replies: 13
    Last Post: 04-22-2005, 10:03 AM
  5. outputting a ASCII chart
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 02-15-2002, 11:55 AM

Tags for this Thread