Thread: Returning an integer ... not returning; weird error

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    178

    Returning an integer ... not returning; weird error

    printhead.h
    Code:
    #ifndef PRINTREAD_H_
    #define PRINTREAD_H_
    
    #define LINE 256
    
    namespace ioline {
    	void outputLine(const char *line);
    	char * inputLine(void);
    }
    
    namespace tokens {
    	int tokenizer(char *str, char **array);
    }
    
    #endif /* PRINTREAD_H_ */
    tokenizer.cpp
    Code:
    #include <iostream>
    #include <cstring>
    #include "printread.h"
    
    namespace tokens {
    
    	int tokenizer(char *str, char **array);
    
    	int tokenizer(char *str, char **array)
    	{
    		int tokenCount;
    
    		const char *delims = " ,;:";
    
    		array[0] = strtok(str, delims);
    		tokenCount++;
    
    		while ( (++*array = strtok(NULL, delims)) != NULL)
    		{
    			tokenCount++;
    		}
    
    		return tokenCount;
    
    	}
    }
    token.cpp
    Code:
    template <typename T>
    T **allocate(int rows, int cols)
    {
        T **array;
    
        array = new T *[rows];
        for (int i = 0; i < rows; i++)
        {
            array[i] = new T [cols];
        }
       return array;
    }
    
    else
    	{
    		int tokenCount;
    
    		char *line = new char [LINE];
    
    		line = ioline::inputLine();
    
    		ioline::outputLine(line);
    
                   array = alloacate(100, 100);
    
    		tokenCount = tokens::tokenizer(line, array);
    
    		std::cout << "Token count: " << tokenCount << std::endl;
    
    		std:: cout << "\nTokens:\n" << std::endl;
    
    		for(int i = 0; i < tokenCount; i++)
    		{
    			std::cout << array[i] << std::endl;
    		}
    	}
    
    	return 0;
    The tokenizer.cpp is supposed to return an integer representing the token count. Somehow it does not.
    Last edited by Imanuel; 09-24-2011 at 10:31 PM.

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Why are you using strtok and malloc with C++ code? I will not even bother to look at your code until malloc is gone.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    178
    I wasn't sure how to dynamically allocate an array of pointers so I fell back on malloc. I have a page open to allocate it so I will change the code for you.

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by Imanuel View Post
    I wasn't sure how to dynamically allocate an array of pointers so I fell back on malloc. I have a page open to allocate it so I will change the code for you.
    I am not sure why you are mixing so much C code in with C++ constructs. This is a recipe for disaster. I suggest you take a look at How do I separate a string into tokens (C++)-FAQ.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    178
    Quote Originally Posted by AndrewHunter View Post
    I am not sure why you are mixing so much C code in with C++ constructs. This is a recipe for disaster. I suggest you take a look at How do I separate a string into tokens (C++)-FAQ.
    All I know is C and I am making a slow transition to C++... we have been over this before; yeah that book thing c->c++.learning_thing.

    Not like I can help it nor like I can do anything about it but I am finally at the point of objects and classes in C++. Oh well, someone will come along and help me out.

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by Imanuel View Post
    All I know is C and I am making a slow transition to C++... we have been over this before; yeah that book thing c->c++.learning_thing.

    Not like I can help it nor like I can do anything about it but I am finally at the point of objects and classes in C++. Oh well, someone will come along and help me out.
    Really? Did you click on the red thing? Yes, it is a link. You should click on it and read through.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    178
    I did and read it but it has nothing to do with resolving my problem of passing an integer from one function to another. The use of strtok does not generate errors related to my problem. I have never encountered someone such as you; obstinate, belligerent, head-strong, overbearing.

    Have a good night Andrew Hunter.

  8. #8
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by Imanuel View Post
    I did and read it but it has nothing to do with resolving my problem of passing an integer from one function to another. The use of strtok does not generate errors related to my problem.

    Have a good night Andrew Hunter.
    Your program is attempting to tokenize a string and then return token count correct? So how is what I posted not helping you exactly?
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    178
    Because returning a simple integer out of a namespace should not cause a return error. Tokenization is not my problem and clearly I can tokenize a character string and have done so problem free with my program. If you want to tackle the actual issue and not your insistence that I conform immediately to using proper C++ code or the world will crumble beneath my feet, feel free.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Imanuel
    The tokenizer.cpp is supposed to return an integer representing the token count. Somehow it does not.
    What do you mean by "somehow it does not"? Are you saying that your tokens::tokenizer function runs into an infinite loop?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by Imanuel View Post
    Because returning a simple integer out of a namespace should not cause a return error. Tokenization is not my problem and clearly I can tokenize a character string and have done so problem free with my program. If you want to tackle the actual issue and not your insistence that I conform immediately to using proper C++ code or the world will crumble beneath my feet, feel free.
    Fine keep char arrays and malloc while you do your C++ buffoonery.......
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  12. #12
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by Imanuel View Post
    I did and read it but it has nothing to do with resolving my problem of passing an integer from one function to another. The use of strtok does not generate errors related to my problem. I have never encountered someone such as you; obstinate, belligerent, head-strong, overbearing.

    Have a good night Andrew Hunter.
    Hmm....noted.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  13. #13
    Registered User
    Join Date
    May 2010
    Posts
    178
    Quote Originally Posted by laserlight View Post
    What do you mean by "somehow it does not"? Are you saying that your tokens::tokenizer function runs into an infinite loop?
    No, what I mean is when I perform a trace of function execution, the value is correct at the return but when verifying the value is passed into the variable to receive the return it is 0;

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Imanuel
    No, what I mean is when I perform a trace of function execution, the value is correct at the return but when verifying the value is passed into the variable to receive the return it is 0;
    That sounds impossible. That said, I noticed that you neglected to initialise tokenCount.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    May 2010
    Posts
    178
    I thought about what AndrewHunter replied and I gave up C++ Primer Plus (Prata's book) and picked up Accelerated C++ which teaches C++ from the get go and not 10 chapters after teaching C. Looks like good book and I do want write C++, not C code with some C++ in it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. physics class returning weird numbers
    By avgprogamerjoe in forum C++ Programming
    Replies: 1
    Last Post: 09-17-2007, 08:13 PM
  2. Recursion: base case returning 1, function returning 0
    By yougene in forum C Programming
    Replies: 5
    Last Post: 09-07-2007, 05:38 PM
  3. returning error on if/else in function
    By xion in forum C++ Programming
    Replies: 6
    Last Post: 06-12-2004, 03:45 AM
  4. error when returning template type~
    By black in forum C++ Programming
    Replies: 5
    Last Post: 06-01-2004, 01:33 AM
  5. kernel keeps returning a clos() error
    By Chronom1 in forum Linux Programming
    Replies: 2
    Last Post: 03-30-2004, 06:21 PM