Return true if character capital letter!

This is a discussion on Return true if character capital letter! within the C Programming forums, part of the General Programming Boards category; I've wrote the following piece of code but i haven't quite got the while statement to terminate at the right ...

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    5

    Return true if character capital letter!

    I've wrote the following piece of code but i haven't quite got the while statement to terminate at the right place, i would appreciate any help, thanks. The aim was to get the sequence of characters entered to terminate by a full stop.

    Code:
    #include <stdio.h>
    
    	void main(void) {
    	   int ch;
    	       	   	
    	   printf("Please enter a letter, terminated by a full stop? "); 
               ch = getchar();
               
               while (ch != '.'){
             
               if (ch >= 'A' && ch <= 'Z') 
    	   printf("true \n", ch); 
    	   else    printf("false\n"); } 
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >void main(void) {
    No. main always returns an int.

    >if (ch >= 'A' && ch <= 'Z')
    This is okay if you only intend to use ASCII, but keep in mind that this test is not portable. A better option would be to test using isalpha and isupper from ctype.h.

    Your problem is that you don't call getchar again inside your loop. This can be fixed by either doing so, or using a call to getchar as the condition of the while loop for a more elegant method:
    Code:
    #include <stdio.h>
    
    int main ( void )
    {
      int c;
    
      printf ( "Enter letters, . to quit\n" );
      while ( ( c = getchar() ) != '.' ) {
        if ( c >= 'A' && c <= 'Z' )
          printf ( "True\n" );
        else
          printf ( "False\n" );
      }
    
      return 0;
    }
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    5
    Thanks, it works better now, but for some reason it returns true or false twice, instead of once. For example when i enter a small letter it will return:
    false
    false

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    Post the code you are using.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Originally posted by JamesMontgomery
    Thanks, it works better now, but for some reason it returns true or false twice, instead of once. For example when i enter a small letter it will return:
    false
    false
    The newline character '\n' is the extra false.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    Good call, Dave.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  7. #7
    Registered User
    Join Date
    Nov 2003
    Posts
    5
    This is the code I am using. I've tried removing the newline character "\n" but it still returns 2 false's just this time on the same line. e.g.falsefalse

    [code]
    #include <stdio.h>

    int main ( void )
    {
    int c;

    printf ( "Please enter a letter, . to quit: " );
    while ( ( c = getchar() ) != '.' ) {
    if ( c >= 'A' && c <= 'Z' )
    printf ( "True\n" );
    else
    printf ( "False\n" );
    }

    return 0;
    }
    [code]

  8. #8
    Registered User
    Join Date
    Nov 2003
    Posts
    5
    This is the output i keep getting:

    Please enter a letter, . to quit: p
    False
    False
    T
    TrueFalse.

    What I want is one False to be returned when a small letter is entered e.g.(p- as above) and one True to be returned when a capital letter is entered e.g.(T- as above)

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    I've tried removing the newline character "\n" but it still returns 2 false's just this time on the same line. e.g.falsefalse
    I was referring to the newline in the input stream -- the one that is there because you pressed enter/return/whatever. It will be presented to your program as the 'c' variable. If you want to ignore it, try something like this.
    Code:
    #include <stdio.h>
    
    int main ( void )
    {
       int c;
       printf ( "Enter letters, . to quit\n" );
       while ( ( c = getchar() ) != '.' )
          if ( c >= 'A' && c <= 'Z' )
             printf ( "True\n" );
          else if ( c != '\n' )
             printf ( "False\n" );
       return 0;
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  10. #10
    Registered User
    Join Date
    Nov 2003
    Posts
    5
    That works great, thanks very much!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 10:00 PM
  3. Simple thread object model (my first post)
    By Codeplug in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2004, 10:34 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. problem with open gl engine.
    By gell10 in forum Game Programming
    Replies: 1
    Last Post: 08-21-2003, 04:10 AM

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