Thread: Strings and cin.get/tokens

  1. #1
    Registered User Kayoss's Avatar
    Join Date
    Sep 2005
    Location
    California
    Posts
    53

    Strings and cin.get/tokens

    I'm trying to understand strncpy.tokenization and cin.get in this simple program. It should read in a sentence entered by the user, copy it to another variable, tokenize and output the original entry along with the copied version. However it's not printing anything at all! Can anyone help?

    Code:
    #include <iostream>
    using namespace std;
    #include <cstring> // prototype for strtok; strtok modifies entry!!
    
    int main()
    {
       char englishStringEntered[ 99 ]; // limits the string to 99 characters
       char englishStringModified[ 99 ]; // storage for tokenized string
       char *englishStringModifiedPtr; // creates pointer to englishString
    
       cout << "Enter a Sentence: " << endl;
       cin.getline( englishStringEntered, 99, '\n' ); // reads keyboard entry into englishStringEntered
       strcpy(englishStringEntered,englishStringModified);
    
       cout << englishStringEntered << endl;
       cout << englishStringModified << endl;
    
       englishStringModifiedPtr = strtok( englishStringModified, " " ); // tokenizes entry
       // loops over each token until reaching null
       while ( englishStringModifiedPtr != NULL ) {
    	   cout << englishStringModifiedPtr << '\n';
    	   englishStringModifiedPtr = strtok( NULL, " " ); // gets next word
       } // end while
    
    
       return 0;
       
    }  // end function main
    THE redheaded stepchild.

  2. #2
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    Code:
    strcpy(char *dst, const char *src);
    Your source and destination are swapped

  3. #3
    Registered User Kayoss's Avatar
    Join Date
    Sep 2005
    Location
    California
    Posts
    53
    Thanks a bunch!
    THE redheaded stepchild.

Popular pages Recent additions subscribe to a feed