편집 기록

편집 기록
  • 프로필 nowp님의 편집
    날짜2020.06.20

    숫자와 함께 숫자 이외의 알파벳들도 같이 입력받아서 int형 숫자로 출력하는 프로그램을 만들려하는데 코드하나가 오류가 나네요 이유를 모르겠어요


    #include<stdio.h>
    
    int myatoi(char[]);
    
    void main()
    {
        char string[81];
        int re;
        printf("Enter the string: \n");
        gets_s(string,81);
        re=myatoi(string);
        printf("result is %d", re);
    }
    
    int myatoi(char str[]) {
        int i = 0, j = 0;
    
        if (str[0] == '+' || str[0] == '-') {
            while (str[i] != '\0') {
                str[i] = str[i + 1];
                i++;
            }
        }
    
    
    
    
    
    
    //이 부분 이후에서 오류가 납니다.
    
    
    
        int p = 0;
        while (str[j] != '\0') {
            if (str[j] >= '0' && str[j] <= '9') {
                j++;
            }
            else {
                while (str[p] == '\0') {
                    str[p] = str[p + 1];
                    p++;
                }
            }
    
        }
            int k = 0, t = 0, m = 1, result = 0;
            while (str[i] != '\0') {
                k++;
            }
            for (t; t < k; t++) {
                m = m * 10;
            }
            i = 0;
            while (str[i] != '\0') {
                result = result + str[i] * m;
                m = m / 10;
                i++;
            }
            return result;
        }
    
  • 프로필 김태규님의 편집
    날짜2020.06.20

    숫자와 함께 숫자 이외의 알파벳들도 같이 입력받아서 int형 숫자로 출력하는 프로그램을 만들려하는데 코드하나가 오류가 나네요 이유를 모르겠어요


    include

    int myatoi(char[]);

    void main() { char string[81]; int re; printf("Enter the string: \n"); gets_s(string,81); re=myatoi(string); printf("result is %d", re); }

    int myatoi(char str[]) { int i = 0, j = 0;

    if (str[0] == '+' || str[0] == '-') {
        while (str[i] != '\0') {
            str[i] = str[i + 1];
            i++;
        }
    }
    

    //이 부분 이후에서 오류가 납니다ㅜ

    int p = 0;
    while (str[j] != '\0') {
        if (str[j] >= '0' && str[j] <= '9') {
            j++;
        }
        else {
            while (str[p] == '\0') {
                str[p] = str[p + 1];
                p++;
            }
        }
    
    }
        int k = 0, t = 0, m = 1, result = 0;
        while (str[i] != '\0') {
            k++;
        }
        for (t; t < k; t++) {
            m = m * 10;
        }
        i = 0;
        while (str[i] != '\0') {
            result = result + str[i] * m;
            m = m / 10;
            i++;
        }
        return result;
    }