Thread: new to c programming, Homework help

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    5

    new to c programming, Homework help

    Hello everyone,

    I'm new to this forum as well as c programming. I could use some guidance on a program I am trying to write. I need the program to analyze the below character and print out. I need to use if if-else or switch statements and I am lost.

    • an alphabetic character (a-z or A-Z)
    • a digit (0-9) or
    • a special character (anything else)

    Code:
    #include <stdio.h>
    
    using namespace std;
    
    int main()
    
    {
        char character;
        int number = 0;
        
        printf ("Please enter a character\n"); /*Asks the user to enter a character*/
        scanf("%c",&character);
        
        if (character >= 'a' && character <= 'z') {
            printf("You entered a character from a-z");
            }
               
        if (character >= 'A' && character <= 'Z') {
            printf("You entered a character from A-Z");
            }
            
        if (number >= '0' && number <= '9')
            printf("You entered a digit 0-9\n");
        
     
            else  {
            printf("You entered a special character");
            }
            
        
        fflush(stdin); 
        getchar();
        return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    If Statements in C - Cprogramming.com


    FAQ > Why fflush(stdin) is wrong - Cprogramming.com

    I suggest adding an space in scan format to skip spaces (linefeeds etc.).
    Code:
    scanf(" %c",&character);
    Tim S.

  3. #3
    spaghetticode
    Guest
    Grab a sheet of paper, a pen, and go through your programm. Write down step by step exactly what will happen if your input is, say, 'b'. Don't rush through your code writing down what you believe it does; take your time to note down what it actually does in every single line.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by esardinha08 View Post
    Hello everyone,

    I'm new to this forum as well as c programming. I could use some guidance on a program I am trying to write. I need the program to analyze the below character and print out. I need to use if if-else or switch statements and I am lost.

    • an alphabetic character (a-z or A-Z)
    • a digit (0-9) or
    • a special character (anything else)

    Code:
    #include <stdio.h>
    
    using namespace std;
    
    int main()
    
    {
        char character;
        int number = 0;
        
        printf ("Please enter a character\n"); /*Asks the user to enter a character*/
        scanf("%c",&character);
        
        if (character >= 'a' && character <= 'z') {
            printf("You entered a character from a-z");
            }
               
        if (character >= 'A' && character <= 'Z') {
            printf("You entered a character from A-Z");
            }
            
        if (number >= '0' && number <= '9')
            printf("You entered a digit 0-9\n");
        
     
            else  {
            printf("You entered a special character");
            }
            
        
        fflush(stdin); 
        getchar();
        return 0;
    }
    Lose line 3 ... C does not support namespaces.

    If your compiler does not give you an error, you are building a C++ program, not a C program...
    And yes it does matter.

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    The variable "number" is not initialized. You can store digits in "char", as '0'-'9' = 0x30-0x39 in ASCII, so there's no point to have more than one type.

    Also, lose the scanf and use getchar() or fgetc().

  6. #6
    Registered User
    Join Date
    Dec 2011
    Posts
    5

    new to c programming, Homework help

    Thanks for the feedback everyone, much appreciated. The program seems to work except when I enter a numeric value such as 5, it says I entered a special character. How can I get it to say I entered a numeric value and for anything else display a special character.

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    If your code is still the same as in post #1, notice that you read into "character" on line 12 (which is fine, even if it is a digit) but then test "number" on line 22.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Registered User
    Join Date
    Dec 2011
    Posts
    5

    new to c programming, Homework help

    Thanks all!

    My final draft is:
    Code:
    #include <stdio.h>
    
    int main()
    
    {
        char character;
        
        printf ("Please enter a character\n"); /*Asks the user to enter a character*/
        scanf("%c",&character);
        
        
        if (character >= 'a' && character <= 'z') {
            printf("You entered a character from a-z");
            }
               
        else if (character >= 'A' && character <= 'Z') {
            printf("You entered a character from A-Z");
            }
            
        else if (character >= '0' && character <= '9') {
            printf("You entered a number from 0-9\n");
            }
     
        else {
            printf("You entered a special character");
             }
            
        
        fflush(stdin); 
        getchar();
        return 0;
    }

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You might want to look up the functions in the standard header, <ctype.h>. There are functions like isalpha(), isnum(), isupper(), islower() that can simplify your code further.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    And you totally ignored the link above detailing why
    Code:
    fflush(stdin);
    is wrong.

  11. #11
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Quote Originally Posted by grumpy View Post
    You might want to look up the functions in the standard header, <ctype.h>. There are functions like isalpha(), isnum(), isupper(), islower() that can simplify your code further.
    This. Here's a link to these functions: C character classification - Wikipedia, the free encyclopedia
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C programming homework (SHUFFLING A DECK OF CARD)
    By arm3103 in forum C Programming
    Replies: 11
    Last Post: 10-16-2011, 03:22 PM
  2. What should be the Homework for game Programming?
    By mistu4u in forum Game Programming
    Replies: 7
    Last Post: 06-16-2011, 12:36 AM
  3. Homework
    By cagurtay in forum C Programming
    Replies: 3
    Last Post: 11-01-2010, 05:28 PM

Tags for this Thread