Thread: pls help to solve the problem with this program

  1. #1
    Registered User
    Join Date
    Apr 2015
    Posts
    2

    Lightbulb pls help to solve the problem with this program

    Code:
    #include<stdio.h>
    #include<conio.h>
    int main()
    {
        char name[100];
        int i=0,j=0,k=0;
        
        printf("enter the line\n");
        gets(name);
        puts(name);
        
        while(name[i]!='\0')
        {
        
        if( name[i] == "a"||name[i] == "e"||name[i] == "i"||name[i] == "o"|| name[i] == "u" )
        {
        j++;
    }
        else
        {
        k++;
    }
    i++;
    }
    printf("number of vowels=%d",j);
    printf("number of consonants=%d",k);
    
    
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    After I removed the inclusion of <conio.h>, the program compiled. I ran the program, entering "a" as the input, and was presented with this output:
    Code:
    a
    number of vowels=0number of consonants=1
    Since you did not tell us what is the expected output, I conclude that there is nothing wrong with your program, other than:
    • You should not use gets since it is unsafe. fgets could be used as an alternative, but be careful to note that it stores the newline read into the string, if it is read.
    • You should indent your code more consistently.

    So, you should tell us what is the purpose of your program and how does it not work. As part of this, tell us what is your test input, expected output and actual output.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You should tell us how it's not working, along with warnings/error messages you receive.

    Also make sure your formatting and indentation is neat and consistent; i.e.

    Code:
    #include<stdio.h>
    //#include<conio.h>
    
    int main()
    {
        char name[100];
        int i=0,j=0,k=0;
         
        printf("enter the line\n");
        gets(name);
        puts(name);
         
        while(name[i]!='\0')
        {
            if( name[i] == "a"||name[i] == "e"||name[i] == "i"||name[i] == "o"|| name[i] == "u" )
            {
                j++;
            }
            else
            {
                k++;
            }
        
            i++;
        }
    
        printf("number of vowels=%d",j);
        printf("number of consonants=%d",k);
    }
    This line:

    Code:
    if( name[i] == "a"||name[i] == "e"||name[i] == "i"||name[i] == "o"|| name[i] == "u" )
    Note that single characters are denoted with single quotes --> 'a'

    Don't use "gets()" to read strings!

    FAQ > Why gets() is bad / Buffer Overflows - Cprogramming.com
    FAQ > Get a line of text from the user/keyboard (C) - Cprogramming.com

    To make your code more readable, I'd suggesting giving your variables meaningful names, such as "number_of_vowels" and "number_of_consonants".

  4. #4
    Registered User
    Join Date
    Apr 2015
    Posts
    2
    thanks matticus problem solved . my mistake

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. i have a problem in my program that iam not able to solve
    By bahaahathout in forum C++ Programming
    Replies: 7
    Last Post: 04-13-2011, 06:06 AM
  2. Replies: 4
    Last Post: 09-14-2008, 06:17 PM
  3. [NEED HELP] I just can't solve this program problem!
    By frodonet in forum C Programming
    Replies: 7
    Last Post: 09-23-2007, 02:44 PM
  4. can u solve this program :P
    By bawen in forum C++ Programming
    Replies: 1
    Last Post: 07-24-2006, 12:16 PM
  5. Replies: 2
    Last Post: 04-25-2005, 11:59 AM