Understanding the Output of C Language to Compilation

Asked 1 years ago, Updated 1 years ago, 225 views

For the following issues, an exception has been thrown, and the compilation is not outputting.
Could someone please tell me the corrections?

Receive parameters from the console and sort logs in ascending/descending order of runtime
US>Display on the console
Example) Enter ASC

 April 26, 2015 10:20:00, 5+6,11
2015/04/27 14:30:51, 7 - 6, 1
2015/04/27 15:30:00, 7 + 8, 15
2015/04/28 14:30:51, 8 + 9, 17

Example) Enter DESC

 April 28, 2015 14:30:51,8+9,17
2015/04/27 15:30:00, 7 + 8, 15
2015/04/27 14:30:51, 7 - 6, 1
2015/04/26 10:20:00, 5 + 6, 11
#include<stdio.h>
# include <time.h>
# include <string.h>
# include <stdlib.h>

intcmp_u(const void*a, const void*d){
    return*(int*)a-*(int*)d;
}

intcmp_d(const void*a, const void*d){
    return*(int*)d-*(int*)a;
}

int main() {
    int num1, num2;
    charop;
    float answer;
    intr, i;
    FILE*fp;
    chara[11];
    char sin [9];
    charad;

    fp = fopen("log.txt", "a");

    if(fp==NULL){
        printf("File Open Failed\n";
        return-1;
    }
    
    
    while(1){
        r = scanf("%d%c%d", & num1, & op, & num2);
        if(r!=3){
            puts("input error");
            return1;
        }

        if(op=='+'){
            answer = num1 + num2;
        }
        else if(op=='-'){
            answer = num1-num2;
        }
        else if(op=='*'){
            answer = num1 * num2;
        }
        else if(op=='/'){
            answer=(float) num1/num2;
        }

        time_tt = time(NULL);
        structure tm*tm=localtime(&t);
        printf("%d/%02d/%02d", tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday);
        printf("%02d:%02d:%02d", tm->tm_hour, tm->tm_min, tm->tm_sec);
        printf("%d%c%d,%f\n", num1, op, num2, answer);
        fprintf(fp, "%d/%02d/%02d", tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday);
        fprintf(fp, "%02d:%02d:%02d:%02d", tm->tm_hour, tm->tm_min, tm->tm_sec);
        fprintf(fp, "%d%c%d, %f\n", num1, op, num2, answer);

        printf("Do you want to continue the calculation?");
        scanf("%s\n", & a);
        if(strcmp(a, "no")==0){
            break;
        }
    }
        fclose(fp);

        printf("ASC or DESC:");
        scanf("%s", & ad);

        if(strcmp(a, "no")==0){
            qsort(sin,9,sizeof(int),cmp_u);
        }
        else{
            qsort(sin,9,sizeof(int),cmp_d);
        }


        for(i=0;i<9;i++){
            printf("%d\n", sin[i]);
        }
    
        return 0;
}

c

2022-11-14 17:17

1 Answers

I don't know what you're using for OS, compiler IDE, etc., but if you try to debug the results of compilation in debug mode, you'll see more detailed error messages, so you can look them up and re-examine them.

In this case, the following excerpts are caused by the fact that only one character must have at least 5 characters of space with 3 or 4 characters + termination \0.

charad;

        printf("ASC or DESC:");
        scanf("%s", & ad);

If you define the ad definition as an array of five or more char characters instead of a single char, no exceptions will occur.

charad[5];

        printf("ASC or DESC:");
        scanf("%s", & ad);

However, in the program source code provided, the parts that read, sort, and display log files are incomplete (nearly created), so there is still a long way to go.


2022-11-14 20:57

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.