About Break in C Language

Asked 1 years ago, Updated 1 years ago, 264 views

I'm trying to set up a program that ends when no is entered in the code below, but it doesn't stop and it loops indefinitely.
Where should I correct it?

#include<stdio.h>
# include <time.h>

int main() {
    int num1, num2;
    charop;
    float answer;
    intr,a;
    FILE*fp;
    int cnt = 0;

    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;
        }
        cnt++;

        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(a=='no'){
            break;
        }
    }
        fclose(fp);
    
        return 0;
}

c

2022-11-12 02:31

1 Answers

If you can compile and execute with the current source code, it may depend on the compiler, but is it "unstoppable and infinite looping" in that state?

Isn't the input (scanf() function not completed and if statement processing not completed?

Otherwise, would you like to continue the calculation? If you can enter something after the display, and then return to the beginning of the loop to enter and process the formula, would you like to continue the calculation?

instead of no display.

Maybe that will break the loop and end it.

In that case, the program remains wrong (or behaving badly) for the C language, but if you reverse the n and o lines like if(a=='on'){, you should end it if a person enters no.

The hint is that the character constant 'no' is treated as a numeric 0x00006e6f of type int.
And if it works with the current source code, the value of a after entering no can be 0x00006f6e.
Therefore, if you enter on, the value of a may become 0x00006e6f and break/terminate.

Although

is actually wrong for a program in C language, as there are a lot of comments.
    The
  • scanf() function uses \n as a character string indicating the format of input data.
    This will require you to enter exactly up to \n, so you don't need it here.
    For example, refer to the end of the scanf function page

  • A variable of type int is used for storing input data in the
  • scanf() function.
    If you want to store the string (%s), an array of type char should be sufficient in size.

  • You cannot use == to compare whether the string (array of char) is the same in the C language.
    You must use the strcmp function (case sensitive) or the strcmp function (case sensitive) to compare strings.

  • You cannot use single quotation to describe a fixed string for comparison when comparing strings.
    It must be enclosed in a double quotation.

In the scanf() function, \n is used as a character string indicating the format of the input data.
This will require you to enter exactly up to \n, so you don't need it here.
For example, refer to the end of the scanf function page

A variable of type int is used for storing input data in the scanf() function.
If you want to store the string (%s), an array of type char should be sufficient in size.

You cannot use == to compare whether the string (array of char) is the same in C language.
You must use the strcmp function (case sensitive) or the strcmp function (case sensitive) to compare strings.

You cannot use single quotation to describe a fixed string for comparison when comparing strings.
It must be enclosed in a double quotation.

So I took the following excerpt from the source code of the question:

intr,a;

        scanf("%s\n", & a);
        if(a=='no'){

You can change it like this.(stricmp so that you can also use NO)

inter;
    chara[16] = {0};

        scanf("%s", a);
        if(stricmp(a, "no")==0){


2022-11-12 03:21

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.