Hello guys, please help me with the code below, its a bit lengthy!. Oh and i am trying to do this using cstrings, i managed to get it right with std::strings! Thanks. :)
void func()
{
if ( some_condition )
{
some_type variable ;
// do something with variable.
}
// variable does not exist here.
}
You cannot create a variable in a scope such as that defined by the compound statement above and expect it to exist outside that scope. Scope defines lifetime for variables of automatic duration.
Your code doesn't make much sense. It would appear you pasted your code in the middle of your code.
Line 42 will always be true, which is not what you want.
You're using != with an ||. One or more of the conditions will always be true.
This is precisely why I suggested using isVowel() here.
i have changed the code to the one below and use isVowel like you advised, the problem now is subStr doesnt on line 47, doesnt retain all the values before a vowwel is detected, why could this be?, i thought line 47 means:
#from copy in array c , from 0 - cntr element! 0.0
//Translate english string to PIGLATIN!
#include <iostream>
#include <cstring>
usingnamespace std;
/**
Rules:
1. First word a vowel, add "ay"
2. First!= vowel, copy till vowel, add those coppied words at end
and add "way"
**/
bool isVowel(char b[])
{
if(b[0] == 'a' || b[0] == 'e' || b[0] == 'i' || b[0] == 'o' || b[0] == 'u') {
returntrue;
}
else {
returnfalse;
}
}
void translate(char c[])
{
char pigLatin[255] = {};
char subStr[255] = {};
//test if first letter is vowel add "ay"
if (isVowel(c) == true) {
strcat(pigLatin, c);
strcat(pigLatin, "ay");
}
//===>>This part below is not working,,YYYYY! //if not vowel!
else {
int cntr = 0;
//increament cntr untill a vowel is detected
while(!isVowel(c)) {
cntr++;
c++;
}
//use counter to copy all non vowel words
strncpy(subStr, c, cntr);
for(int i =0; i< cntr; i++) {
c[i] = '\0'; //append null to all begining non vowel words
}
strcpy(pigLatin, c); //strcpy will copy all characters left in C excluding nulls!
strcat(pigLatin, subStr);
strcat(pigLatin, "way");
}
cout<<pigLatin;
}
int main()
{
char a[255] = {};
cout<<"Enter your string! :\n";
cin>>a;
translate(a);
}