0

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.

OmniOwl
  • 5,477
  • 17
  • 67
  • 116
  • 1
    to start with, you forgot to include "stdbool.h" – Sourav Ghosh Nov 21 '14 at 12:36
  • 1
    You never actually set end. You pass it to a function, put this does not change its value unless it is a pointer. I would suggest changing the function from a void to a boolean here, then when you return, just check that value. – Hashman Nov 21 '14 at 12:36

5 Answers5

3

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);
Ashalynd
  • 12,363
  • 2
  • 34
  • 37
2

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

oopcode
  • 1,912
  • 16
  • 26
2

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))

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
2

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;
}
Omer Eldan
  • 1,757
  • 1
  • 11
  • 10
  • That's the first time I've had to use a pointer. But given all the other answers to this question, I see now why I have to. Thank you for pointing it out! – OmniOwl Nov 21 '14 at 12:43
1

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)

Gophyr
  • 406
  • 2
  • 11