Thread: Return true if character capital letter!

  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,897
    >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,708
    Post the code you are using.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    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,708
    Good call, Dave.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  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,005
    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, 11:34 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. problem with open gl engine.
    By gell10 in forum Game Programming
    Replies: 1
    Last Post: 08-21-2003, 04:10 AM