online compiler and debugger for c/c++

code. compile. run. debug. share.
Source Code   
Language
/********************/ #include <stdio.h> #include <stdlib.h> //#include <ctype.h> #include "readline.h" #define NAME_LEN 25 struct part { int number; char name[NAME_LEN+1]; int on_hand; struct part *next; }; struct part *inventory = NULL; /* points to first part */ struct part *find_part(int number); void insert(void); void search(void); void update(void); void print(void); /* main prompts the user to enter an operations code then calls a function to perform the requested action until user enters command 'q'*/ int main(void) { char code; for (;;){ printf("Enter operation code : "); scanf(" %c", &code); while (getchar() !='\n') /*skips to end of line */ ; switch (code) { case 'i' : insert(); break; case 's' : search(); break; case 'u' : update(); break; case 'p' : print(); break; case 'q' : return 0; default: printf("Illegal code \n"); } printf("\n"); } } /* find_part : looks up a part number in the inventory list and rerturns a pointer to the node containing the part number, if the part number is not found it returns NULL */ struct part *find_part(int number) { struct part *p; for (p=inventory; p!=NULL && number > p->number; p=p->next) ; if (p!=NULL && number == p->number) return p; return NULL; } /* insert : Prompts the users for information about a new part and then inserts the part info into the inventory list; the list remains sorted by part number. prints an error message and return prematurely if the part already exits or space could not be allocated for the part */ void insert(void) { struct part *cur, *prev, *new_node; new_node = malloc(sizeof(struct part)); if (new_node == NULL){ printf("Database is full; cannot add more parts. \n"); return; } printf("Enter part number : "); scanf("%d", &new_node->number); for (cur = inventory, prev = NULL; cur != NULL && new_node->number > cur->number; prev = cur, cur = cur->next) ; if(cur !=NULL && new_node->number == cur->number){ printf(" Part already exists. \n"); free(new_node); return; } printf("Enter part name: "); read_line(new_node->name, NAME_LEN); printf("Enter quantity on hand: "); scanf("%d", &new_node->on_hand); new_node->next=cur; if(prev==NULL) inventory=new_node; else prev->next=new_node; } /* search : prompts user to enter a part number and looks up the part in the database. If the part exits, prints the name and quantity on hand, if not prints an error message */ void search(void) { int number; struct part *p; printf( " Enter part number to search : "); scanf("%d", &number); p=find_part(number); if(p!=NULL){ printf("Part name: %s\n", p->name); printf("Quantity on hand : %d\n", p->on_hand); } else printf("Part not found. \n"); } /* update : prompts the user to enter a part number. prints an error message if the part does not exist, otherwise prompts the user to enter change in quantity on hand and updates the database*/ void update(void) { int number, change; struct part *p; printf("Enter part number: "); scanf("%d", &number); p = find_part(number); if(p != NULL){ printf("Enter change in quantity on hand: "); scanf("%d", &change); p->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. part numbers will appear in ascending order. */ void print(void) { struct part *p; printf("Part Number Part Name Quantity on Hand\n"); for (p = inventory; p !=NULL; p = p->next) printf("%7d %-25s%11d\n", p->number, p->on_hand); }
#ifndef READLINE_H #define READLINE_H /* read_line: skips leading white space chars then reads the remainder of the input line and stores it in str. Truncates the line if its length exceeds n. Returns a number of char stored. */ int read_line (char str[], int n); #endif
#include <ctype.h> #include <stdio.h> #include "readline.h" int read_line(char str[], int n) { int ch, i=0; while (isspace(ch=getchar())) ; while (ch != '\n' && ch !=EOF){ if(i<n) str[i++]=ch; ch=getchar(); } str[i] = '\0'; return i; }

Compiling Program...

Command line arguments:
Standard Input: Interactive Console Text
×

                

                

Program is not being debugged. Click "Debug" button to start program in debug mode.

#FunctionFile:Line
VariableValue
RegisterValue
ExpressionValue