Jump to content

Help! Problem with string manipulation and using tokens??

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
hybridalter

hybridalter

    Newbie

  • Members
  • Pip
  • 7 posts
hi all, im doing this problem with string manipulation (C programming on MS Visual Studio 2005 Pro Ed.) and im crapping out on calling this function that i have under routines.cpp when trying to call it from main.cpp

the function prototype (which i have under routines.h) is:
int tokenize(char *token[ ], int max_tokens, char *str);

and i have the actual function in routines.cpp as this:
#include "routines.h"
#include <stdio.h>
#include <string.h>

int tokenize(char token[ ], int max_tokens, char *str)
{	
	printf("%s\n%s\n\n%s\n",
		"The string to be tokenized is:", token,
		"The tokens are:" );

	str = strtok (token, " ");

	while (str != NULL) {
		printf("%s\n", str);
		str = strtok(NULL, " ");
	}
	return 0;
}
------------------------------------------------------------

and so, if i wanted to call this function from main.cpp how would i do that?

i have: tokenize(*token, max_tokens, *str);

but i know its wrong because it wont compile. Please help on how to call this function from main.cpp, i have horrible skills with calling functions. THANK YOU IN ADVANCE

Edited by WingedPanther, 03 April 2008 - 12:42 PM.
Add code tags


#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
First observation: your definition and declaration disagree. One has type char * [], the other char [] for the first parameter.

What error messages are you getting?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
hybridalter

hybridalter

    Newbie

  • Members
  • Pip
  • 7 posts
well i went back to routines.cpp and fixed the agreement but i still get 3 error messages when i compile (that is when i compile and calling the tokenizing function as: tokenize( *token, max_token, *str);

these are the errors:
error C2065: 'max_tokens' : undeclared identifier
error C2065: 'str' : undeclared identifier
error C2664: 'strtok' : cannot convert parameter 1 from 'char *[]' to 'char *'

and im calling the function tokenize( *token, max_token, *str); under main.cpp

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
The first two errors mean you have to declare the variables in main() before you can use them.
The third error means your function declaration/definition should be:
int tokenize(char *token, int max_tokens, char *str);
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
hybridalter

hybridalter

    Newbie

  • Members
  • Pip
  • 7 posts
hey thanks a lot i got it to work, thanks!!!!!!

#6
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
You're very welcome :)
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog