简体   繁体   中英

an Assert Error pops up after running code

断言失败 There are two functions in this code, and they both seem to be working perfectly until I run the code. I get the above error after running this code and I really don't know what to do to fix the error, and also the user input doesn't seem to work but the test cases I put into the code work perfectly.

All the code is meant to do, is take user inputs(strings) and then print out the integers that were typed in

int findInteger(const char str[81], char** end) {
    int i = (int)(*end - str), j = 0;
    bool isNum = false;
    char str1[40];
    char prev = ' ';//instead of str[0], if number at beginning
    while (str[i]) {   //str[i]!=0
        if (isdigit(str[i])) {
            if (!isNum && (prev == ' ' || prev == '\t')) isNum = true;
            if (isNum) {
                str1[j] = str[i];
                j += 1;
            }
        }
        else {
            if (isNum) {
                if (str[i] == ' ' || str[i] == '\t') {
                    str1[j] = 0;
                    *end = (char*)&(str[i]);
                    return atoi(str1);
                }
                else {
                    isNum = false; j = 0;
                }
            }
        }
        prev = str[i];
        i += 1;
    }
    if (isNum) {
        str1[j] = 0;
        *end = (char*)&(str[i]);
        return atoi(str1);
    }
    *end = (char*)&(str[i]);
    return NULL;
}

int getNumbers(const char str[81], int numbers[40]) {
    char* end = (char*)str;
    int c = 0, num;
    while (*end) {
        if ((num = findInteger(str, &end)) != NULL) {
            numbers[c] = num;
            c += 1;
        }
    }
    return c;
}

int main(void) {
    char str[3][81] = { "46 adfg 123 163 asd a    723 hdhaj dsa 87 dg 24",
            "456 asdfg 12 53 dhgajasda    723 hdhaj dsa 87 dg",
            "46 dfg 123 23 dhgaj 163 asd hdhaj dsa 37 dg 33" };
    for (int i = 0; i < 3; i++) {
        int numbers[40];
        int len = getNumbers(str[i], numbers);
        printf("len=%d\n", len);
        for (int j = 0; j < len; j++) printf("num=%d ", numbers[j]);
        printf("\n");
    }
    printf("\n");

    char string[20][81];
    int num[40];
    printf("Type your input:\n");
    scanf("%d", string);

    for (int i = 0; i < 20; i++) {
        int len = getNumbers(string[i], num);
        printf("length = %d\n", len);
        for (int j = 0; j < len; j++) printf("num=%d ", num[j]);
        printf("\n");
    }
    return 0;
}

断言失败 There are two functions in this code, and they both seem to be working perfectly until I run the code. I get the above error after running this code and I really don't know what to do to fix the error, and also the user input doesn't seem to work but the test cases I put into the code work perfectly.

All the code is meant to do, is take user inputs(strings) and then print out the integers that were typed in

int findInteger(const char str[81], char** end) {
    int i = (int)(*end - str), j = 0;
    bool isNum = false;
    char str1[40];
    char prev = ' ';//instead of str[0], if number at beginning
    while (str[i]) {   //str[i]!=0
        if (isdigit(str[i])) {
            if (!isNum && (prev == ' ' || prev == '\t')) isNum = true;
            if (isNum) {
                str1[j] = str[i];
                j += 1;
            }
        }
        else {
            if (isNum) {
                if (str[i] == ' ' || str[i] == '\t') {
                    str1[j] = 0;
                    *end = (char*)&(str[i]);
                    return atoi(str1);
                }
                else {
                    isNum = false; j = 0;
                }
            }
        }
        prev = str[i];
        i += 1;
    }
    if (isNum) {
        str1[j] = 0;
        *end = (char*)&(str[i]);
        return atoi(str1);
    }
    *end = (char*)&(str[i]);
    return NULL;
}

int getNumbers(const char str[81], int numbers[40]) {
    char* end = (char*)str;
    int c = 0, num;
    while (*end) {
        if ((num = findInteger(str, &end)) != NULL) {
            numbers[c] = num;
            c += 1;
        }
    }
    return c;
}

int main(void) {
    char str[3][81] = { "46 adfg 123 163 asd a    723 hdhaj dsa 87 dg 24",
            "456 asdfg 12 53 dhgajasda    723 hdhaj dsa 87 dg",
            "46 dfg 123 23 dhgaj 163 asd hdhaj dsa 37 dg 33" };
    for (int i = 0; i < 3; i++) {
        int numbers[40];
        int len = getNumbers(str[i], numbers);
        printf("len=%d\n", len);
        for (int j = 0; j < len; j++) printf("num=%d ", numbers[j]);
        printf("\n");
    }
    printf("\n");

    char string[20][81];
    int num[40];
    printf("Type your input:\n");
    scanf("%d", string);

    for (int i = 0; i < 20; i++) {
        int len = getNumbers(string[i], num);
        printf("length = %d\n", len);
        for (int j = 0; j < len; j++) printf("num=%d ", num[j]);
        printf("\n");
    }
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM