Help

This is a discussion on Help within the C++ Programming forums, part of the General Programming Boards category; How do i make this code count the vowels in a word and loop it Code: #include <cstdlib> #include <iostream> ...

  1. #1
    C++ GOD italiano40's Avatar
    Join Date
    Nov 2005
    Posts
    9

    Question Help

    How do i make this code count the vowels in a word and loop it
    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        string word;
        cout<<"Enter a Word\n";
        cin>>word;
        
       int y=0;
        if (word =="a"){
                 cout<<y+1; 
                 }
        else if (word=="e"){
             cout<<y+1; 
             }
        else if (word=="i"){
             cout<<y+1;
             }
        else if (word=="u"){
             cout<<y+1;
             }
             else if (word=="o"){
                  cout<<y+1;
                  } 
    
        system("PAUSE");
        return EXIT_SUCCESS;
    }

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    y has not been declared.

    You'll probably want a for loop to loop through all of the characters in the string.

    And another thing; this code
    Code:
    if(word[0] == 'a') {
    
    }
    if(word[0] == 'e') {
    
    }
    is the same as
    Code:
    if(word[0] == 'a' || word[0] == 'e') {
    
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
        cout<<"Please enter your word\n>>";
        
        string word;
        cin>>word;
        
        
        
        int y = 0;  //make sure you initialise this.
                    //so you must set it to zero
        
        //interate through your string using a for loop
        for(int i=0; i<word.length(); i++) 
        //word.length() automatically finds the length of 
        //the string you have entered
        {
            if(word[i]=='a') //single quotes!
            {
                y = y + 1; //this is how you would increment the counter
            }
            //do other conditions here    
        }
        
        cout<<"There were "<<y<<" in your word";
        cin.get();
        cin.get();
        return 0;
    }

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