Thread: new c programmer really needs help!

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

    new c programmer really needs help!

    Hi everyone,
    I'm new to C programming and I'm trying to write a program that can print text in columnar format with a maximum width.
    I've been trying with this but I get a seg fault. Am I going about this the right way? Any help would be really appreciated.

    Thanks,
    Cam


    Code:
    #include "columnarText.h"
    #include <string.h>
    #include <stdio.h>
    
    void textPrint(const char *text, const int columnWidth){
    	int lineLength = 0;
    	int count = 0;
    	int stop = 0;
    	int finish = 0;
    	while(stop!=1){
    		
    		char *currWord;
    		int i=0;
    		int k=count;
    		
    		while(strcmp(text[k]," ")!=0){
    			if(strcmp(text[k],"/0")==0){
    				finish = 1;
    				break;
    			}
    			currWord[i]=text[k];
    			k++;
    			i++;
    			count=k;
    		}
    		currWord[i]="/0";
    		i=0;
    		
    		if((strlen(currWord)+lineLength)<column){
    			printf("%s",currWord);
    			lineLength+=strlen(currWord);
    		}
    		else{
    			printf("/n");
    			printf("%s",currWord);
    			lineLength = strlen(currWord);
    		}
    		if(finish==1){
    			stop=1;
    		}
    	}
    
    }
    
    int main(void)
    {
    	char *s = "This is a test string to test my program with.";
    	textPrint(s,10);
    	return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    char *currWord is a pointer; currently it points to nowhere in particular. Thus when you to try to set currWord[i], bad things happen.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    strcmp(text[k]," ")
    are you ignoring warnings or what?
    text[k] is ONE char, you compare char-strings with strcmp, chars are compared as
    Code:
    if(text[k] == ' ')
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What game programmer should I be? need some advice.
    By m3rk in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 04-20-2009, 11:12 PM
  2. When are you REALLY a programmer?
    By m.mixon in forum C Programming
    Replies: 5
    Last Post: 07-19-2006, 09:08 PM
  3. Senior 3D Programmer - Moab Studios™ LLC
    By moab in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 08-30-2005, 10:19 PM
  4. Me as a programmer?
    By Cheeze-It in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 03-31-2002, 06:19 PM
  5. I need to interview professional programmer.....please
    By incognito in forum C++ Programming
    Replies: 1
    Last Post: 01-05-2002, 02:46 PM