No ouput from my function, please check?

This is a discussion on No ouput from my function, please check? within the C Programming forums, part of the General Programming Boards category; Hi, I wrote a c file to output text, making sure it doesn't go over a maximum width: Code: #include ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    23

    No ouput from my function, please check?

    Hi, I wrote a c file to output text, making sure it doesn't go over a maximum width:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    #include "printer.h"
    
    /* Print the given text to stdout, adding newlines as
     * necessary between words in order to keep the maximum line
     * length to the given specified value.
     */
    
    void printText(const char *text, const int maxLineWidth) {
    	int lineWidth = 0;
    	int textEnd = 0;
    	int i = 0;
    	char currWord[strlen(text)+1];
    	while (textEnd == 0) {
    		int k = 0;
    		while (isspace(text[i]) == 0) {
    			if (text[i] == '\0') {
    				textEnd = 1;
    				break;
    			}
    			currWord[k++] = text[i++];
    		}
    		currWord[k] = '\0';
    		if ((strlen(currWord) + lineWidth) <= maxLineWidth) {
    			printf("&#37;s", currWord);
    			lineWidth += strlen(currWord);
    		}
    		else {
    			printf("\n%s", currWord);
    			lineWidth = strlen(currWord);
    		}
    	}
    	printf("\n");
    }
    
    int main() {
    	printDescription("Hello, this is some text aligned to a maximum width of 15 characters.", 15);
    	return 0;
    }
    instead of printing as it's supposed to it just hangs, waiting with no output, i assume it's to do with a loop condition and that its looping forever but i can't spot where and figure how to fix it. please help me out, thanks.

    great board by the way, glad to be a part.
    Last edited by space-oddity; 04-27-2008 at 03:59 AM.

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    You never change the value that controls your loop, ie `textEnd`.

    Code:
    while (isspace(text[i]) == 0) {
    			if (text[i] == '\0') {
    				textEnd = 1;
    				break;
    			}
    			currWord[k++] = text[i++];
    		}
    How can the character be a space AND a nul-terminator at the same time!?

    Perhaps a better way to loop would be,
    Code:
    size_t len = strlen(text);
    size_t i = 0;
    
    for(i = 0; i < len; i++)
    {
        /* blah */
    }

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,762
    What does isspace() return on the nul character?

    If you want to see what happens, follow each printf with a
    fflush(stdout)

    Also, char currWord[strlen(text)+1]; is non-standard, and with a bit of thought, unnecessary.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    23
    Quote Originally Posted by zacs7 View Post
    Code:
    while (isspace(text[i]) == 0) {}
    How can the character be a space AND a nul-terminator at the same time!?
    But doesn't while (isspace(text[i]) == 0) {} only do the things while it isn't a space? (as opposed to == 1 which would be while it is a space)
    So it couldn't be both?

    I used char currWord[strlen(text)+1] because I wanted to try and avoid dynamic memory. What can I change it to?

    Thanks for your replies, only been doing C for 2 months (did a bit of Java before) so still gaining an understanding.

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    I suggest you draw a flow-chart and then implement it in C. Do you want to trim at words or characters? The latter being much easier.

    > I used char currWord[strlen(text)+1] because I wanted to try and avoid dynamic memory.
    Why avoid dynamic memory? It's a huge part of C. Perhaps Salem was hinting towards pointers...
    Last edited by zacs7; 04-27-2008 at 04:58 AM.

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    23
    i want to trim it at words

  7. #7
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    http://en.wikipedia.org/wiki/Word_wrap

    Perhaps try and implement the "Greedy algorithm". Your biggest challenge will probably be splting the text into 'words', while preserving spaces.

    If you feel upto a challenge perhaps "The Knuth" algorithm could be good.
    Last edited by zacs7; 04-27-2008 at 06:08 AM.

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    23
    looks like the greedy algorithm is the same as what im trying to do with my code.

    if i use dynamic memory allocation for currWord there's no possibility of a memory leak when a program using the function exits?
    Last edited by space-oddity; 04-27-2008 at 06:46 AM.

  9. #9
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Yes, but you're diving straight into the coding. Do it on paper first, like how you're going to split words.

    Do you know how to use pointers in C? If not, go and learn.

  10. #10
    Registered User
    Join Date
    Apr 2008
    Posts
    23
    I had another try at it, starting over from scratch, after trying to understand pointers better and so I utilised them more here. Unfortunately this time the function, when run, just continually outputs blank space until I press cntrlC.
    If anyone could take a look at my new version and explain why this is happening I'd appreciate it.

    Code:
    void printText(const char *text, const int maxLineWidth) {
    	int wordLen = 0;
    	int lineLen = 0;
    	const char* wordEnd = NULL;
    	int i = 0;
    	while (isspace(*text) == 1) {
    		text++;
    	}
    	wordLen = strcspn(text, " ");
    	for (i = 0; i < wordLen; i++) {
    		putchar(*text++);
    	}
    	lineLen = wordLen;
    	while (*text != '\0') {
    		while (isspace(*text) == 1) {
    		text++;
    		}
    		wordLen = strcspn(text, " ");
    		wordEnd = text + wordLen;
    		if ((wordLen + lineLen) < maxLineWidth) {
    			putchar(' ');
    			while (text != wordEnd) {
    				putchar(*text++);
    			}
    			lineLen += wordLen;
    		}
    		else {
    			putchar('\n');
    			while (text != wordEnd) {
    				putchar(*text++);
    			}
    			lineLen = wordLen;
    		}
    	}
            putchar('\n');
    }
    
    int main() {
    	printText("A sentence output to a maximum width of twenty characters per line.", 20);
    	return 0;
    }
    Last edited by space-oddity; 04-30-2008 at 11:16 PM.

  11. #11
    Registered User
    Join Date
    Apr 2008
    Posts
    23
    someone?

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    There's a difference between a NULL pointer and the character \0. When text points to the end of the string, the pointer is still a normal pointer pointing to a memory address. However, *text will == 0.

  13. #13
    Registered User
    Join Date
    Apr 2008
    Posts
    23
    oh okay, i'd been assuming that
    text != NULL
    and
    *text != '\0'
    were the same.
    thanks.

  14. #14
    Registered User
    Join Date
    Apr 2008
    Posts
    23
    just to make sure,
    *text == 0
    and
    *text == '\0'
    are exactly the same?

  15. #15
    Registered User
    Join Date
    Apr 2008
    Posts
    23
    i fixed up that statement, but the output is still exactly the same.
    more help?

Page 1 of 2 12 LastLast
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-30-2005, 12:21 AM
  3. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 06: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

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