Thread: Problem with my program i cant figure out...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    17

    Problem with my program i cant figure out...

    Ive been trying to get this program to work for the past two days and i cant seem to find whats wrong with it. bear, with me if its very obvious for you guys but a little hint would be much appreciated.

    the purpose of this program is to check if user inputted character is a vowel or consonant in the Hawaiian language. the program is split into three different files, a header file, a .c file for two functions, and a driver.c file to test the functions. oh, and also a makefile to execute the program.

    here is the header file
    Code:
    /* header file for letters.c */
    
    int is_vowel(char ch);
    
    int is_consonant(char ch);
    
    #define TRUE 1
    #define FALSE 0
    and the .c file for the int is_vowel and int is_consonant functions.
    Code:
    /* functions that test if a character is a vowel or consonant in the hawaiian language. */
    
    #include "letters.h"
    
    int is_vowel(char ch)
    {
            switch(ch)
             {
                    case 'a': return TRUE;
                            break;
                    case 'A': return TRUE;
                            break;
                    case 'e': return TRUE;
                            break;
                    case 'E': return TRUE;
                            break;
                    case 'i': return TRUE;
                            break;
                    case 'I': return TRUE;
                            break;
                    case 'o': return TRUE;
                            break;
                    case 'O': return TRUE;
                            break;
                    case 'u': return TRUE;
                            break;
                    default: return FALSE;
            }
    }
    
    int is_consonant(char ch)               
    {
            switch(ch)
             {
                    case 'h': return TRUE;
                            break; 
                    case 'H': return TRUE;
                            break; 
                    case 'k': return TRUE;
                            break;
                    case 'K': return TRUE;
                            break;
                    case 'l': return TRUE;
                            break; 
                    case 'L': return TRUE;
                            break; 
                    case 'm': return TRUE;
                            break; 
                    case 'M': return TRUE;
                            break; 
                    case 'n': return TRUE;
                            break; 
                    case 'N': return TRUE;
                            break; 
                    case 'p': return TRUE;
                            break; 
                    case 'P': return TRUE;
                            break; 
                    case 'w': return TRUE;
                            break; 
                    case 'W': return TRUE;
                            break; 
                    case '`': return TRUE;
                            break; 
                    default: return FALSE;
            }
    }
    and the driver.c to test the two functions
    Code:
    /* driver to test letters.c */
    
    #include "letters.h"
    #include <stdio.h> 
    #define FLUSH while(getchar() != '\n');
    
    main()
    {
       char ch;
    
            printf("Please enter a Hawaiian letter to be tested for being either a vowel or consonant:");
            
            while( (ch = getchar()) != EOF )
             {   
                    if( is_vowel(ch) == TRUE )
                      printf("The character is a vowel\n");
                    else
                      printf("The character is not a vowel or consonant\n");
    
                    if( is_consonant(ch) == TRUE )
                      printf("The character is a consonant");
                    else
                      printf("The character is not a vowel or consonant\n");
            
                    printf("Please enter the next character to be tested\n");
            FLUSH
            } 
    }
    the problem i am having is whenever i compile and run the program and type in say the vowel "a", the program prints both "the character is a vowel" and "The character is not a vowel or consonant" statement. it does the same if the letter is a consonant.

    ....idk whats wrong????

    Edit: just noticed, sorry for using an implicit main.
    Last edited by youareafever; 10-31-2008 at 01:06 AM.

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    HINT: Why do you need to check if a character is a consonant if you've already ascertained that it is a vowel?

    *EDIT*

    I've just read your printf's now... Change the false printf's to be "The character is not a vowel" for checking vowels, "The character is not a consonant",
    checking consonants.
    Last edited by The Dog; 10-31-2008 at 01:25 AM.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    17
    ok now i feel silly, for posting this and having trouble on it for such a long time...i saw wat my problem was and just ended up writing a new driver.

    Code:
    /* driver2.c */
    
    #include "letters.h"
    #include <stdio.h>
    #define FLUSH while(getchar() != '\n');
    
    
    main()
    {
      char ch;
    
      printf("please enter a letter to be tested:");
    
      while((ch = getchar()) != EOF)
      {
            if(is_vowel(ch) == TRUE)
              printf("The character is a vowel.\n");
                
            if(is_consonant(ch) == TRUE)
            printf("The character is a consonant.\n"); 
            
            if(is_vowel(ch) == FALSE && is_consonant(ch) == FALSE)
            printf("The character is not in the Hawaiian alphabets.\n");
            
            printf("Please enter the next letter to be tested:");
      
      FLUSH
      }
    }

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    17
    thanks for your help dog.

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Furthermore... why doesn't your code look like this?

    Cleaner:
    Code:
    /* functions that test if a character is a vowel or consonant in the hawaiian language. */
    
    #include "letters.h"
    
    int is_vowel(char ch)
    {
            switch(ch)
             {
                    case 'a':
                    case 'A':
                    case 'e':
                    case 'E':
                    case 'i':
                    case 'I':
                    case 'o':
                    case 'O':
                    case 'u':
                            return TRUE;
                    default:
                            return FALSE;
            }
    }
    
    int is_consonant(char ch)               
    {
            switch(ch)
             {
                    case 'h':
                    case 'H':
                    case 'k':
                    case 'K':
                    case 'l':
                    case 'L':
                    case 'm':
                    case 'M': 
                    case 'n':
                    case 'N': 
                    case 'p':
                    case 'P': 
                    case 'w':
                    case 'W':
                    case '`':  // <--- what the ??? whatever.. its your code
                            return TRUE; 
                    default: return FALSE;
            }
    }
    Though that hardly enumerates all the consonants, plus it uses the ` char.... perhaps you are trying to add international support or something....

    Just so you are aware, a return statement leaves the function. A break breaks out of the switch. Ergo, anything after a return will never be reached and is therefore superfluous.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    And another question begs to answer: why are you using implicit main?
    http://cpwiki.sourceforge.net/Implicit_main
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I think you should write your is_consonant() function more like:

    Code:
    int is_consonant(int ch)
    {
      return ( (isalpha(ch) && !is_vowel(ch)) || (ch == '`'));
    }

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    17
    Eylsia: i know, you mentioned that in my previous thread. its just that my professor has already scolded me about using things in my code we haven't gone over yet and actually docked points from my grade for doing so. arguing with him is pointless as my TA even admits, hes the kinda person that has to always be right.

    master5001: didn't know if u caught it but these function are for a larger program meant to spell check hawaiian words. in the hawaiian language there are only 13 letters used, 5 vowels (excluding Y) and 8 consonants. the ' character is considered a consonant and in hawaiian is referred to as the okina.

    the reason for all the breaks is mostly for practice.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program problem
    By Birdhaus in forum C++ Programming
    Replies: 6
    Last Post: 11-21-2005, 10:37 PM
  2. Replies: 2
    Last Post: 04-25-2005, 11:59 AM
  3. Some Problem With My Program
    By Americano in forum C Programming
    Replies: 5
    Last Post: 10-18-2003, 01:58 AM
  4. problem with 2d array program.
    By Niloc1 in forum C Programming
    Replies: 1
    Last Post: 04-08-2002, 05:47 PM
  5. Console Program Problem
    By Breach23 in forum C++ Programming
    Replies: 3
    Last Post: 10-19-2001, 12:35 AM