Thread: Default Case Keeps Printing In Switch Statement

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    I'm a computer snoopfrogg's Avatar
    Join Date
    Feb 2019
    Posts
    29

    Default Case Keeps Printing In Switch Statement

    I'm working on a simple program that maintains parts in a database. The program tracks parts stored in a warehouse. Information about the parts is stored in an array of structures. The program is separated into different operations insert, update, and print.

    The program runs fine but for some reason the default case in my switch statement keeps printing out when I try to execute a new code.

    Here's what my program inventory.c looks like:
    Code:
    /* Maintains ia parts database (array version) */
    #include <stdio.h>
    #include "readline.h"
    
    #define NAME_LEN 25
    #define MAX_PARTS 100
    
    struct part{
        int number;
        char name[NAME_LEN + 1];
        int on_hand;
    };
    
    int find_part(int number, struct part inv[], int np);
    void insert(struct part inv[], int *np);
    void search(struct part inv[], int np);
    void update(struct part inv[], int np);
    void print(struct part inv[], int np);
    
    /********************************************************** 
     * main: Prompts the user to enter an operation code,     * 
     *       then calls a function to perform the requested   * 
     *       action. Repeats until the user enters the        * 
     *       command 'q'. Prints an error message if the user * 
     *       enters an illegal code.                          * 
     **********************************************************/
    int main(void){
    
        char code;
        struct part inventory[MAX_PARTS];
        int num_parts = 0; // number of parts currently stored
    
        for(;;){
            printf("Enter operation code: ");
            scanf("%c", &code);
            while(getchar() != '\n'); // skips to end of line 
            switch(code){
                case 'i':
                    insert(inventory, &num_parts);
                    break;
                case 's':
                    search(inventory, num_parts);
                    break;
                case 'u':
                    update(inventory, num_parts);
                    break;
                case 'p':
                    print(inventory, num_parts);
                    break;
                case 'q':
                    return 0;
                default:
                    printf("Illegal code\n");
            }
            printf("\n");
        }
    }
    
    /********************************************************** 
     * find_part: Looks up a part number in the inventory     * 
     *            array. Returns the array index if the part  * 
     *            number is found; otherwise, returns -1.     * 
     **********************************************************/
    int find_part(int number, struct part inv[], int np){
    
        int i;
        for(i = 0; i < np; i++){
            if(inv[i].number == number){
                return i;
            }
        }
        return -1;
    }
    
    /********************************************************** 
     * insert: Prompts the user for information about a new   * 
     *         part and then inserts the part into the        * 
     *         database. Prints an error message and returns  * 
     *         prematurely if the part already exists or the  * 
     *         database is full.                              * 
     **********************************************************/
    void insert(struct part inv[], int *np){
    
        int part_number;
        if(*np == MAX_PARTS){
            printf("Database is full; can't add more parts.\n");
            return;
        }
        printf("Enter part number: ");
        scanf("%d", &part_number);
        if(find_part(part_number, inv, *np) >= 0){
            printf("Part already exist\n");
            return;
        }
    
        inv[*np].number = part_number;
        printf("Enter part name: ");
        read_line(inv[*np].name, NAME_LEN);
        printf("Enter the quantity on hand: ");
        scanf("%d", &inv[*np].on_hand);
        (*np)++;
    }
    
    /********************************************************** 
     * search: Prompts the user to enter a part number, then  * 
     *         looks up the part in the database. If the part * 
     *         exists, prints the name and quantity on hand;  * 
     *         if not, prints an error message.               * 
     **********************************************************/
    void search(struct part inv[], int np){
    
        int i, number;
    
        printf("Enter part number: ");
        scanf("%d", &number);
        i = find_part(number, inv, np);
        if(i >= 0){
            printf("Part name: %s\n", inv[i].name);
            printf("Quantity on hand: %d\n", inv[i].on_hand);
        }
        else{
            printf("Part not found\n");
        }
    }
    
    /********************************************************** 
     * update: Prompts the user to enter a part number.       * 
     *         Prints an error message if the part doesn't    * 
     *         exist; otherwise, prompts the user to enter    * 
     *         change in quantity on hand and updates the     * 
     *         database.                                      * 
     **********************************************************/
    void update(struct part inv[], int np){
    
        int i, number, change;
        printf("Enter part number: ");
        scanf("%d", &number);
        i = find_part(number, inv, np);
        if(i >= 0){
            printf("Enter change in quantity on hand: ");
            scanf("%d", &change);
            inv[i].on_hand += change;
        }
        else{
            printf("Part not found\n");
        }
    }
    
    /********************************************************** 
     * print: Prints a listing of all parts in the database,  * 
     *        showing the part number, part name, and         * 
     *        quantity on hand. Parts are printed in the      * 
     *        order in which they were entered into the       * 
     *        database.                                       * 
     **********************************************************/
    void print(struct part inv[], int np){
    
        int i;
    
        printf("Part Number         Part Name                  "
                "Quantity on Hand\n");
        for(i = 0; i < np; i++){
            printf("%7d             %-25s%11d\n", inv[i].number, inv[i].name, inv[i].on_hand);
        }
    }
    The readline.h header file:
    Code:
    /*********************************************************
    *  From C PROGRAMMING: A MODERN APPROACH, Second Edition *
    *  By K. N. King                                         *
    *  Copyright (c) 2008, 1996 W. W. Norton & Company, Inc. *
    *  All rights reserved.                                  *
    *  This program may be freely distributed for class use, *
    *  provided that this copyright notice is retained.      *
    *********************************************************/
    /* readline.h (Chapter 16, page 395) */
    #ifndef READLINE_H
    #define READLINE_H
    /**********************************************************
    *  read_line: Skips leading white-space characters, then  *
    *  reads the remainder of the input line and   *
    *  stores it in str. Truncates the line if its *
    *  length exceeds n. Returns the number of     *
    *  characters stored.                          *
    **********************************************************/
    int read_line(char str[], int n);
    #endif
    The readline.c program:
    Code:
    /*********************************************************
    *  From C PROGRAMMING: A MODERN APPROACH, Second Edition *
    *  By K. N. King                                         *
    *  Copyright (c) 2008, 1996 W. W. Norton & Company, Inc. *
    *  All rights reserved.                                  *
    *  This program may be freely distributed for class use, *
    *  provided that this copyright notice is retained.      *
    *********************************************************/
    /* readline.c (Chapter 16, page 395) */
    
    #include <stdio.h>
    #include <ctype.h>
    #include "readline.h"
    
    int read_line(char str[], int n){
    
        int ch, i = 0;
    
        while(isspace(ch = getchar()));
        str[i++] = ch;
        while((ch = getchar()) != '\n'){
            if(i < n){
                str[i++] = ch;
                // ch = getchar();
            }
        }
        str[i] = '\0';
        return i;
    }
    Here is what my output looks like:
    Default Case Keeps Printing In Switch Statement-output2-png

    Any input on why my default case keeps printing would be greatly appreciated. Thanks!
    Last edited by snoopfrogg; 04-16-2019 at 12:14 PM.
    "One of the best programming skills you can have is knowing when to walk away for awhile."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C: Default case in switch not working
    By LunaLux in forum C Programming
    Replies: 5
    Last Post: 04-24-2011, 08:46 AM
  2. switch () {default: <something>; case ...
    By Kennedy in forum C Programming
    Replies: 4
    Last Post: 11-30-2010, 12:40 PM
  3. Switch statement / default:
    By kcpilot in forum C Programming
    Replies: 4
    Last Post: 12-02-2008, 03:14 PM
  4. Replies: 11
    Last Post: 08-25-2008, 12:01 PM
  5. default switch statement not working
    By gL_nEwB in forum C++ Programming
    Replies: 3
    Last Post: 05-13-2006, 10:13 AM

Tags for this Thread