-
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.
-
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
-
Please use code tags when posting code. I'll fix your original post.
-
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.
-
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().
-
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;
}
-
thanks for lending me a hand. ;)
-
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
}
}
}
-
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.