number is showing as 2?

This is a discussion on number is showing as 2? within the C Programming forums, part of the General Programming Boards category; This program is supposed to count the number of letters, punctuations and numbers a user enters. The problem I'm having ...

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

    number is showing as 2?

    This program is supposed to count the number of letters, punctuations and numbers a user enters.

    The problem I'm having is that "letter" is showing as 2 when I only type in 1 letter. If I assign letter to -1, then it all works fine. So what is wrong here?



    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main(void)
    {
        char ch;
        int digit, punc, letter;
        
        printf("Enter number, punctuation or letter: \n");
        
        digit = 0;
        punc = 0;
        letter = 0;
        
        do {
        ch = getche();
        switch(ch) {
        
            case '0':
            case '1':
            case '2':
            case '3':
                 case '4':
                      case '5':
                           case '6':
                                case '7':
                                     case '8':
                                          case '9':
                                               digit++;
                                               break;
                                               
            case '.':
                 case ',':
                      case '?':
                           case '!':
                                case ':':
                                     case ';':
                                          punc++;
                                          break;
                                          default:
                                                  letter++;
                                                  }
                                                  
                                                  } while(ch!='\r');
                                                  
                                                  printf("\nDigits: %d\n", digit);
                                          printf("Punctuation: %d\n", punc);
                                                  printf("Letters: %d\n", letter);
                                                  
                                                  getche();
                                                  }

  2. #2
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    How about you print the ASCII values of the letters you're getting. My bet is there's a newline lingering around in your input buffer.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    5
    I'm not to sure what you mean, I type in the letter 'a' and then press enter. Then it sais:

    letters: 2

  4. #4
    Aia
    Aia is offline
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    Quote Originally Posted by Corleone View Post
    I'm not to sure what you mean, I type in the letter 'a' and then press enter. Then it sais:

    letters: 2
    Exactly, two characters: 'a' and 'newline'.

    char ch should be int ch since you have to make accommodation for the possibility that getche() would return an EOF which is an integer.
    Last edited by Aia; 11-12-2007 at 08:33 PM.

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    5
    Quote Originally Posted by Aia View Post
    Exactly, two characters: 'a' and 'newline'.

    char ch should be int ch since you have to make accommodation for the possibility that getche() would return an EOF which is an integer.
    Okay, I changed char ch to int ch and nothing changed.

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    54
    you need a case '\n': break;

  7. #7
    Registered User
    Join Date
    Nov 2007
    Posts
    5
    Quote Originally Posted by Doodle77 View Post
    you need a case '\n': break;

    Where do I put this?

    I've tried it a couple places and its either changing it so the number value is 0, or 2

  8. #8
    Aia
    Aia is offline
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    Quote Originally Posted by Doodle77 View Post
    you need a case '\n': break;
    [Edit]: right before the default after the punc, break
    Code:
    case '\n':
    case '\r':
         break;
    Last edited by Aia; 11-12-2007 at 09:01 PM.

  9. #9
    Registered User
    Join Date
    Nov 2007
    Posts
    5
    Quote Originally Posted by Aia View Post
    [Edit]: right before the default after the punc, break
    Code:
    case '\n':
    case '\r':
         break;
    thanks you kind user

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with this compiler error
    By Evangeline in forum C Programming
    Replies: 7
    Last Post: 04-05-2008, 09:27 AM
  2. Calculating next prime number
    By anilemon in forum C Programming
    Replies: 8
    Last Post: 04-17-2006, 10:38 AM
  3. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  4. parsing a number
    By juancardenas in forum C Programming
    Replies: 1
    Last Post: 02-19-2003, 12:10 PM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 04:00 PM

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