If you type "What kind of movies do you like?" in da.txt, you want to display "What kind of movies do you like?"

Asked 1 years ago, Updated 1 years ago, 38 views

Problem 1
"When I asked ""What kind of movies do you like?"", they replied ""What kind of movies do you like?"" which I saved in da.xt, which was originally made in advance, ""I don't have any particular kind of movies in particular."
If the characters do not match, perhaps because of garbled characters, "Sorry.What is ○○?

memoy value: 0 buffer value: buffer value: Why do you like movies?
Why do you like movies?What is ?

To avoid garbled characters, I used getline(buffer, inputmozi);//str to store strings, but it didn't work.

Problem 2
I tried to display the contents of buffer and buffer2, but it didn't work and nothing came up.

I did everything I could to debug problem 1 and problem 2, but I couldn't solve it.
So please let me know in detail how to debug it until it is resolved.

The environment is Windows 10, Visual Studio 2019.

Current Source Code

#pragma warning (disable:4996)
# include <string>
# include <Windows.h>
# include <iostream>
    
char String [256];
int InputHandle;
int InputHandleA;
int modoru = 0;
int mozicount = 0;
int gimonnlock = 0;
int gimon = 0;

int my_str2(const char*s1, const char*s2)// The character string entered here and the prepared character string are treated as arguments.

{
    As the size of the character string s2 is required only to use a function comparing //s1, s2, information s2 of the character string prepared in the variable a is passed as an argument to a function strlen for measuring the length of the character string.
    const size_ta = strlen(s2);
    // There is an infinite loop.
    for(;;){
        // If the return value of the function memcmp is 0, it is the time of matching, so == 0.
        if(memcmp(s1,s2,a) == 0)

            Return1;// Configured to return 1 if the string entered contains the specified string.
// If the entered string reaches the last character, there is no matching string, so I decided to return 0.
        else if (*s1 == '\0')

            return 0;// was not included
// The number of bytes of the character code for one character of the inputted character string is advanced even if the character string matches but no matching character string exists.
        else

            ++s1;

    }

}

int memory = 0;
Char buffer [256]; // ★ InputHandle is the type of variable in which character data can be received.
char buffer2 [256];
char*p;
FILE* outputfile;// output stream
int inputmozi = 0;
int hyouzi = 0;
unsigned charuchr;
void getline(chars[], intlim){  
    int c, i; // getchar receive variable c, loop variable i  
      
    for(i=0;i<lim-1&&(c=getchar())!='\n';++i)  
        s[i] = c;  
      
    s[i] = '\0';  
}  
void getline(charline[], int maxline);

int main (void)
{
    printf("memoy" value: %d", memory);
    printf("buffer value: %s", buffer);
    printf("buffer value: %s", buffer2);

    // Processing new words

        //  Waiting for keyboard input
        inputmozi=getchar();
        getline(buffer, inputmozi); // string stored in str  
        if(my_str2(buffer, "remember")}{
            printf("What do you remember?");
           ++ mozicount;
            memory = 1;
            gimon = 0;
        }
        // I tried to get into the loop by reading a note except for the word "remember".
        else if(memory==0){
            outputfile=fopen("da.txt", "r"); // Open file for loading (open)
            if (outputfile==NULL) {// Open failed
                printf("cannot open\n");// with error message
                exit(1);// Abnormal termination
            }
            // Repeat until fgets are NULL
     // fgets(str,256,lf)! = NULL
     Same as //.It can be shortened like this.
            // ★ buffer contains a character input string, so you can't put a character string from a memo here.So I'm going to make a new buffer 2.
            while((fgets(buffers2,256,outputfile)))!=NULL)// Put the string written in the memo into buffer2.
            {
                if(my_str2(buffer, "movie")&my_str2(buffer, "I like you")&my_str2(buffer, "what")&my_str2(buffer, "what")&my_str2(buffer, "?") ||
                    my_str2(buffer, "movie")&my_str2(buffer, "I like you")&my_str2(buffer, "?")) {
                    ++ mozicount;
                    gimonnlock=1;
                    if(my_str2(buffer2, "movie")&my_str2(buffer, "what")){
                        hyouzi = 1;
                        break;
                    }
                }
               // When a character matches a buffer containing characters from a note and a buffer containing characters from a note.
                if(strcmp(buffer, buffer2)==0){
                    hyouzi = 1;
                }
                // Buffer with characters input and buffer 2 with characters from memo appear that do not match.
                if(hyouzi==0){
                    if(strcmp(buffer, buffer2)==1){
                        gimon=1;
                    }
                }
            }

            fclose (outputfile); // Close file (close)

        }
        if(memory==1&my_str2(buffer, "what is") or memory==1&my_str2(buffer, "is") or memory==1&my_str2(buffer, "is"){
            ++ mozicount;
            memory = 2;
        }
    // What to do when the Enter key is not pressed?
    if(memory==2){   
        // For example, for int, 49 is the same value, but for char, 49 is the character code for 1 // It's like this conversion.
        outputfile=fopen("da.txt", "a+"; // Open file for write (open)
        if (outputfile==NULL) {// Open failed
            printf("cannot open\n");// with error message
            exit(1);// Abnormal termination
        }
        fprintf(outputfile, "%s\n", buffer); // Write to file
        fclose (outputfile); // Close file (close)
        // fclose(fp); // The window disappears when you write to a file, so before you write to the moment memory,
        // The window disappears because the file closes.
        memory = 0;
        // return 0; // If it reaches 0 here, it will end, so I will erase it so that I can type words even after writing.

    }

    if(hyouzi==1){
        printf("%s", buffer2);

    }
    if(hyouzi==0&gimon==1){
        printf("What is %s?", buffer);
    }

    // Closing
    return 0;
}

c++

2022-09-30 19:57

1 Answers

inputmozi=getchar();
getline(buffer, inputmozi); // string stored in str

is wrong.

getline The second parameter that you pass to the function is that you create your own function to specify the maximum length of the string. I think you should specify sizeof(buffer).

Also, inputmozi=getchar(); for the first byte (first half of full-width characters)
getline has been truncated, so the first full-width character is garbled.

Debugging Methods

If it's Visual Studio, check the contents of [F10] [F11] and variables in the debug step. It is recommended that you do it step by step.

If you can predict to some extent, you should set breakpoints and focus on the surrounding areas.

Tutorial: Understanding C++ Debugging Using Visual Studio

will be helpful.


2022-09-30 19:57

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.