Thread: No ouput from my function, please check?

  1. #16
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by space-oddity View Post
    just to make sure,
    *text == 0
    and
    *text == '\0'
    are exactly the same?
    AFAIAA, yes.

  2. #17
    Registered User
    Join Date
    Apr 2008
    Posts
    23
    After doing a bit of study, I realised that the code might not be working because im incrementing text, ie. text++, when text is declared to be a const pointer. Is this allowed? If not how can I correct my code? (I have to keep the text pointer paramter const.) I tried creating a new non-const pointer to point at the start of text so I could use that, but got an invalid conversion error (which also happened when I first made the wordEnd pointer, before making that const.).

    EDIT: I just found out that the way the text parameter is declared:
    Code:
    const char *text
    means that only what is pointed to is const rather than the pointer itself, right?
    ..so I'm back to where I was..
    Last edited by space-oddity; 05-01-2008 at 01:57 AM.

  3. #18
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I looked at your code - it's quite late, and frankly it didn't hit me as the way to quite do it. I know this job can be done elegantly with pointers, but it can also be done OK without pointers.

    This is a sample of the latter:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    void printText(void);
    
    int main(void) {
       printf("\n\n\n\n");
       printText();
       return 0;
    }
    
    void printText()  {
       int i, j, left, lwidth, right, width, gar;
       char t[] = "The quick red fox jumped quickly and nimbly over the sleeping dog.\0";
    
       i = left = right = lwidth = 0;
       width = 20;
       
       while(t[i])   {                                 //loop the whole phrase
          i++;
          right++;
          if(t[right] == ' ' || t[right] == '\0')   {  //print the word if it
             if(lwidth + (right - left) <= width)  {   //will fit on the line
                for(j = left; j < right; j++)   
                   putchar(t[j]);
                
                lwidth = lwidth + (right - left);      //set up for next word
                left = right;
             }
             else  {                                   //word wouldn't fit 
                putchar('\n');                         //on the line
                lwidth = 0;                            //start a new line
                for(j = left + 1; j < right; j++)  {
                   putchar(t[j]);
                   lwidth++;
                }
                left = right;                          //set up for next word
             } //else   
          } //if(t[i]...
       } //while t[i]
       
       gar = getchar(); gar++;
    }
    
    //gar is just shorthand for garbage value to hold the window open.

  4. #19
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    I'm thinking there's an even easier way:
    Check whether strlen() returns a value greater than the allowed width, if not, print the line.
    Otherwise, go to the position at the max width in the string. If it's a space, print the first part (you could use snprintf into a string and then print that, rather than printing each char individually) and then print the second part (same way as the first).
    If you landed on a char, then go backwards (decrement a pointer) until you hit a space and then repeat the above.
    For a nice, recursive touch, instead of just printing the second line, call your printText function on it. That will ensure that if you're printing text that's bigger than two line widths, it gets broken down properly.

    QuantumPete
    P.S. Please ask if you want me to elaborate on anything.
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  5. #20
    Registered User
    Join Date
    Apr 2008
    Posts
    23
    Thanks for your help guys, I played around with your code adak and realised I was getting the bad output due to using the isspace function.. why was that the cause? The description of it in the man makes it look perfect for the purpose.

    QuatumPete, your recursion idea looks cool and I might try it out sometime but doing it that way would mean any extra spaces in between the words would still be there, which is one of the things i was trying to eliminate, ie two spaces between two words reduces to one in output.
    Last edited by space-oddity; 05-02-2008 at 07:37 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM
  2. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  3. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Replies: 5
    Last Post: 02-08-2003, 07:42 PM