-1

I am not sure why wont this work.I am trying to print lines(from input) that are longer than 80 characters.But i dont get correct characters at all.I am not sure what am i doing wrong.Anyone there knows ??

Note: I dont want you to write me the whole new program doing this function (printing lines that are longer than 80).I just wanna know the wrong side of my program.Correction

Note:Last line overwrites the previous ones.

#include <stdio.h>
#define MAXARRAYSIZE 500

char c;
int i = 0;
int j = 0;
int jCopy = 0;
char line[MAXARRAYSIZE];
char linesToPrint[MAXARRAYSIZE][MAXARRAYSIZE];

void emptyArray(char theArray[]);

void InsertArrayIntoArray(char to[MAXARRAYSIZE][MAXARRAYSIZE], char from[], int toIndex, int lengthSoFar);

void printArray(char theArray[MAXARRAYSIZE][MAXARRAYSIZE], int lengthSoFarX);

int main(void) {
    while ((c = getchar()) != EOF) {
        if (i > MAXARRAYSIZE) {
            i = 0;
        }
        if (j > MAXARRAYSIZE) {
            j = 0;
        }
        if (c != '\n') {
            line[i] = c;
            i++;
            //printf("%d",i);
        } else {
            printf("\n j:%d \n i:%d", j, i);
            j++;
            jCopy = j;
            if (i >= 10) {
                InsertArrayIntoArray(linesToPrint, line, j, i);
                //printArray(linesToPrint, j, i);
            }
            emptyArray(line);
            i = 0;
        }
    }
    printArray(linesToPrint, jCopy);
}

void emptyArray(char theArray[]) {
    int i;
    for (i = 0; i < sizeof(theArray) / sizeof(theArray[0]); i++) {
        theArray[i] = 0;
    }
}

void InsertArrayIntoArray(char to[MAXARRAYSIZE][MAXARRAYSIZE], char which[], int toSize, int whichSize) {
    int i, j;
    //printf("\n To size: %d \t Which Size: %d",toSize,whichSize);
    for (i = 0; i < toSize; i++) {
        for (j = 0; j < whichSize; j++) {
            to[i][j] = which[j];
        }
    }
}

void printArray(char theArray[MAXARRAYSIZE][MAXARRAYSIZE], int lengthSoFarX) {
    int i, j;
    for (i = 0; i < lengthSoFarX; i++) {
        printf("\n Line %d :", i + 1);
        printf(" %s\n", theArray[i]);
    }
}
Silidrone
  • 1,471
  • 4
  • 20
  • 35
  • Break the code down into the simplest part that you need help with. – Jay Sep 27 '16 at 12:17
  • 1
    For starters `char c = getchar()` is [wrong](http://stackoverflow.com/questions/35356322/difference-between-int-and-char-in-getchar-fgetc-and-putchar-fputc/35356684#35356684). `getchar()` returns an `int`, you should respect that. – Ilja Everilä Sep 27 '16 at 12:18
  • Be careful, [the `getchar` function](http://en.cppreference.com/w/c/io/getchar) returns an `int`. This is actually very important. – Some programmer dude Sep 27 '16 at 12:18
  • missing string null termination at `InsertArrayIntoArray` when copying chars. – Jean-François Fabre Sep 27 '16 at 12:18
  • 2
    There are also other problems, like the `sizeof(theArray) / sizeof(theArray[0])` "trick" you have in the `emptyArray` function. That will not work because `theArray` is a *pointer* so `sizeof(theArray)` will give you the size of the pointer and not what it points to. – Some programmer dude Sep 27 '16 at 12:19
  • Finally, *learn how to use a debugger*! With a debugger you can step through your code line by line while monitoring variables. This is an essential skill for all programmers, no matter if it's just as a hobby or professionally. – Some programmer dude Sep 27 '16 at 12:20
  • First of all i dont see the problem where getchar returns an int ?? It returns ASCII Code of the character in input,whats the problem there ?? And thank you @JoachimPileborg for pointing out that sizeof(theArray).I think that might be the bug. – Silidrone Sep 27 '16 at 12:42
  • @JoachimPileborg I dont really know any debugger for C.Do you have any suggestions ? – Silidrone Sep 27 '16 at 12:48
  • `getchar` returns an `int` because sometimes `(char) EOF == (int) EOF` is *false*. – Some programmer dude Sep 27 '16 at 12:55
  • As for debuggers, it depends on your environment and operating systems. Use your favorite search engine to find out more. – Some programmer dude Sep 27 '16 at 12:55
  • @MuhamedCicak In simplified terms: `getchar` returns an `unsigned char` that gets casted to an `int`. The `unsigned char` to get all possible bytes and the `int` to be able to return `EOF` which is in most cases just `-1`. You can do it because an `unsigned char` cannot be negative by definition. All states together do not fit into a single byte, so an `int` is needed. – deamentiaemundi Sep 27 '16 at 12:58
  • @deamentiaemundi: Slight correction: the `unsigned char` is not _cast_ to an `int`, but converted (how this is done is a matter of the library, but any sane programmer will not use an explicit cast). Nit-pick: An `int` can also be in a single byte. – too honest for this site Sep 27 '16 at 13:11
  • @deamentiaemundi Hi, i am not sure i understand you.I am pretty new to C so i am not really familiar with it.Maybe you are right,but i dont know how my code works.I found the solution.But it wasnt anything related to *c being int*.It is something in InsertArrayIntoArray Function. – Silidrone Sep 27 '16 at 14:04
  • @MuhamedCicak so `EOF` can be represented in a `char` on _your_ system, but that is not true for _all_ systems. Storing the return value of `getchar()` in a `char` and comparing to `EOF` is _not portable_. – Ian Abbott Sep 27 '16 at 14:46
  • The problem – assuming 8-bit chars – is that you're trying to store **257 values** in a type that can store **256 values**: 256 characters + EOF. Possibly on your system `char` is a signed value and so it can possibly represent `EOF`, but doing so you will for example misinterpret the character value *255* as `EOF` as well. The designers of the function `getchar()` knew what they were doing when they chose for it to return an `int`. You're trying to outsmart them. – Ilja Everilä Sep 28 '16 at 06:21

1 Answers1

0

First of all the sizeof(theArray) cannot be taken because theArray is pointer as Joachim Pileborg pointed out.And the second mistake is in InsertArrayIntoArray function. It should be like this :

void InsertArrayIntoArray(char to[MAXARRAYSIZE][MAXARRAYSIZE], char which[], int toSize, int whichSize) {
    int i, j;
    //printf("\n To size: %d \t Which Size: %d", toSize, whichSize);
    for (j = 0; j < whichSize; j++) {
        to[toSize][j] = which[j];
    }
}
Silidrone
  • 1,471
  • 4
  • 20
  • 35