Small string program help please

This is a discussion on Small string program help please within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm trying Chapter 5, programming question 35 in the Dietel book and I come accross these 2 errors: It's ...

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    14

    Small string program help please

    Hi, I'm trying Chapter 5, programming question 35 in the Dietel book and I come accross these 2 errors:

    It's supposed to be a PigLatin word program, and I'm trying to take in a whole sentence, break it up into words, take the first letter and place that at the end of the word, then add 'ay' to it.

    I'm also learning how to pass data to functions without needing to pass by value or reference when that uses up time and memory to make another copy of the data for pass by value. So, I'm trying to pass through pointer references.

    Code:
    //Chapter 5.35
    
    #include <iostream>
    #include <cstring>
    using namespace std;
    
    //Used to break the input string into individual word tokens.
    //After that, it will call Print_Latinword to change to tolken word into PigLatin and cout it.
    void Tolkenize (char *input);
    
    //ACCEPTS: The tokenized word from function Tolkenize.
    //DOES:    It will take the 1st letter from the word.
    //		   Shifts the rest of the string down an index.
    //		   Places the first letter at the end of the string word, and then adds 'ay' to it.
    //		   Cout's the new PigLatin word.
    //RETURNS: Nothing.
    void Print_Latinword (char *S1);
    
    int main()
    {
    	char input[81];
    
    	cout << "Enter a short phrase of words."
    	     << "\nThis program will break that phrase into seperate words and convert them to PigLatin."
    		 << endl;
    	cin.getline(input,80);
    
    	Tolkenize(input);
    
    	return 0;
    }
    
    
    void Tolkenize (char *input)
    {
    	char token[30];
    
    	do
    	{
    		for (int i=0; i<20; i++)
    		{
    			token[i] = '\0';		//Resets the token string to NULL
    		}
    
    		token = strtok(input, " "); //ERROR
    
    		cout << "\nDEBUG: This is the token: " << token << endl;
    
    		Print_Latinword(token);
    
    	} while (token != '\0');
    
    	return;
    }
    
    void Print_Latinword (char *S1)
    {
    	char first_letter = *S1;
    
    	for(; S1 != '\0'; S1++)
    	{
    		*S1 = *(S1+1);			//Moves the rest of the letters in the string down an index.
    	}
    		
    	strcat(S1, first_letter);	//ERROR
    	strcat(S1, "ay");
    
    	cout << "\n\nThe new PigLatin word is: " << S1 << endl;
    
    	return;
    }
    For the 2 errors, Visual C++ 6.0 gives me this:

    1. '=' cannot convert from char* to char. This conversion requires a reinterpret_cast.

    2. 'strcat': cannot convert parameter 2 from 'char' to 'const char *'


    Thanks for the help.

  2. #2
    Registered User
    Join Date
    Jul 2003
    Posts
    59
    proper use of strtok

    Code:
    char string[STR_SIZE];
    char *p; <-- notice 
    
    get_some_input_to(string);
    
    chop the words up at a space
    
    p = strtok(string," ");
    
    while(p!=NULL)
    {
        do_something_with(p);
        p=strtok(NULL," ");
    }
    your second error

    strcat(S1, first_letter); //ERROR

    as the compiler says.. you need to give it a char pointer not a char, so you can't use it like that

    s1[strlen(s1)+1]='\0';
    s1[strlen(s1)]=first_letter;

    this will do the trick
    Last edited by erikj; 12-28-2003 at 03:56 PM.

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    14
    But can't you just place the token into a declared string? Why would you need to have it sent to a pointer first?

    Also, when I use strok for this phone number string: (different program example)

    (555) 555-5555

    Code:
    oid Tokenize (char input[], char *areacode, char *begin, char *end, int SIZE)
    {
    	areacode = strtok(input, " ");
    	cout << "\nDEBUG: *areacode1: " << areacode1 << endl;
    	begin	  = strtok(input, "-");
    	cout << "\nDEBUG: begin: " << begin << endl;
    	end	  = strtok(input, "\0");
    	cout << "\nDEBUG: end: " << end << endl;
    
    
    	return;
    }
    It is staying at (555) only and keeps assigning that to each of the strings. Why?

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    59
    look at the example I wrote

    Change the other two from input to NULL.
    The first time you specify what string you want to tokenize. After that you send in NULL and the function continues on the string you specified at the first call.

    areacode = strtok(input, " ");
    begin = strtok(NULL, "-");
    end = strtok(NULL, "\0");


    But can't you just place the token into a declared string? Why would you need to have it sent to a pointer first?
    strtok returns a pointer to a string therefore you cant assign the result to a declared string.

    char token[20];
    token is a const char pointer and it can't be assigned to. You can't do this

    str="khjkj";

    nor

    token=strtok(string," ");

  5. #5
    Registered User
    Join Date
    Dec 2003
    Posts
    14
    Thanks! I'm starting to understand this chapter better. Only 17 more chapters left

    Then I'll move on to Visual GUI's in C++.

  6. #6
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    I suggest continuing studying standard C++ for at least one more book C++ Programming Language by Bjarne Strostrup. I tried taking the same route as you are and found many holes in my basic programming abilities that learning the win32 api did not resolve. I am currently reading this book and doing the exercises is both a challenge and reward. It will help tighten many of the loose ends that you don't even realize you have. It is written by the creator of the language after all. Be patient and have fun!

  7. #7
    Registered User
    Join Date
    Dec 2003
    Posts
    14
    Well, I finally got it to compile and run, but it only works on the first run.

    Code:
    void Tolkenize (char *input)
    {
    	char *token_ptr;
    
    	token_ptr = strtok(input, " ");		//Gets the first word
    
    	cout << "\nDEBUG: This is the token: " << token_ptr << endl;
    	
    	Print_Latinword(token_ptr);			//Calls function
    
    	
    	
    	token_ptr = strtok(NULL, " ");
    
    	
    	while(token_ptr != NULL)
    	{
    			cout << "\nDEBUG: This is the token: " << token_ptr << endl;
    
    		Print_Latinword(token_ptr);
    
    		token_ptr = strtok(NULL, " ");
    	}
    
    	return;
    
    
    }
    
    void Print_Latinword (char *S1)
    {
    	char * const start_of_string_ptr = S1;
    	char first_letter = *S1;
    	
                     for(; *S1 != '\0'; S1++)
    	{
                       *S1 = *(S1+1);
    	}
    
    	cout << "\nDEBUG: This is the string after taking out the first letter and moving down: " << endl
    		 << start_of_string_ptr << endl;
    
    	start_of_string_ptr[strlen(start_of_string_ptr)+3] = '\0';
    	start_of_string_ptr[strlen(start_of_string_ptr)]   = first_letter;
    
    	cout << "\n\nThe new PigLatin word is: " << start_of_string_ptr << "ay " << endl;
    
    	return;
    }
    I did get the first letter to be added on to the end of the word, but I didn't know how to add "ay" to it, so I just cheated and cout "ay" at the end...

    Also, when I run the program with the input "This that" it works for This -> hisTay but for "that" it just says that-> tay.

    Code:
    (MS DOS SCREEN)
    Enter a short phrase of words.
    This program will break that phrase into seperate words and convert them to PigL
    atin.
    This that
    
    DEBUG: This is the token: This
    
    DEBUG: This is the string after taking out the first letter and moving down:
    his
    
    
    The new PigLatin word is: hisTay
    
    DEBUG: This is the token: t
    
    DEBUG: This is the string after taking out the first letter and moving down:
    
    
    
    The new PigLatin word is: tay
    Press any key to continue

  8. #8
    Registered User
    Join Date
    Jul 2003
    Posts
    59
    I recommend that you read the chapters about pointers and strings. You are getting it wrong.

    Try this if you can't get it to work.
    Code:
    #include <iostream>
    #include <string.h>
    #include <stdio.h>
    
    using namespace std;
    
    #define MAX_LATIN_WORD_SIZE	255
    
    void PrintLatin(char *word)
    {
    	char latinWord[MAX_LATIN_WORD_SIZE+1];
    		
    	sprintf(latinWord,"%s%cay ",word+1,*word);
    	
    	cout <<latinWord;
    	
    	
    }
    
    void Tokenize(char* input)
    {
    	char* word;
    	
    	word=strtok(input," ");
    	
    	while(word!=NULL)
    	{
    		PrintLatin(word);
    		
    		word=strtok(NULL," ");
    	}
    }
    
    int main()
    {
    	char input[81];
    	
    	cout<<"Enter phrase:";
    	
    	cin.getline(input,80);
    	
    	Tokenize(input);
    	
    	return 0;
    }

  9. #9
    Registered User
    Join Date
    Dec 2003
    Posts
    14
    I just don't get why this code:

    Code:
    void Tolkenize (char *input)
    char *token_ptr;
    
    	token_ptr = strtok(input, " ");		//Gets the first word
    
    	cout << "\nDEBUG: This is the token: " << token_ptr << endl;
    	
    		
    	while(token_ptr != NULL)
    	{
    			cout << "\nDEBUG: This is the token: " << token_ptr << endl;
    
    		Print_Latinword(token_ptr);
    
    		token_ptr = strtok(NULL, " ");
    	}
    
    	return;
    
    
    }
    Doesn't seem to work right. It works for tokenizing This, but when it tokenizes that, it only gets "t" and nothing else. Why is that?

    Also, that phone number program tokenizes right now, but it doesn't send the values through the pointers back into main for some reason, yet I followed the C++'s book's examples when dealing with this.

    Code:
    #include <iostream>
    #include <cstring>
    using namespace std;
    
    void Get_Data (char input[]);
    
    void Tokenize (char input[], char *areacode, char *begin, char *end);
    
    void Concatanate (char *areacode, char *begin, char *end);
    
    int main()
    {
    	const int SIZE = 15;
    	char input[SIZE];
    	char areacode[6];
    	char begin[4];
    	char end[5];
    
    	
    
    
    	Get_Data(input);
    	Tokenize(input, areacode, begin, end);
    //	cout << "\nDebug: areacode in main: " << areacode[0] << endl;
    	Concatanate(areacode, begin, end);
    
    	return 0;
    }
    
    void Get_Data (char input[])
    {
    	cout << "Enter a phone number in the format: (area code) ###-####" << endl;
    	cin.getline(input, 15);
    
    	return;
    }
    
    
    void Tokenize (char input[], char *areacode, char *begin, char *end)
    {
    	areacode = strtok(input, " ");
    	cout << "\nDEBUG: areacode: " << areacode << endl;
    	begin	  = strtok(NULL, "-");
    	cout << "\nDEBUG: begin: " << begin << endl;
    	end	  = strtok(NULL, "\0");
    	cout << "\nDEBUG: end: " << end << endl;
    
    
    	return;
    }
    
    
    void Concatanate (char *areacode, char *begin, char *end)
    {
    	char phone_number[9] = {'\0'};
    
    	strcat(phone_number, begin);
    	strcat(phone_number, "-");
    	strcat(phone_number, end);
    
    	cout << "\n\nThis is the phone number again:" << endl; 
    	cout << areacode << " " << phone_number << endl;      
    
    	return;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  3. Abnormal program termination
    By Kayoss in forum C++ Programming
    Replies: 3
    Last Post: 05-16-2006, 03:29 PM
  4. want to make this small program...
    By psycho88 in forum C++ Programming
    Replies: 8
    Last Post: 11-30-2005, 01:05 AM
  5. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM

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