I am having trouble with question 6 on this program. It\'s not processing what I
ID: 662917 • Letter: I
Question
I am having trouble with question 6 on this program. It's not processing what I need it to do. Also, if you can check the rest of my code, that would be greatly appreciated!
Here is the code along with the other questions I am supposed to code:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#pragma warning(disable : 4996)
// [Q1 - 5pts] Update the struct Order to include the following fields :
// (Please structure the object to use the least amount of memory.
// HINT: think back to padding structure.)
// char name[61];
// int customerNo;
// char item[103];
// char address[128];
// float cost;
struct Order
{
int orderid;
char name[61];
char item[103];
char address[128];
int customerNo;
float cost;
struct Order* next;
} *Queue = NULL;
void flush(); // forward declaration of functions
void branching(char c);
int insert_order(struct Order* item);
int update_order(struct Order* item);
int cancel_order(int orderid);
int process_order(int numOfItems);
int sizeofOrderStructure();
int insert_update_order_helper(char c);
int process_order_helper();
int cancel_order_helper();
int main() {
char ch = 'i';
ungetc(' ', stdin); // inject input buffer with a return character
printf("The size of the order structure is %d bytes. ", sizeofOrderStructure());
do {
printf("Enter your selection ");
printf(" i: insert a new order. ");
printf(" u: update an order. ");
printf(" d: cancel an order. ");
printf(" p: process an order. ");
printf(" q: quit ");
flush(); // flush input buffer
ch = tolower(getchar()); // see tutorial
branching(ch);
} while (ch != 113);
return 0;
}
void flush() {
int c;
do {
c = getchar();
} while (c != ' ' && c != EOF);
}
int sizeofOrderStructure() {
return sizeof(struct Order);
}
void branching(char c) { // branch to different tasks
switch (c) {
case 'i':
insert_update_order_helper(c);
break;
case 'u':
insert_update_order_helper(c);
break;
case 'd':
cancel_order_helper();
break;
case 'p':
process_order_helper();
break;
case 'q':
break;
default:
printf("Invalid input ");
}
}
// [Q2 - 5pts] Write code to capture customerNo, cost, name, address, and item
// into the new created struct Order pointed by ptr. Assume name, address, and item
// cannot accept any spaces or return characters.
int insert_update_order_helper(char c) {
struct Order *ptr = (struct Order *)malloc(sizeof(struct Order));
printf("What is the order id? ");
scanf("%s", &ptr->orderid);
/* -- START CODING HERE -- */
ptr->customerNo;
ptr->cost;
ptr->name;
ptr->address;
ptr->item;
/* -- END CODING HERE -- */
ptr->next = NULL;
if (c == 'i') {
return insert_order(ptr);
}
else {
int value = update_order(ptr);
if (value == -1) { printf("No Record Found."); }
return update_order(ptr);
}
}
// [Q3 - 10pts] Implement the insert function. The function should check
// if existing order has been made (using the order id). If an order
// exist, update the record. Otherwise, insert a new order into the queue
//
Explanation / Answer
Complete Program:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#pragma warning(disable : 4996)
// [Q1 - 5pts] Update the struct Order to include the following fields :
// (Please structure the object to use the least amount of memory.
// HINT: think back to padding structure.)
// char name[61];
// int customerNo;
// char item[103];
// char address[128];
// float cost;
struct Order
{
int orderid;
char name[61];
char item[103];
char address[128];
int customerNo;
float cost;
struct Order* next;
} *Queue = NULL;
void flush(); // forward declaration of functions
void branching(char c);
int insert_update_order_helper(char c);
int process_order_helper();
int cancel_order_helper();
int insert_order(struct Order* item);
int update_order(struct Order* item);
int cancel_order(int orderid);
int process_order(int numOfItems);
int sizeofOrderStructure();
int main()
{
char ch;// = 'i';
ungetc(' ', stdin); // inject input buffer with a return character
printf("The size of the order structure is %d bytes. ", sizeofOrderStructure());
do
{
printf(" Enter your selection ");
printf(" i: insert a new order. ");
printf(" u: update an order. ");
printf(" d: cancel an order. ");
printf(" p: process an order. ");
printf(" q: quit ");
flush(); // flush input buffer
ch = tolower(getchar()); // see tutorial
branching(ch);
}while(ch != 't');
return 0;
}
void flush()
{
int c;
do
{
c = getchar();
}while(c != ' ' && c != EOF);
}
int sizeofOrderStructure()
{
return sizeof(struct Order);
}
void branching(char c)
{ // branch to different tasks
switch(c)
{
case 'i':
insert_update_order_helper(c);
break;
case 'u':
insert_update_order_helper(c);
break;
case 'd':
cancel_order_helper();
break;
case 'p':
process_order_helper();
break;
case 'q':
break;
default:
printf("Invalid input ");
}
}
// [Q2 - 5pts] Write code to capture customerNo, cost, name, address, and item
// into the new created struct Order pointed by ptr. Assume name, address, and item
// cannot accept any spaces or return characters.
int insert_update_order_helper(char c)
{
struct Order *ptr = (struct Order *)malloc(sizeof(struct Order));
printf("What is the order id? ");
scanf("%d", &ptr->orderid);
ptr->next = NULL;
if(c == 'i')
{
return insert_order(ptr);
}
else
{
int value = update_order(ptr);
if(value == -1)
{
printf("No Record Found. ");
}
return value;
}
}
// [Q3 - 10pts] Implement the insert function. The function should check
// if existing order has been made (using the order id). If an order
// exist, update the record. Otherwise, insert a new order into the queue
// – follow FIFO.
int insert_order(struct Order* item)
{
if(item == NULL)
{
return -1;
}
printf("What is the name? ");
scanf("%s", item->name);
printf("What is the item? ");
scanf("%s", item->item);
printf("What is the address? ");
scanf("%s", item->address);
printf("What is the customerNo? ");
scanf("%d", &item->customerNo);
printf("What is the cost? ");
scanf("%f", &item->cost);
if(Queue == NULL)
{
Queue = item;
}
else
{
struct Order *temp;
temp = Queue;
while(temp->next != NULL)
{
if(temp->orderid == item->orderid)
return update_order(item);
temp = temp->next;
}
temp->next = item;
}
return 0;
}
// [Q4 - 10pts] Implement the update function. The function will search
// the queue using order id to locate the existing order. If order does
// not exist, return -1. Otherwise, update the existing record.
int update_order(struct Order* item)
{
if(item == NULL)
{
return -1;
}
struct Order *temp;
temp = Queue;
while(temp != NULL && temp->orderid != item->orderid)
{
temp = temp->next;
}
if(temp == NULL)
{
// if the orderid is not found
return -1;
}
else
{
// if the orderid is found
printf("What is the name? ");
scanf("%s", item->name);
printf("What is the item? ");
scanf("%s", item->item);
printf("What is the address? ");
scanf("%s", item->address);
printf("What is the customerNo? ");
scanf("%d", &item->customerNo);
printf("What is the cost? ");
scanf("%f", &item->cost);
temp->orderid = item->orderid;
strcpy(temp->name, item->name);
strcpy(temp->item, item->item);
strcpy(temp->address, item->address);
temp->customerNo = item->customerNo;
temp->cost = item->cost;
return 0;
}
}
int cancel_order_helper()
{
int x;
printf("Please enter the order id: ");
scanf("%d", &x);
return cancel_order(x);
}
// [Q5 - 10pts] Implement the cancel function. The function will search
// through the queue and locate the order. If order exist, remove it from
// the queue. Otherwise, please display - “Record cannot be found” - on
// the console.
int cancel_order(int orderid)
{
if(Queue == NULL)
{
printf("Record cannot be found! ");
return -1;
}
else
{
struct Order *temp, *prev;
temp = Queue;
prev = NULL;
while(temp != NULL && temp->orderid != orderid)
{
prev = temp;
temp = temp->next;
}
if(temp == NULL)
{
printf("Record cannot be found! ");
return -1;
}
else if(prev == NULL)
{
Queue = Queue->next;
}
else
{
prev->next = temp->next;
}
free(temp);
return 0;
}
}
int process_order_helper()
{
int x;
printf("How many items will the external system process? ");
scanf("%d", &x);
return process_order(x);
}
// [Q6 - 10pts] Implement the process function. The function will process
// n-number of orders from the queue starting at the beginning. Each item
// processed will be displayed and then removed from the linked list.
// If the queue has less items then requested; process all the items in the
// linked list and then display - "Queue is empty". You must also consider
// if the linked list is empty. If so, please display – “Queue is empty” and return -1.
int process_order(int numberOfItems)
{
if(Queue == NULL)
{
printf("Queue is empty! ");
return -1;
}
struct Order *temp;
int count = 0;
while(count < numberOfItems && Queue != NULL)
{
printf("%d, %s, %s, %s, %d, %.2f ", Queue->orderid, Queue->name, Queue->item, Queue->address, Queue->customerNo, Queue->cost);
temp = Queue;
Queue = Queue->next;
free(temp);
count++;
}
if(count != numberOfItems)
{
printf("Queue is empty! ");
return -1;
}
printf(" ");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.