Problem With Line Wrap Header File (segfault)

This is a discussion on Problem With Line Wrap Header File (segfault) within the C++ Programming forums, part of the General Programming Boards category; I am trying to program a header that prevents line wrap in the console. The problem is that I keep ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Location
    Texas
    Posts
    4

    Question Problem With Line Wrap Header File (segfault)

    I am trying to program a header that prevents line wrap in the console. The problem is that I keep getting a Segmentation fault error in seemingly strange places. Here is the code:

    Code:
    #include <iostream>
    using namespace std;
    
    const int LINEWIDTH = 80; // this holds the width of the line
    int counter1 = 1; // records the current position on the current line
    int counter2 = 0; // records the current position in the string as it is being checked
    int counter3 = 0; // records the length of the current word
    int counter4 = 0; // records how much of the word has been printed
    int counter5 = 0; // records the current position in the string as it is being printed
    bool loop = true;
    
    void lineWrap (char string[])
    {
    	while(counter1 < LINEWIDTH) // loops if the current position on the line is before the the specified line width
    	{
    		while(string[counter2] != ' ') // this finds the length of the current word, stored in counter3
    		{
    		    counter2++;
    			counter3++;
    		}
    		if(counter3 <= ((counter1 - LINEWIDTH) / -1)) // this checks to see if there is sufficent space to print the word before running
    		{                                             // out of space on the current line
                while(counter4 != counter3) // this prints the current word
                {
                    cout<<string[counter5];
                    counter5++;
                    counter4++;
                    counter1++;
                }
    		}
    		else // if there isn't sufficent space to print the next word then this will start a new line and restart the line position counter
    		{
    		    cout<<endl;
    		    counter1 = 1;
    		}
            counter2++;
    		counter3 = 0;
    		counter4 = 0;
    	}
    }
    I call the function with:

    Code:
    lineWrap("This is a long string of text to see if the lineWrap function will prevent linewrap");
    Every time I compile and run the program though, I get this output:

    Code:
    This is a long string of text to see if the lineWrap functio
    
    
    Segmentation fault
    Press ENTER to continue.
    I realize that segmentation faults are a common issue, and I tried to search the forums for an answer, but I still couldn't figure out my problem. Thanks.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    "while (counter1 < LINEWIDTH)" would seem to always be true, since you are very careful to not let counter1 go over the line. Something like "while (counter5 < strlen(string))" seems more like the point.

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    and what happens
    Code:
    while(string[counter2] != ' ')
    if the last word does not have space at the end?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Registered User
    Join Date
    Mar 2010
    Location
    Texas
    Posts
    4

    Thumbs up

    Both of you were right, thank you very much. I don't know how I missed both of those, but now I can get back to working on the function rather trying to fix some stupid error, thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Replies: 3
    Last Post: 04-27-2005, 11:50 AM
  5. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM

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