Thread: function supposed to strip vowels

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    38

    function supposed to strip vowels

    I'm getting errors saying that Index and Input are undeclared identifiers. They are declared. Suggestions?

    The idea is to create a functions to read a string and output the consonants.


    Code:
    #include <iostream>
    #include <conio.h>
    #include <string>
    using namespace std;
    
    int main()
    {
    	//Step01: Declare Memory Locations
    	string Input;
    	string StripVowels(string);
    	//Step02: Initialize Memory Locations
    	int Index = 0;
    	//Step03: Do the Work
    	//Step03a: Ask for the Characters
    	cout << "Please enter characters: ";
    	cin >> Input;
    	cout << endl;
    	//Step03b: Output consanants
    	cout << StripVowels;
    	//Step04: Wrap up and Exit
    	cout << endl;
    	getch();
    	return 0;
    }
    
    
    string StripVowels(string MyChar)
    {
    	while(Index < Input.size())
    	{
    		if(Input.substr(Index, 1) == "a";
    			Index++;
    		else if(Input.substr(Index, 1) == "e";
    			Index++;
    		else if(Input.substr(Index, 1) == "i";
    			Index++;
    		else if(Input.substr(Index, 1) == "o";
    			Index++;
    		else if(Input.substr(Index, 1) == "u";
    			Index++;
    		else
    		{cout << Input.subst(Index, 1) << endl;
    			Index++;
    		}
    	}
    }

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    They're declared only in main(), not in your other function. If you want to use them in that function, then you ought to pass them in as parameters.

    Variables have scope, places where they are valid. Trying to access a variable when it is out of scope as you are trying to do, should throw a compiler error.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Hi,

    It seems you also have some fundamental misunderstandings about how functions are declared, defined, and called. Here is a simple example:

    Code:
    #include <iostream>
    using namespace std;
    
    void display(int n);
    
    int main()
    {
    	int number = 10;
    	display(number); //calls the display() function
    
    	return 0;
    }
    
    void display(int n)
    {
    	cout<<n<<endl;
    }
    Some things to note:

    1) The first line in blue is the function 'prototype' or the function 'declaration'. It is placed outside of main() and before main().

    2)In main() the function is called with this line:

    display(number);

    Notice that when you call a function, you use paretheses after the function name, and inside the parentheses, you have to include the values the function is expecting. In this case, the variable 'number' is listed inside the parentheses. That is an instruction to fetch the value stored in the variable 'number' and send it on to the function. Notice also that you do not send a type to a function. You wouldn't do this:

    display(int); //error

    After the value is sent to the function, the value is stored in what's called the function 'parameter'. The function parameter for the display() function is 'n':
    Code:
    void display(int n)
    {
    	cout<<n<<endl;
    }
    It's type is an int, so whatever value is sent to the function has to be type int. The int value that is sent to the function is then stored in the variable 'n'. Inside the function, the variable 'n' can be used to obtain that int value:

    cout<<n<<endl;

    Finally, when a function returns a value(which is not the case with the display() function), the function call in main() is replaced by the return value.

    3) The function definition is placed outside of main() and after main(), and it must list the function parameter by name---not just by type which is allowed in the function declaration.

    Another comment on scope: generally variables are only valid inside the 'block' they are declared in. A 'block' is formed by a set of braces:
    Code:
    int main()
    {
        int num;
        string str;
       
        return 0;
    }
    So, if you need the values of the variables in a function, you must pass them to the function:
    Code:
    int main()
    {
        int num;
        string str;
    
        myFunc(num, str);
        
        return 0;
    }
    Last edited by 7stud; 04-23-2005 at 02:35 AM.

  4. #4
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    Code:
    /******************
      Strip vowels
    *******************/
    #include<iostream>
    #include<string.h>
    
    //function declaration
    int Strip_vowels(char[],int);
    
    
    using namespace std;
    
    int main()
    {
        char array[81];
        cout<<"Please enter a sentence without capitals:"<<endl;
        cin.getline(array,81);
        int size=strlen(array);
        
        //call the function
        Strip_vowels(array,size);
        
        int stop;
        cin>>stop;
    }
    
    //function definition
    int Strip_vowels(char array[],int size)
    {
        char vowels[6]={"aeiou"};
        for(int a=0; a<size; a++)
        {
            int condition=0;
            for (int b=0; b<5; b++)
            {
                if(array[a]==vowels[b])
                {
                    condition=1;
                }
           }
           if(condition==0)
           {
               cout<<array[a];
           }
       }
    }

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    how about my version?

    Code:
    int Strip_vowels(register char *p)
    {
    
    	int stripped = 0;
    	for(register char c = *p ; c; c = tolower(*p++))
    	{
    		switch(c)
    		{
    			case 'a':
    			case 'e':
    			case 'i':
    			case 'o':
    			case 'u':
    			        stripped++;
    				continue;
    			default:
    				cout << c;
    		}
    	}
    	return stripped;
    }
    Last edited by misplaced; 04-23-2005 at 02:13 AM.
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Quote Originally Posted by treenef
    #include<string.h>
    1) Not a very good example to set.

    2) You also seem intent on continually violating the cboard homework policy.

    Quote Originally Posted by misplaced
    Code:
    for(register char c = *p ; c; c = tolower(*p++))
    Do you really expect a beginner to understand that?

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    Quote Originally Posted by 7stud
    Do you really expect a beginner to understand that?

    no, but it's fun

    but who knows, if s/he studies it they might learn something
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    but who knows, if s/he studies it they might learn something
    Well, in this example we might have to turn the tables on you. What's the output from your function for a string like this:

    "some words"
    Last edited by 7stud; 04-23-2005 at 06:04 AM.

  9. #9
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    Quote Originally Posted by 7stud
    Well, in this example we might have to turn the tables on you. What's the output from your function for a string like this:

    "some words"

    silly me

    there's another one too. can you find it?
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You mean aside from your use of the worthless register keyword? Or the fact that continue can be replaced with the standard break keyword?

    At any rate, this comes up a lot. Here are some fun [i]isvowel[/b] related posts.

    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    You also seem intent on continually violating the cboard homework policy.
    This is only possible now since the rep system is not in place.



    Although it was probably a better idea to wait for Mb1 to come up with a code himself and then make suggestions.

    However, I don't like making suggestions.

  12. #12
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    This is only possible now since the rep system is not in place.
    Yes, but I think you have a sugar tooth anyway, and you like the red candy you get.

    However, I don't like making suggestions.
    Well, how about this: do the program as an exercise, which you seem to enjoy, and then DON'T post anything?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Post your games here...
    By Hammer in forum Game Programming
    Replies: 132
    Last Post: 02-28-2013, 09:29 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM