Thread: C++ help

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    2

    C++ help

    how to write a program to enter a string and count the number of words whose first letter is 'a'
    this is what i got...

    #include<iostream.h>
    #include<ctype.h>
    #include <string.h>
    # include <stdio.h>
    void main ()
    {
    char str[60];
    int
    cout<<"enter string";
    gets (str);
    int n=strlen (str)
    for (int i=0;i<n;i++)
    if (is space (str[i])
    {
    if (str[i+1]=='a'|| str[i]=='A')
    a++
    }
    break;
    cout<<"/n no of words starting with a"<<a;
    }

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    I don't think you wrote this. It seems like a problem given by a professor where you have to correct your mistakes. And actually, there are quite a few, some of which you should be able to spot with fairly little experience. So try to fix it yourself, at least, and maybe we may help you if you show us you're at least going to try doing this.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    2
    a professor gave me the question...bt not the answer...that is why i have tried it on my own...its not a "find the error question"

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    #inclue <iostream>, not <iostream.h>. Then, #include <string>, and read into std::string objects in a loop. For each string, find an 'a'. If there is an 'a', increment a count.
    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

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by starlitzz View Post
    a professor gave me the question...bt not the answer...that is why i have tried it on my own...its not a "find the error question"
    I'm really having a hard time believing this. If you would know enough to write what is written there, then you should know enough to fix most of the issues there as well...

    But okay, show us a try on fixing this. If there's a specific issue, we might be able to help. But we won't do your homework for you.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Why write so much code without attempting to compile?
    How about reading the compiler error messages and using some of those hints to fix the easiest bugs?
    Then you could post some of the remaining error messages.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed