Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C problem

Name: Anonymous 2012-10-30 23:27

Why the fuck this isn't working???

#include <stdio.h>

#define MAX 1000

int main(){
    char line[MAX];
    int lgh;
    line[0] = '\0';
    while ((lgh = getLine(line, MAX)) != 0){
        printf("%s\n", line);
        line[0] = '\0';
    }
    return 0;
}

int getLine(char s[], int length){
    char s2[length];
    int i, ii, qttWord = 0, qttWord2 = 0;
    int c; // c = getchir() d = EOF
    char flag = '\0';
   
    s2[0] = '\0';
    /*Reads the input and puts it into s[], then, verifies if the input is just \n,
    * if so, returns 0(i), if not, puts '\n' at the end of the string ind '\0' to close
    */
    for (i = 0; i < length-1 && (c = getchar()) != EOF && c != '\n'; ++i){
            s[i] = c;
            ++qttWord;
    }
    if (i == 0){
        if (c == '\n')
            return 0;
    } else if (c == '\n'){
        ++i;
        s[i] = '\0';
    }
    /*Verifies if the string is just ' ' or '\t'
    * if so, returns 0
    */
    for (i = 0; i < qttWord && flag != 1; ++i){
        if (s[i] == ' ' || s[i] == '\t'){
            flag = '0';
        } else{
            flag = '1';
        }
    }
    if (flag == '0')
        return 0;
    /*
    *The trim function
    */
    for (i = 0; i < qttWord; ++i){
        if (i < qttWord-1){
            if (s[i] == ' ' && s[i+1] != ' '){
                s2[i] = s[i];
                ++qttWord2;
                printf("1%d\n", s2[i]);//test thing
            }
        }
        if (s[i] != ' '){
            s2[i] = s[i];
            ++qttWord2;
            printf("0%d\n", s2[i]);//test thing
        }
    }
    for (i = 0; i < qttWord2+1; ++i){
        s[i] = s2[i];
    }
   
    return 1;
}

Name: Anonymous 2012-10-30 23:32

Because you are an idiot and a shitty program. If you had bothered to step through your code (or put absolutely any effort in to this at all), you would have caught the mistake that is glaring me in the fucking face.

Get the fuck out you undereducated idiot. I'm not doing your homework for you.

Name: Anonymous 2012-10-30 23:33

It's a read input/trim function.

Name: Anonymous 2012-10-30 23:34

Oops, update:

#include <stdio.h>

#define MAX 1000

int main(){
    char line[MAX];
    int lgh;
    line[0] = '\0';
    while ((lgh = getLine(line, MAX)) != 0){
        printf("%s\n", line);
        line[0] = '\0';
    }
    return 0;
}

int getLine(char s[], int length){
    char s2[length];
    int i, ii, qttWord = 0, qttWord2 = 0;
    int c; // c = getchir() d = EOF
    char flag = '\0';
   
    s2[0] = '\0';
    /*Reads the input and puts it into s[], then, verifies if the input is just \n,
    * if so, returns 0(i), if not, puts '\n' at the end of the string ind '\0' to close
    */
    for (i = 0; i < length-1 && (c = getchar()) != EOF && c != '\n'; ++i){
            s[i] = c;
            ++qttWord;
    }
    if (i == 0){
        if (c == '\n')
            return 0;
    } else if (c == '\n'){
        ++i;
        s[i] = '\0';
    }
    /*Verifies if the string is just ' ' or '\t'
    * if so, returns 0
    */
    for (i = 0; i < qttWord && flag != 1; ++i){
        if (s[i] == ' ' || s[i] == '\t'){
            flag = '0';
        } else{
            flag = '1';
        }
    }
    if (flag == '0')
        return 0;
    /*
    *The trim function
    */
    for (i = 0; i < qttWord; ++i){
        if (i < qttWord-1){
            if (s[i] == ' ' && s[i+1] != ' '){
                s2[i] = s[i];
                ++qttWord2;
                printf("1%d\n", s2[i]);//test thing
            }
        }
        if (s[i] != ' '){
            s2[i] = s[i];
            ++qttWord2;
            printf("0%d\n", s2[i]);//test thing
        }
    }
    for (i = 0; i < qttWord2+1; ++i){
        s[i] = s2[i];
    }
   
    return 1;
}

Name: Anonymous 2012-10-30 23:36

Fuck I'm quite drunk...

#include <stdio.h>

#define MAX 1000

int main(){
    char line[MAX];
    int lgh;
    line[0] = '\0';
    while ((lgh = getLine(line, MAX)) != 0){
        printf("%s\n", line);
        line[0] = '\0';
    }
    return 0;
}

int getLine(char s[], int length){
    char s2[length];
    int i, ii, qttWord = 0, qttWord2 = 0;
    int c; // c = getchir() d = EOF
    char flag = '\0';
   
    s2[0] = '\0';
    /*Reads the input and puts it into s[], then, verifies if the input is just \n,
    * if so, returns 0(i), if not, puts '\0' at the end of the string.
    */
    for (i = 0; i < length-1 && (c = getchar()) != EOF && c != '\n'; ++i){
            s[i] = c;
            ++qttWord;
    }
    if (i == 0){
        if (c == '\n')
            return 0;
    } else if (c == '\n'){
        ++i;
        s[i] = '\0';
    }
    /*Verifies if the string is just ' ' or '\t'
    * if so, returns 0
    */
    for (i = 0; i < qttWord && flag != 1; ++i){
        if (s[i] == ' ' || s[i] == '\t'){
            flag = '0';
        } else{
            flag = '1';
        }
    }
    if (flag == '0')
        return 0;
    /*
    *The trim function
    */
    for (i = 0; i < qttWord; ++i){
        if (i < qttWord-1){
            if (s[i] == ' ' && s[i+1] != ' '){
                s2[i] = s[i];
                ++qttWord2;
                printf("1%d\n", s2[i]);//test thing
            }
        }
        if (s[i] != ' '){
            s2[i] = s[i];
            ++qttWord2;
            printf("0%d\n", s2[i]);//test thing
        }
    }
    for (i = 0; i < qttWord2+1; ++i){
        s[i] = s2[i];
    }
   
    return 1;
}

Name: Anonymous 2012-10-30 23:51

Segmentation fault (core dumped)
Segmentation fault (core dumped)
Segmentation fault (core dumped)
Segmentation fault (core dumped)
Segmentation fault (core dumped)
Segmentation fault (core dumped)
Segmentation fault (core dumped)
Segmentation fault (core dumped)
Segmentation fault (core dumped)
Segmentation fault (core dumped)
Segmentation fault (core dumped)
Segmentation fault (core dumped)

Name: Anonymous 2012-10-31 0:00

flag != 1

Name: Anonymous 2012-10-31 2:32

You‘re moron

Name: Anonymous 2012-10-31 6:05

What a fucking mess, think before you do.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List