Question

So for some reason my program wont run. Its for creating Justified text in a column. Just 1 line of text under the size of the column. Just need help with the issue of it crashing, so i can work out if there is anything else wrong with it. I am still a little new to monogramming so tips appreciated. Thank you in advance for all of your time and help.

#include <stdio.h>
#include <string.h>
#include <stdbool.h>

int col, textlen, space, i, j, letters, spaces;
char text[100];
bool ok;

int main()
{
    // Setting varibles.
    col = 0;
    textlen = 0;
    space = 0;
    i = 0;
    letters = 0;
    spaces = 0;
    ok = false;
    // Prompt and validation.
    while (ok = false)
    {
        printf("Enter the width of the column: ");
        scanf("%d", &col);
        printf("\n");
        printf("Enter a line of text: ");
        gets(text);
        textlen = strlen(text);
        if (col > textlen)
        {
            ok = true;
        }
        else
        {
            printf("Your text is too long for this column please try again.");
        }
    }
    // Working out the space left.
    for (i = 0; i = strlen(text); i++)
    {
        if (text[i] != ' ')
        {
            letters++;
        }
        if (text[i] == ' ')
        {
            spaces++;
        }
    }
    space = (col - letters) / spaces;
    // Writing the final product.
    i = 0;
    while (text[i] != '\0')
    {
        while ((text[i] != ' ') || (text[i] != '\0'))
        {
            printf("%c", text[i]);
        }
        if (text[i] == ' ')
        {
            for (j = space; j > 0; j--)
            {
                printf(" ");
            }
        }
        else
        {
            break;
        }
    }
}
Was it helpful?

Solution

while(ok=false){

should be

while(ok==false){

(The first is an assignment, the second is a test).

And

for(i=0;i=strlen(text);i++){

should probably be

for(i=0;i<strlen(text);i++){

OTHER TIPS

replacing

while(ok=false)

with

while(ok==false)

should be a start

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top