Search code examples
carraysgetchar

Program won't store characters in 2d array in c


I am creating a program where I insert a number of sentences and the program outputs them in order. I have finished the program, but when I run it it seems like the characters I input into the array aren't displayed or stored correctly, getting as a result random letters instead of the full sentence. Here is the code of the program:

char ch;
int i,j,k;
int nothing = 0;
int count = 1;
char lines[5][256];
int length[256];

int main() {
    printf("Please insert up to a max of 5 lines of text (Press enter to go to next line and twice enter to stop the program):\n");
    i = 0;
    while (i<5){

        j = 0;
        ch = getche();

        if (ch == '\r'){
            if(i!= 0){
                break;    
            }
            printf("You have not inserted anything, please insert a line:");
            i=-1;   
        }

        if(ch != '\r'){
            lines[i][j]=ch;
            while (ch!='\r'){
                ch = getche();
                lines[i][j] = ch;
                j++;
            }
        }
        printf("\n");
        i++;
    }
    for (k=i ; k > 0; k--){ 
        printf("\tphrase %i :", count);
        for ( j =0 ; j <= length[k]; j++){
            printf("%c",lines[j][k]);
        }
        count++;
        printf("\n");
    }   
    return 0;
}

How can I get the characters to be stored and displayed correctly? Any help is appreciated, thank you!!


Solution

  • There are numerous problems with your code. I'll try and summarise here, and give you improved code.

    Fist, some changes that I made to get this to compile on my system:

    1. Changed getche() to getchar() (getche() does not appear to be available on Ubuntu).
    2. I took out the section about re-entering a string, and just focused on the rest (since the logic there was slightly broken, and not relevant to your question). It will still check for at least one line though, before it will continue.
    3. I had to change the check for \r to \n.
    4. I changed your length array to size 5, since you'll only have the lengths of maximum 5 strings (not 256).

    Some problems in your code:

    1. You never updated the length[] array in the main while loop, so the program never knew how many characters to print.
    2. Arrays are zero indexed, so your final printing loops would have skipped characters. I changed the for parameters to start at zero, and work up to k < i, since you update i after your last character in the previous loop. The same with j.
    3. Your reference to the array in the printing loop was the wrong way around (so you would've printed from random areas in memory). Changed lines[j][k] to lines[k][j].
    4. No need for a separate count variable - just use k. Removed count.
    5. The nothing variable does not get used - removed it.
    #include <stdlib.h>
    #include <stdio.h>
    
    char ch;
    int i,j,k;
    char lines[5][256];
    int length[5];
    
    int main()
    {
        printf("Please insert up to a max of 5 lines of text (Press enter to go to the next line and twice enter to stop the program):\n");
        i = 0;
        while (i<5)
        {
            j = 0;
            ch = getchar();
    
            if ((ch == '\n') && (j == 0) && (i > 0))
            {
                break;
            }
    
            if (ch != '\n')
            {
                while (ch != '\n')
                {
                    lines[i][j] = ch;
                    j++;
                    ch = getchar();
                }
            }
            length[i] = j;
            printf("\n");
            i++;
        }
        for (k = 0; k < i; k++)
        {
            printf("\tPhrase %i : ", k);
            for (j = 0; j < length[k]; j++)
            {
                printf("%c", lines[k][j]);
            }
            printf("\n");
        }
        return 0;
    }