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.