English ==> Piglatin translator not working!

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. :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//Translate english string to PIGLATIN!

#include <iostream>
#include <cstring>
using namespace 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') {
        return true;
    }

    else {
        return false;
    }
}

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(c[cntr]!= 'a' || c[cntr]!= 'e' || c[cntr]!= 'i' || c[cntr]!= 'o' || c[cntr]!= 'u') {
            cntr++;
        }

        //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);
}

Last edited on
1
2
3
4
5
6
7
8
9
10
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.

Please edit your post so there is only a sinlge copy of your code.

An error I noticed:
Line 44 - i is undefined.

Suggestion: isVowel should accept a single char rather than an array.
Line 44 is an obvious candidate for using isVowel.






Sorry about the initial code i posted i dont know what happend,! i have changed it now,, !

^^, please have a look again, thanks for the heads up AbstractionAnon!
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

=>Thanks!
oh, here's the code! :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//Translate english string to PIGLATIN!

#include <iostream>
#include <cstring>
using namespace 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') {
        return true;
    }

    else {
        return false;
    }
}

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);
}
Topic archived. No new replies allowed.