Thread: help...Lower and Upper case problem only

  1. #1
    Registered User j4k50n's Avatar
    Join Date
    Apr 2007
    Posts
    14

    help...Lower and Upper case problem only

    i've got a program that will ask for a person initials and only in capital letters...
    and if you were to give a lower case then the program will end
    here is the program
    Code:
    #include <stdlib.h>
    #include <ctype.h>
    
    int main(int argc, char ** argv)
    {
    // Mainline Variable Declarations
    FILE * output = stdout;
    FILE * input = stdin;
    
    char FName;
    char toupper;
    
    	//Enter first name
    	fprintf(output,"--------------------------\n"); fflush(output);
    	fprintf(output,"Enter Your first initial name in capital letters: "); fflush(output);
    
    	//input first name
    	fscanf(input,"%c",&FName);fflush(output);
    
    	if(!(FName == toupper))
    	{
    		fprintf(output,"Error\n",FName);
    		//Exit Program if its not M/F/-
    		exit(0);
    	}
    but the problem is if i put lower or upper case it will end program anyway...
    please help...thxxx

  2. #2
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    toupper() is a function that converts its argument to upper case, as you can read here. What you need is isupper().

    And since I'm in a good mood, I'll explain how to interpret its prototype:

    Code:
    int isupper ( int c );
    First, let's check the argument: "int c". First, you might think that an integer as argument is a little weird, but if you read some books/tutorials, you'd probably know that a character is stored as an integer value.
    As for the return type, it's an integer again. This time, it's a little different and we need to examine first what the return value is: "A value different from zero (i.e., true) if indeed c is an uppercase alphabetic letter. Zero (i.e., false) otherwise."

    That's all we need to know, to use it in our code:

    Code:
    char c = 'a';
    if (isupper(c))
    {
      puts("Uppercase");
    }
    else
    {
      puts("Lowercase");
    }

  3. #3
    Registered User j4k50n's Avatar
    Join Date
    Apr 2007
    Posts
    14
    thank you it works and i learn something more from that...
    but i found out other ways..
    here it is...
    Code:
    #include <stdlib.h>
    #include <ctype.h>
    
    int main(int argc, char ** argv)
    {
    // Mainline Variable Declarations
    FILE * output = stdout;
    FILE * input = stdin;
    
    char FName;
    
    	//Enter first name
    	fprintf(output,"--------------------------\n"); fflush(output);
    	fprintf(output,"Enter Your first initial name in capital letters: "); fflush(output);
    
    	//input first name
    	fscanf(input,"&#37;c",&FName);fflush(output);
    
    	if(!(FName == toupper(FName)))
    	{
    		fprintf(output,"Error\n",FName);
    		//Exit Program if its not upper case
    		exit(0);
    	}

  4. #4
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    Of course there are several possible ways, like the one you mentioned. You could also write the following:

    Code:
    if (c >= 'A' && c <= 'Z')
    {
      puts("Uppercase");
    }
    which would give the same result. But why not use the appropriate function if it exists ?

    Oh and btw:

    !(A == B) is the same as writing (A != B)

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > if (c >= 'A' && c <= 'Z')
    Well you could, if you want to make assumptions about your character set being contiguous (read all about EBCDIC).

    if ( isupper(ch) ) puts("uppercase");
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    Quote Originally Posted by Salem View Post
    > if (c >= 'A' && c <= 'Z')
    Well you could, if you want to make assumptions about your character set being contiguous (read all about EBCDIC).

    if ( isupper(ch) ) puts("uppercase");
    ...
    [abbreviation, Extended Binary Coded Decimal Interchange Code] An alleged character set used on IBM dinosaurs. It exists in at least six mutually incompatible versions, all featuring such delights as non-contiguous letter sequences and the absence of several ASCII punctuation characters fairly important for modern computer languages (exactly which characters are absent varies according to which version of EBCDIC you're looking at). IBM adapted EBCDIC from punched card code in the early 1960s and promulgated it as a customer-control tactic (see connector conspiracy), spurning the already established ASCII standard. Today, IBM claims to be an open-systems company, but IBM's own description of the EBCDIC variants and how to convert between them is still internally classified top-secret, burn-before-reading. Hackers blanch at the very name of EBCDIC and consider it a manifestation of purest evil. See also fear and loathing.
    ...ewwww...

    and I already mentioned isupper() in post #2.

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by j4k50n View Post
    Code:
    	if(!(FName == toupper(FName)))
    	{
    		fprintf(output,"Error\n",FName);
    		//Exit Program if its not upper case
    		exit(0);
    	}
    Wrong. Try entering a non-letter and see what it does. FName == toupper(FName) only implies that the character is an uppercase letter if the character is, in fact, a letter. If you pass a non-letter to toupper() it simply returns the character unchanged.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Calling a variable the same name as a function is a bad idea. If it's a local variable, you can't caLL the function. If it's a global one, it may not compile.
    Code:
    char toupper;
    Just so you know.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User j4k50n's Avatar
    Join Date
    Apr 2007
    Posts
    14
    Quote Originally Posted by brewbuck View Post
    Wrong. Try entering a non-letter and see what it does. FName == toupper(FName) only implies that the character is an uppercase letter if the character is, in fact, a letter. If you pass a non-letter to toupper() it simply returns the character unchanged.
    yeah i actually wanted it to exit the program if it isn't an upper case letter...
    so it works fine with me...thanks for the suggestion though

  10. #10
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    Quote Originally Posted by j4k50n View Post
    yeah i actually wanted it to exit the program if it isn't an upper case letter...
    so it works fine with me...thanks for the suggestion though
    "It works fine for me" should NEVER decide how you write a program, always try to write as perfect as possible, which in this case comes down to using isupper().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Upper case output
    By Duskan in forum C++ Programming
    Replies: 2
    Last Post: 08-18-2007, 11:03 AM
  2. to lower or to upper that is the question!
    By verbity in forum C++ Programming
    Replies: 20
    Last Post: 04-25-2007, 06:42 PM
  3. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  4. upper case to lower case problem
    By Jasonymk in forum C++ Programming
    Replies: 3
    Last Post: 04-27-2003, 05:35 AM
  5. lower to upper
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 07-29-2002, 07:50 PM