Thread: No ouput from my function, please check?

Threaded View

Previous Post Previous Post   Next Post Next Post
  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 02:59 AM.

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