Thread: Program only runs the first function, then stops....

  1. #1
    Registered User
    Join Date
    Aug 2015
    Posts
    12

    Program only runs the first function, then stops....

    Onle the get_user_choice runs, then the program exits. I emailed my code to my professor and this is what he responded with:

    You are calling get user choice, but you aren't ever returning anything from that function. Your choice variable in main doesn't ever get changed.

    I'm not sure what this means or how to solve the issue. I thought that using scanf assigned the value to the global variable. Help would is greatly appreciated. Here is the code:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    
    void get_user_choice(int choice, char dump);
    void change_shift(int choice, char dump, int shift);
    void get_string(char message [500]);
    void encrypt(int choice, char message[500], int shift);
    void decrypt(int choice, char message[500], int shift);
    
    
    int main()
    {
            int choice;
            char dump;
            int shift = 3;
            char message[500];
    
    
    
    
            while (choice != 4)
            {
    
    
                    get_user_choice(choice, dump);
    
    
                    if (choice == 1)
                    {
                            change_shift(choice, dump, shift);
                    }
                    else if (choice == 2)
                    {
                            get_string(message);
                            encrypt(choice, message, shift);
                    }
                    else if (choice == 3)
                    {
                            get_string(message);
                            decrypt(choice, message, shift);
                    }
    
    
                    else
                    {
                            return 0;
                    }
            }
    }
    
    End code \\\
    void get_user_choice(int choice, char dump)
    {
            printf("\n1: Change the shift (default 3)\n");
            puts("2: Encrypt a message");
            puts("3: Decrypt a message");
            puts("4: Quit");
            printf("Option: ");
            scanf("%d%c", &choice, &dump);
    
    
            printf("\n");
    }
    
    
    void change_shift(int choice, char dump, int shift)
    {
            printf("Choose a shift: ");
            scanf("%d%c", &shift, &dump);
    }
    
    
    void get_string(char message[500])
    {
            printf(" Input: ");
            fgets(message, 500, stdin);
            printf("\n");
    }
    
    
    void encrypt(int choice, char message[500], int shift)
    {
            int i = 0;
    
    
            if (choice == 2)
            {
    
    
                    printf("Output: ");
    
    
                    while (message[i] != '\0')
                    {
                            message[i] += shift;
                            printf("%c", message[i++]);
                    }
                    printf("\n\n");
            }
    }
    
    
    void decrypt(int choice, char message[500], int shift)
    {
    
    
            int i = 0;
    
    
            if (choice == 3)
            {
    
    
                    printf("Output: ");
    
    
                    while (message[i] != '\0')
                    {
                            message[i] -= shift;
                    }
                    printf("\n\n");
            }
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Drunk_Eskimo
    I'm not sure what this means or how to solve the issue.
    Basically, you should define the function like this:
    Code:
    int get_user_choice(void)
    {
        int choice;
        char dump;
        /* code that gets the user's choice
           ... */
        return choice;
    }
    This way, you return a value -- the user's choice -- from get_user_choice. This allows you to change the variable named choice in main, e.g.,
    Code:
    choice = get_user_choice();
    Notice that I removed both choice and dump from the parameter list: choice is to be returned; dump is implementation detail.

    Quote Originally Posted by Drunk_Eskimo
    I thought that using scanf assigned the value to the global variable.
    You don't have any global variables: the variable named choice in main and the variable named choice in get_user_choice are two different variables, each local to the respective function. That is a Good Thing; keep it that way.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2013
    Posts
    228
    You are passing 'choice' and 'dump' by value, instead of by reference (see here for the difference).

  4. #4
    Registered User
    Join Date
    Aug 2015
    Posts
    12
    Thanks for the help! I've made changes, but now I run into a problem when I try to compile:

    It says:
    In function 'main':
    c:22:10: error: void value not ignored as it ought to be
    In function 'get_user_choice':
    c:64:2: warning: 'return' with a value, in function returning void [enabled by default]




    Heres the code changes:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    
    void get_user_choice();
    void change_shift(int shift);
    void get_string(char message [500]);
    void encrypt(char message[500], int shift);
    void decrypt(char message[500], int shift);
    
    
    int main()
    {
            int choice;
            int shift = 3;
            char message[500];
    
    
            get_user_choice();
    
    
            while (choice != 4)
            {
                    int choice;
                    choice = get_user_choice();
    
    
                    if (choice == 1)
                    {
                            change_shift(shift);
                            get_user_choice();
    
    
                    }
                    else if (choice == 2)
                    {
                            get_string(message);
                            encrypt(message, shift);
                            get_user_choice();
    
    
                    }
                    else if (choice == 3)
                    {
                            get_string(message);
                            decrypt(message, shift);
                            get_user_choice();
    
    
                    }
                    else
                    {
                            return 0;
                    }
            }
    }
    
    
    void get_user_choice()
    {
            int choice;
            char dump;
    
    
            printf("\n1: Change the shift (default 3)\n");
            puts("2: Encrypt a message");
            puts("3: Decrypt a message");
            puts("4: Quit");
            printf("Option: ");
            scanf("%d%c", &choice, &dump);
            printf("\n");
    
    
            return choice;
    }
    
    
    void change_shift(int shift)
    {
            printf("Choose a shift: ");
            scanf("%d", &shift);
    }
    
    
    void get_string(char message[500])
    {
            printf(" Input: ");
            fgets(message, 500, stdin);
            printf("\n");
    }
    
    
    void encrypt(char message[500], int shift)
    {
            int i = 0;
    
    
            printf("Output: ");
    
    
            while (message[i] != '\0')
            {
                    message[i] += shift;
                    printf("%c", message[i++]);
            }
            printf("\n\n");
    
    
    }
    
    
    void decrypt(char message[500], int shift)
    {
    
    
            int i = 0;
    
    
            printf("Output: ");
    
    
            while (message[i] != '\0')
            {
                    message[i] -= shift;
            }
            printf("\n\n");
    
    
    }

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Notice that you declared your function as having a return type of void:
    Code:
    void get_user_choice();
    Compare with my example from post #2. Furthermore, since the function has no parameters, you should use void in the parameter list.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Aug 2015
    Posts
    12
    I got it all working. Thanks again for the help everyone!

  7. #7
    Registered User
    Join Date
    Aug 2015
    Posts
    12
    Final code for those interested:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    
    int get_user_choice();
    int change_shift();
    void get_string(char message [500]);
    void encrypt(char message[500], int shift);
    void decrypt(char message[500], int shift);
    
    
    int main()
    {
            int shift = 3;
            int choice;
            char message[500];
    
    
            while (choice != 4)
            {
                    choice = get_user_choice();
    
    
                    if (choice == 1)
                    {
                            shift = change_shift();
    
    
                    }
                    else if (choice == 2)
                    {
                            get_string(message);
                            encrypt(message, shift);
    
    
                    }
                    else if (choice == 3)
                    {
                            get_string(message);
                            decrypt(message, shift);
    
    
                    }
                    else
                    {
                            return 0;
                    }
            }
    }
    
    
    int get_user_choice()
    {
            int choice;
            char dump;
    
    
            printf("\n1: Change the shift (default 3)\n");
            puts("2: Encrypt a message");
            puts("3: Decrypt a message");
            puts("4: Quit");
            printf("Option: ");
            scanf("%d%c", &choice, &dump);
            printf("\n");
    
    
            return choice;
    }
    
    
    int change_shift()
    {
            int shift = 3;
    
    
            printf("Choose a shift: ");
            scanf("%d", &shift);
    
    
            return shift;
    }
    
    
    void get_string(char message[500])
    {
            printf(" Input: ");
            fgets(message, 500, stdin);
    }
    
    
    void encrypt(char message[500], int shift)
    {
            int i = 0;
    
    
            printf("Output: ");
    
    
            while (message[i] != '\0')
            {
                    message[i] += shift;
                    printf("%c", message[i++]);
            }
            printf("\n");
    
    
    }
    
    
    void decrypt(char message[500], int shift)
    {
    
    
            int i = 0;
    
    
            printf("Output: ");
    
    
            while (message[i] != '\0')
            {
                    message[i] -= shift;
                    printf("%c", message[i++]);
            }
            printf("\n\n");
    
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program in c that runs on a PC but not on another
    By first100 in forum C Programming
    Replies: 2
    Last Post: 03-17-2015, 09:13 AM
  2. Program Stops Responding.
    By KittenAqua in forum C++ Programming
    Replies: 5
    Last Post: 12-16-2011, 05:26 PM
  3. program stops before EOF
    By doia in forum C Programming
    Replies: 2
    Last Post: 03-22-2010, 12:10 PM
  4. Replies: 2
    Last Post: 02-26-2009, 11:48 PM
  5. Program stops executing
    By bargomer in forum C++ Programming
    Replies: 16
    Last Post: 04-26-2008, 12:05 PM