So I'm trying to use a while loop to repeat a menu of options for the user to select until they enter 0 to exit. The issue is when I attempt to put my separate my code into different files to make my program more organized, the functions in my while loop in main.c no longer call for some reason.
The first output is when I put everything in main.c. As you can see, the print function works correctly, but when I attempt to move my menu to a different file and display it by calling a function, the user can continuously enter a bunch of options but the functions for each option never call. I was wondering how to fix this?
I want the user to enter '1', print the inventory as shown in the first output, and then be asked to enter another number again. But as you can see in the second output, when the user enters 1, the print function doesn't call for some reason.
Aside from the break statements, I'm calling print before I break so I'm confused as to why it's not printing anything at all?
First output:
main.c
Code:
int choice;
printf("INVENTORY MANAGEMENT SYSTEM \n");
printf("(1) Print inventory \n");
printf("(2) Add product stock \n");
printf("(3) Buy product \n");
printf("(0) Exit \n");
printf("\n");
printf("Please enter your choice: ");
scanf("%d", &choice);
while(choice != 0){
if(choice == 1){
printProducts(p);
}
if(choice == 2){
int enterId, numUnits;
printf("Product id: ");
scanf("%d", &enterId);
printf("Number of units: ");
scanf("%d", &numUnits);
}
}
Second output:
main.c
Code:
int choice;
while(choice != 0){
display(choice);
if(choice == 1){
printProducts(p);
}
if(choice == 2){
int enterId, numUnits;
printf("Product id: ");
scanf("%d", &enterId);
printf("Number of units: ");
scanf("%d", &numUnits);
}
}
Display.c
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "defs.h"
void display(int choice){
printf("INVENTORY MANAGEMENT SYSTEM \n");
printf("(1) Print inventory \n");
printf("(2) Add product stock \n");
printf("(3) Buy product \n");
printf("(0) Exit \n");
printf("\n");
printf("Please enter your choice: ");
scanf("%d", &choice);
return choice;
}
Output: