Thread: Why is this C program giving the wrong output?

  1. #1
    Registered User
    Join Date
    May 2018
    Posts
    11

    Why is this C program giving the wrong output?

    Code:
    #include<string.h>
    #include<stdio.h>
    int main()
    {
    
    
        char s[10];
        fgets(s,10,stdin);
        func(s);
        return 0;
    }
    void func(char a[])
    {
        int i, c1, c2;
        while(a[i]!='\0')
        {
            if((a[i]>=65 && a[i]<=90) || (a[i]>=97 && a[i]<=122))
            {
                if(a[i]=='A' || a[i]=='I' || a[i]=='O' || a[i]=='U'|| a[i]=='E' || a[i]=='a' || a[i]=='i' || a[i]=='o' || a[i]=='u'|| a[i]=='e')
                {
                    c1++;
                }
                else
                {
                    c2++;
                }
            }
            i++;
        }
        printf("Consonants: %d Vowels: %d", c2, c1);
    }

  2. #2
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    What are the initial values of i, c1, and c2?

  3. #3
    Registered User
    Join Date
    May 2018
    Posts
    11
    Quote Originally Posted by christop View Post
    What are the initial values of i, c1, and c2?
    If we initialize c1=0, c2=0;
    Still the answer is incorrect for any string that includes one or more blank spaces.
    Last edited by sunil9211; 05-01-2018 at 12:02 PM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You should attempt to answer christop's question, because the answer is the reason for your problem.
    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.

  5. #5
    Registered User
    Join Date
    May 2018
    Posts
    11
    Quote Originally Posted by sunil9211 View Post
    If we initialize c1=0, c2=0;
    Still the answer is incorrect for any string that includes one or more blank spaces.
    You're on the right track, but haven't gone quite far enough yet. Uninitialized variables contain whatever garbage is in memory (they aren't zero-ed out when created) and that garbage is causing your program to break. You've got *three* variables.

  6. #6
    Registered User
    Join Date
    May 2018
    Posts
    11
    I agree with you, but ever after initializing the variables it's giving wrong output. Why?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by sunil9211
    I agree with you, but ever after initializing the variables it's giving wrong output. Why?
    It works for me, so I don't know what you're talking about.

    (In other words, if it is "giving wrong output", you should state your input, expected output, actual output, and why you expect that output given that input.)
    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

  8. #8
    Registered User
    Join Date
    May 2018
    Posts
    11
    It works now. I forgot that function gets() will read only 9 char long string as its size is 10.
    Thank you everyone!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help! always getting wrong answer.
    By V8cTor in forum C++ Programming
    Replies: 2
    Last Post: 07-10-2015, 12:22 AM
  2. c program with the wrong answer
    By adhamhamade in forum C Programming
    Replies: 4
    Last Post: 03-24-2014, 09:18 AM
  3. Always give me wrong answer
    By amroto in forum C Programming
    Replies: 8
    Last Post: 03-29-2012, 09:43 AM
  4. Pls give me the answer
    By meazeem in forum C Programming
    Replies: 2
    Last Post: 03-10-2011, 04:29 AM
  5. Wrong answer?
    By eViLrAcEr in forum C++ Programming
    Replies: 5
    Last Post: 07-16-2009, 06:04 AM

Tags for this Thread