简体   繁体   中英

Program doesn't end after parsing “exit”

I am new to C and I am trying to learn, so go easy on me :D

I am currently trying to make a simple bank account console application. I got the following code:

#include <stdio.h>
#include <string.h>

void parseCommand(char cmd[15], bool end) {
    if(cmd != NULL) {
        if(strstr(cmd, "listaccounts") != NULL) {

        } else if(strstr(cmd, "addaccount") != NULL) {

        } else if(strstr(cmd, "removeaccount") != NULL) {

        } else if(strstr(cmd, "withdraw") != NULL) {

        } else if(strstr(cmd, "deposit") != NULL) {

        } else if (strstr(cmd, "exit") != NULL) {
            end = true;
        } else {
            printf("Unknown Command: %s\n", cmd);
        }
    } else {
        printf("cmd is null");
    }
}

int main() {
    printf("Welcome to the Account Management System\n");
    printf("Please use one of the following commands:\n");
    bool end = false;
    while(true) {
        printf("\tlistaccounts\n");
        printf("\taddaccount\n");
        printf("\tremoveaccount\n");
        printf("\twithdraw\n");
        printf("\tdeposit\n");
        printf("\texit\n");
        char cmd[15];
        fgets(cmd,15,stdin);
        parseCommand(cmd, end);
        if(end == true) {
            printf("Shutting down...");
            break;
        }
    }

    return 0;
}

But when I type "exit" the program just starts over in the while loop and asks for new input. What am I doing wrong? My guess would be how I try to compare two strings.

In C, the variables are passed by value. So whatever value end will have in your parseCommand method, the outer function will not see it. An easy way to fix it is to make your function to return end :

bool parseCommand(char cmd[15], bool end) {
///
  return end;
}


// in main:

end = parseCommand(cmd, end);

You're trying to modify the value of a variable declared outside the function. You need a pointer to it.

To compile, first add #include <stdbool.h> .


In your code, you're passing end using pass-by-value. This will create a local copy of end inside parseCommand() scope. Whatever it done with end value inside parseCommand() code will not be reflected to main() .

  1. change parseCommand(cmd, end); to parseCommand(cmd, &end);
  2. change void parseCommand(char cmd[15], bool end) to void parseCommand(char cmd[15], bool *end)
  3. change end = true; to *end = true;

Also, if(cmd != NULL) should be changed to if ((cmd != NULL) && (end != NULL))

If you want 'parseCommand' to change the variable from the main function, you should pass a pointer of it.

void parseCommand(char cmd[15], bool * end) {
if(cmd != NULL) {
    if(strstr(cmd, "listaccounts") != NULL) {

    } else if(strstr(cmd, "addaccount") != NULL) {

    } else if(strstr(cmd, "removeaccount") != NULL) {

    } else if(strstr(cmd, "withdraw") != NULL) {

    } else if(strstr(cmd, "deposit") != NULL) {

    } else if (strstr(cmd, "exit") != NULL) {
        *end = true;
    } else {
        printf("Unknown Command: %s\n", cmd);
    }
} else {
    printf("cmd is null");
}
}

int main() {
    printf("Welcome to the Account Management System\n");
    printf("Please use one of the following commands:\n");
    bool end = false;
    while(true) {
        printf("\tlistaccounts\n");
        printf("\taddaccount\n");
        printf("\tremoveaccount\n");
        printf("\twithdraw\n");
        printf("\tdeposit\n");
        printf("\texit\n");
        char cmd[15];
        fgets(cmd,15,stdin);
        parseCommand(cmd, &end);
        if(end == true) {
            printf("Shutting down...");
            break;
        }
    }

    return 0;
}

When you pass say void parseCommand(char cmd[15], bool end) , you are creating duplicate variables local to the scope of the function (you may want to look up scope). Thus bool end becomes a duplicate local to the function, and when the function finishes, it cannot be accessed again. You could use a pointer and make bool end into bool *end , or you could have the function return a bool , by saying bool parseCommand(char cmd[15], bool end)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM