Thread: having some trouble calling a function in the main program

  1. #16
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> Im going to try to finish the current program since it seems like im having trouble even using the strtok function.

    I don't understand. Other than the fact that it needed to be moved into the input loop, it looked fine.

    >> char *parsed=NULL;

    Still NULL.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  2. #17
    Registered User
    Join Date
    Jun 2009
    Posts
    20
    I got it! Sort of. So the code below does this now:
    Hello world
    Hello
    world

    Which is exactly what I want the program to do but not the way it currently does it.

    Code:
    #include <stdio.h>
    #include <string.h>
    #define MAXLENGTH 256
    
    char *next_word(char *instring, char **new_start);
    
    int main()
    {
    	char instring[MAXLENGTH];
    	char *words=NULL;
    	char *parsed=NULL;
    
    	printf("Enter the freaking text:\n");
    
    	fgets(instring, MAXLENGTH, stdin);
    
    	puts(instring);
    	words=next_word(instring, &parsed);
    
    	while (words != NULL)
    	{
    		printf("%s\n", words);
    		words=next_word(parsed, &parsed);
    	}
    }
    
    char *next_word(char *instring, char **new_start)
    {
    	new_start=strtok(instring, " ");
    	//return instring;
    
    }
    If I uncomment "return instring" ill only get the first word outputted. Is there a way to make the function return NULL & instring?

  3. #18
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by CMakesMeSad :( View Post
    I got it! Sort of. So the code below does this now:
    Hello world
    Hello
    world

    Which is exactly what I want the program to do but not the way it currently does it.

    Code:
    #include <stdio.h>
    #include <string.h>
    #define MAXLENGTH 256
    
    char *next_word(char *instring, char **new_start);
    
    int main()
    {
    	char instring[MAXLENGTH];
    	char *words=NULL;
    	char *parsed=NULL;
    
    	printf("Enter the freaking text:\n");
    
    	fgets(instring, MAXLENGTH, stdin);
    
    	puts(instring);
    	words=next_word(instring, &parsed);
    
    	while (words != NULL)
    	{
    		printf("%s\n", words);
    		words=next_word(parsed, &parsed);
    	}
    }
    
    char *next_word(char *instring, char **new_start)
    {
    	new_start=strtok(instring, " ");
    	//return instring;
    
    }
    If I uncomment "return instring" ill only get the first word outputted. Is there a way to make the function return NULL & instring?
    Instead of returning instring, you should return new_string. Then I think it'll do it.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  4. #19
    Registered User
    Join Date
    Jun 2009
    Posts
    20
    Kind of getting there. This code outputs the first word but it gets stuck in a looping cycle

    Code:
    #include <stdio.h>
    #include <string.h>
    #define MAXLENGTH 256
    #define delimiter ""
    
    char *next_word(char *instring, char **new_start);
    
    int main ()
    {
    	char instring[MAXLENGTH];
    	char *words=NULL;
    	char *new_start=NULL;
    	char *parsed=NULL;
    
    	printf("Enter sentences:\n");
    
    	fgets(instring, MAXLENGTH, stdin);
    
    	words=next_word(instring, &new_start);
    
    	while (words != NULL)
    	{
    		printf("%s\n", words);
    		words=next_word(new_start, &new_start);
    	}
    	
    	return 0;
    }
    
    char *next_word(char *instring, char **new_start)
    {
    	*new_start=strtok(instring, " ");
    	return *new_start;
    
    
    }
    Last edited by CMakesMeSad :(; 06-26-2009 at 03:56 PM.

  5. #20
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    I was talking about just returning new_start in your previous code without any other changes in it. So it may look like this.
    Code:
    #include <stdio.h>
    #include <string.h>
    #define MAXLENGTH 256
    
    char *next_word(char *instring, char **new_start);
    
    int main()
    {
    	char instring[MAXLENGTH];
    	char *words=NULL;
    	char *parsed=NULL;
    
    	printf("Enter the freaking text:\n");
    
    	fgets(instring, MAXLENGTH, stdin);
    
    	puts(instring);
    	words=next_word(instring, &parsed);
    
    	while (words != NULL)
    	{
    		printf("%s\n", words);
    		words=next_word(parsed, &parsed);
    	}
    }
    
    char *next_word(char *instring, char **new_start)
    {
    	new_start=strtok(instring, " ");
    	return new_start;//just a change here
    
    }
    [edit]
    In your last code except for the first time, instring gets replaced by "hello"(in case of hello world) in the function and that's why new_start points to "h" each and everytime when calling the function, that's why it gets stuck in the loop.
    [/edit]
    Last edited by BEN10; 06-26-2009 at 09:45 PM.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Replies: 6
    Last Post: 01-26-2009, 08:01 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Return Statement
    By Daveo in forum C Programming
    Replies: 21
    Last Post: 11-09-2004, 05:14 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM