Thread: C - functions not calling in while loop

  1. #1
    Registered User
    Join Date
    Oct 2020
    Posts
    11

    C - functions not calling in while loop

    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:

    C - functions not calling in while loop-output-jpg

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > display(choice);
    C doesn't do pass by reference.

    > void display(int choice)
    You have a return choice at the end.
    This won't even compile without a warning.

    Code:
    int display(void){
        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: ");
        int choice;
        scanf("%d", &choice);
        return choice;
    }
    Then in main.
    Code:
    while ( (choice=display()) != 0 ) {
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calling functions
    By turbodog66 in forum C++ Programming
    Replies: 1
    Last Post: 03-10-2017, 02:08 AM
  2. Calling functions via a for loop
    By bos1234 in forum C Programming
    Replies: 0
    Last Post: 09-18-2013, 08:11 PM
  3. calling functions
    By njasmine1 in forum C Programming
    Replies: 2
    Last Post: 12-29-2010, 10:28 AM
  4. Calling functions help
    By ForlornOdium in forum C++ Programming
    Replies: 14
    Last Post: 09-29-2003, 08:40 PM
  5. Functions calling themselves...?
    By mikebrewsj in forum C++ Programming
    Replies: 10
    Last Post: 01-18-2002, 12:09 AM

Tags for this Thread