C++ help

This is a discussion on C++ help within the C++ Programming forums, part of the General Programming Boards category; how to write a program to enter a string and count the number of words whose first letter is 'a' ...

  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
    21,893
    #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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,308
    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

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