I'm stupid OR help me with this simple program.

This is a discussion on I'm stupid OR help me with this simple program. within the C++ Programming forums, part of the General Programming Boards category; I feel so dumb for not being able to figure this out. Maybe it's because I'm sick, but for some ...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    28

    I'm stupid OR help me with this simple program.

    I feel so dumb for not being able to figure this out. Maybe it's because I'm sick, but for some reason, I just can't get this program to work right. Currently, it's giving me an illegal operation before it prints one line. It's probably a pointer problem, but I can't figure it out.

    Anyway, the program is supposed to take in a phrase, then translate it to pig latin using the strtok function. (I'm just trying to get it to work with the phrase written into the code right now.) Also, we are to assume that the phrase has no punctuation and that all the words are at least two letters.

    On with the code.

    #include <iostream>
    #include <cstring>
    using namespace std;

    char printlatinword(char* token);

    void main()
    {
    char phrase[]="Hello world";
    char* translation=NULL;
    char* tokenptr= NULL;

    tokenptr = strtok( phrase," ");

    while (tokenptr != NULL)
    {
    printlatinword(tokenptr);
    cout<< tokenptr <<endl;
    tokenptr = strtok(NULL," ");
    }
    }

    char printlatinword(char* token)
    {
    char* firstletter = NULL;
    char* translation = NULL;

    firstletter = strncpy(firstletter, token, 1);

    translation = strcat(token, firstletter);
    translation = strcat(token, "ay\0");
    return *translation;
    }

    The problem is definitely in the printlatinword function. When I replaced the function call with a cout<<tokenptr statement, it printed the tokens without a problem.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    In your printlatinword() function, you are using pointers firstletter and translation without allocating any space for them. Also, it's probably not a good idea to change the contents of tokenptr within printlatinword(), as strtok() is using this to store tokens. Also main() returns an int.

    #include <iostream>
    #include <cstring>

    void printlatinword(char *token, char *translation);

    int main()
    {
    char phrase[]="Hello world";
    char translation[51] = "";
    char* tokenptr= NULL;

    tokenptr = strtok( phrase," ");

    while (tokenptr != NULL)
    {
    printlatinword(tokenptr,translation);
    cout<< tokenptr << "..." << endl;
    cout<< translation << "..." << endl;
    tokenptr = strtok(NULL," ");
    }
    return 0;
    }

    void printlatinword(char *token, char *translation)
    {
    strcpy(translation, token+1);
    strncat(translation, token, 1);
    strcat(translation, "ay");
    }

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    28
    Thanks a lot, I was lost there.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 09-30-2007, 11:08 PM
  2. Help with a very simple program
    By htdefiant in forum C++ Programming
    Replies: 13
    Last Post: 08-14-2007, 01:27 PM
  3. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  4. [Help] Simple Array/Pointer Program
    By sandwater in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 02:42 PM
  5. Simple window program
    By baniakjr in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2006, 02:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21