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");
}
}