be stuck in the loop

Asked 1 years ago, Updated 1 years ago, 357 views

We are working on the following issues, and we are working on loading files without sorting them once.

Creating Calculator Applications  Create an application that meets the following features:
 画面Please follow the example in principle when displaying the screen and the format of the log.

1) Ability to perform four operations
 2) Receive calculated numbers and operators from the console.    Example) 5+6
 3) US>View results on the console
   Example) 11
 4) Save the results and date, time, and seconds of the operation as
   The log limit should be 1000 lines (no consideration should be given to cases where the limit is exceeded)
   Example) Record the following in log.txt
     April 27, 2015 14:30:51, 5+6,11
 5) 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
     April 27, 2015 14:30:51, 7-6,1
     April 27, 2015 at 15:30:00, 7+8,15
     April 28, 2015 14:30:51, 8+9,17
   Example) Enter DESC
     April 28, 2015 14:30:51, 8+9,17
     April 27, 2015 at 15:30:00, 7+8,15
     April 27, 2015 14:30:51, 7-6,1
     April 26, 2015 10:20:00, 5+6,11

However, with Visual Studio debugging,

for(i=0;i<1000;i=i+1){
        fgets(sin[1000], sizeof(sin[0]), fp);
        if(strcmp(sin, "EOF")==0){
            break;
        }

I can't get out of the loop with this line.
Where should I fix it?

#include<stdio.h>
# include <time.h>
# include <string.h>
# include <stdlib.h>
/*
static charge [10];

intcmp_u(const void*a, const void*d){
    return strcmp((char*)a,(char*)d);
}

intcmp_d(const void*a, const void*d){
    return strcmp(char*)d,(char*)a);
}
*/
int main() {
    int num1, num2;
    charop;
    float answer;
    intr, i, n;
    FILE*fp;
    charc[11];
    charsin[1000][1000];
    charad[8];


    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", &c);
        if(strcmp(c, "no")==0){
            break;
        }
    }

    int count = 0;
    
    for(i=0;i<1000;i=i+1){
        fgets(sin[1000], sizeof(sin[0]), fp);
        if(strcmp(sin, "EOF")==0){
            break;
        }
    }
    
    fclose(fp);

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

    if(strcmp(ad, "ASC")==0){
        qsort(sin, ,sizeof(sin[0]),cmp_u);
    }
    else{
        qsort(sin, ,sizeof(sin[0]),cmp_d);
    }
*/
    for(i=1;i<=n;i++){
        printf("%s", sin[i]);
    }
    
        return 0;
}

c

2022-11-16 21:06

3 Answers

If you disable the compiler warning (level 3) C4996 that is incompatible with various commentary and sample programs by default, the build should not end successfully with the following error:(The format of the message has changed.)

 line type contents
93 Error Local variable 'n' not initialized is used
43 Warning '=': Conversion from 'int' to 'float'.Data can be lost.
       Same warning on lines 46 and 49
75 Warning 'function': Indirect reference level is different between 'const char*' and 'char[1000][1000]'.
75 Warning 'strcmp': The type of is different from the temporary and real arguments of 1.
24 Warning 'ad': The local variable has never been used.

You may have manipulated the properties of the project to create an executable file, but you should look into the errors and warnings displayed in IDE and deal with them.

In addition, even if the build passes after correcting the error in line 93, there are the following problems:

  • The proposed part is being processed with the file still open in additional mode and will result in errors or no results.
    → Once the log file is closed and reopened in read mode, it must be processed.
  • The argument sin in line 74 is incorrectly specified.
    → It is defined as a two-dimensional array, but only a one-dimensional index is specified.
    → The location (index) of the read data is always the same.
  • The File Read End decision on line 75 is incorrect.
    →Perhaps it was inferred from the fgetc() reading and termination determination, but the function I am using is fgets(), and the string "EOF" does not indicate the end of the file reading, and so on.

If the assignment comes from any course or course, you will only use the content that should have been explained before that, so please review what you have learned so far.

By the way, it has nothing to do with the end of the loop, but are all the problem sentences/conditions listed?
If everything is written in the questionnaire, I think the following conditions are ambiguous.
If you can ask questions to the questioner, you should ask them.

  • There are only examples of addition and subtraction, and no examples of multiplication and subtraction are provided.
  • All you have to do is say "numeric", but in the example, only integers are presented.
  • Is floating-point number acceptable for input?
  • If the result of the multiplication is a floating point number, is the output a floating point number or an integer?
  • If floating-point numbers are allowed, are there any restrictions or specifications on the format of the input/output values?

It may be part of a course or course, including being asked questions.


2022-11-16 22:32

As a reference in the event of a deadlock in the resolution, we have created an amendment based on the points of those who have already answered.

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

        /* scanf("%s\n", &c);*/
        scanf("%s",c);

    /* for(i=0;i<1000;i=i+1){*/
    /*     fgets(sin[1000], sizeof(sin[0]), fp); */
    /*     if(strcmp(sin, "EOF")==0){*/
    rewind(fp);
    for(i=0;i<1000;i++){
        if(fgets(sin[i], sizeof(sin[0], fp)==NULL){
            count=i;

    /* for(i=1;i<=n;i++){*/
    for(i=0;i<count;i++){


2022-11-16 23:32

fgets(sin[1000], sizeof(sin[0]), fp);
        if(strcmp(sin, "EOF")==0){

Since charsin[1000][1000]; is declared, you can only use sin[0] to sin[999], you cannot use sin[1000].I probably wanted to use fgets(sin[i], sizeof(sin[0]), fp).

fgets() results include line breaks.If you want to say EOF, you may want to compare it to "EOF\n".If you want to detect file termination instead of string "EOF", check the return value of fgets().

Also, fp is used to add data in the code, so it cannot be used to read data.If you want to read the file you added in the first half, you must fclose() and then fopen() or "a+" in the first fopen() mode and put the file position back to the beginning.


2022-11-17 04:25

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.