Thread: Need a little help on a simple program.

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    8

    Need a little help on a simple program.

    The program should find and delete all vowels in a word that is user entered. This is what I have so far and I know it should be essentially this format I just don't know how to set enteredword and word equal to each other. thanks.



    Code:
    #include <string>
    #include <iostream>
    using namespace std;
    
    
    
    
    void vowelremover(string&);
    string word;
    int main ()
    {
    
    
        //string word;
    
    
        cout << "Enter a word of which you want the vowels removed: ";
        cin >> word;
    
    
        vowelremover(word);
    
    
        cout << "The word without vowels is: " << word << endl;
    
    
        return 0;
    
    
    }
    
    
    
    
    void vowelremover (string& enteredword)
    {
    string enteredword = word;
        int posvowel;
    
    
        posvowel = enteredword.find("a,e,i,o,u,A,E,I,O,U");
    
    
        while (posvowel >= 0 && posvowel < enteredword.length())
        {
            enteredword.erase(posvowel, 1);
            
            posvowel = enteredword.find("a,e,i,o,u,A,E,I,O,U");
        }
    
    
    }

  2. #2
    Time-Lord Victorious! The Doctor's Avatar
    Join Date
    Aug 2012
    Location
    Perth, Western Australia
    Posts
    50
    Line 37.
    Code:
    enteredword = word;
    Is there any reason you need that? Unless I'm mistaken that line sets "enteredword" equal to a variable called "word", which does not exist in that scope (the function). I don't think you need that line, and I think that would cause a compiler error.

    "enteredword" is automatically equal to word (because you passed "word" into the function when you called it, "enteredword" effectively became "word") as far as I can tell. It's a reference to "word". You do not need to worry about it.

    However, C++ is not my area of expertise, as I have only been taught Java and C, and am still learning C++ myself.
    Last edited by The Doctor; 03-25-2013 at 09:19 PM.
    Code:
    if (codeWorks( code) && !youCanDoSomethingBetter( code) )
    {
         beHappy();
    } else
    {
         fixCode( code);
    }

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Actually it's worse than that,

    string enteredword = word;

    this is

    string enteredword(word);

    it shadows the parameter enteredword, meaning that it is hidden and cannot be used until the local variable is renamed.

  4. #4
    Time-Lord Victorious! The Doctor's Avatar
    Join Date
    Aug 2012
    Location
    Perth, Western Australia
    Posts
    50
    I tried to compile your code (removing line 37) and it compiled. It just didn't remove the vowels.

    I suspect your use of some of the string functions is wrong. Perhaps you should look up how those functions should be used.


    Quick Google found this site:
    string - C++ Reference
    Last edited by The Doctor; 03-25-2013 at 09:34 PM.
    Code:
    if (codeWorks( code) && !youCanDoSomethingBetter( code) )
    {
         beHappy();
    } else
    {
         fixCode( code);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple program, simple problem
    By KAUFMANN in forum C Programming
    Replies: 5
    Last Post: 02-16-2011, 01:16 PM
  2. simple program, simple error? HELP!
    By colonelhogan44 in forum C Programming
    Replies: 4
    Last Post: 03-21-2009, 11:21 AM
  3. Simple program...simple problem?
    By deadherorising in forum C Programming
    Replies: 2
    Last Post: 03-12-2009, 08:37 PM
  4. Simple program, not so simple problem
    By nolsen in forum C++ Programming
    Replies: 2
    Last Post: 01-18-2008, 10:28 AM
  5. Need help with simple, simple program.
    By LightsOut06 in forum C Programming
    Replies: 5
    Last Post: 09-01-2005, 08:31 PM

Tags for this Thread