Thread: illegal continue?

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    3

    illegal continue?

    I'm having a hard time figuring out what's wrong with the continue command.

    can someone help me?



    Code:
    #include<iostream>
    using namespace std;
    
    void main()
    {
    	char buffer[81];
    	int i;
    	cout<<"Enter a line of text and press Enter key:";
    	cout<<"\nAll the vowels will be discarded!\n";
    	cin>>buffer;
    
    	for (i=0;buffer[i]!='\0';i++);
    	{
    		if ((buffer[i]=='a')||(buffer[i]=='e')||(buffer[i]=='i')||(buffer[i]=='o')||(buffer[i]=='u'))
    			continue;
    		putchar(buffer[i]);
    	}
    }
    EDIT: Code tags inserted by moderator.
    Last edited by VirtualAce; 08-15-2010 at 03:27 PM.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Your main issue here is this:
    Code:
    for (i=0;buffer[i]!='\0';i++);
    That extra semicolon gives the for loop an empty body, and the code in the block that follows is just an ordinary block that will be executed exactly once. Not what you wanted, to be sure. So get rid of the semicolon.

    Also, I'd suggest using std::string's instead of char buffers, because then you don't have to worry about buffer overruns. For example:
    Code:
    #include <iostream>
    #include <string>
    
    int main() {
        std::string name;
        std::cin >> name;
        std::cout << "Greetings, " << name << std::endl;
        return 0;
    }
    Finally, void main() is non-standard and int main() should be used instead. SourceForge.net: Void main - cpwiki
    Last edited by dwks; 08-15-2010 at 03:18 PM.
    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
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Please use code tags when posting code. I'll fix your original post.

  4. #4
    Registered User
    Join Date
    Aug 2010
    Posts
    3
    Code:
    #include<iostream>
    using namespace std;
    
    int main()
    {
    	char buffer[81];
    	int i;
    	cout<<"Enter a line of text and press Enter key:";
    	cout<<"\nAll the vowels will be discarded!\n";
    	cin>>buffer;
    
    	for (i=0;buffer[i]!='\0';i++)
    	{
    		if ((buffer[i]=='a')||(buffer[i]=='e')||(buffer[i]=='i')||(buffer[i]=='o')||(buffer[i]=='u'))
    			continue;
    		putchar(buffer[i]);
    	}
    return 0;
    }
    I already deleted the semicolon. but there's a problem again.

    the program only applies to the first word I input.

    input:
    hello world
    output:
    hll

    can the "string" do the trick?


    EDIT: Code tags inserted by moderator.

  5. #5
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    It's been quite a while since I took input from cin, but doesn't a read through cin stop at whitespace? I think you might want to consider getline().
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    e.g. adapting my previous example slightly:
    Code:
    #include <iostream>
    #include <string>
    
    int main() {
        std::string name;
        std::getline(std::cin, name);
        std::cout << "Greetings, " << name << std::endl;
        return 0;
    }
    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.

  7. #7
    Registered User
    Join Date
    Aug 2010
    Posts
    3
    thanks for lending me a hand.

  8. #8
    Registered User
    Join Date
    Aug 2010
    Posts
    13
    Code:
    #include<iostream>
    
    void main()
    {
    	char Buffer[81];
                   
    	cout<<"Enter a line of text and press enter, all vowels in the text shall be discarded:";
    	
    	cin>>buffer;
    
    	for (int i=0; i < _countof(Buffer); i++);
    	{
                            if ( Buffer[i] != '\0' )
                            {
    		if ( (Buffer[i] == 'a') || (Buffer[i] == 'e') || (Buffer[i] =='i') ||(Buffer[i] == 'o') || (Buffer[i] == 'u') )			
    		// do whatever you like now that you've found a character with a vowel in the array
                             }
    	}
    }

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    iPromise: if you want to correct syk's code, you would do well to correct the other things already mentioned by other users in this thread. Note that _countof is non-standard.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Illegal Case
    By yusko63 in forum C++ Programming
    Replies: 4
    Last Post: 03-26-2010, 11:46 PM
  2. Getting illegal case error
    By scmurphy64 in forum C Programming
    Replies: 2
    Last Post: 09-17-2009, 10:35 AM
  3. Replies: 8
    Last Post: 11-03-2008, 09:48 PM
  4. Continue and switch
    By camzio in forum C Programming
    Replies: 10
    Last Post: 10-04-2008, 08:31 AM
  5. SSH Hacker Activity!! AAHHH!!
    By Kleid-0 in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 03-06-2005, 03:53 PM