Question

I need this code in C programming. Can anyone pls help me in this?

Project 8, Program Design A hotel owner would like to maintain a list of laundry service requests by the guests. Each request

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please comment below if you have any doubts...
Please don't forget to upvote if the answer helped you thanks...


int main(void) 29 30 31 32 Enter operation code: a Enter room number: 1 A request for this room already exists. Please use up

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define NAME_LEN 30

struct request{
   int room_number;
   char first[NAME_LEN+1];
   char last[NAME_LEN+1];
   int num_items;
   struct request *next;
};


struct request *append_to_list(struct request *list);
void update(struct request *list);
void printList(struct request *list);
void clearList(struct request *list);
int read_line(char str[], int n);

/**********************************************************
* 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 request *new_list = NULL;
        printf("Operation Code: a for appending to the list, u for updating a request"
           ", p for printing the list; q for quit.\n");
        for (;;) {
                printf("Enter operation code: ");
                scanf(" %c", &code);
                while (getchar() != '\n') /* skips to end of line */
                ;
                switch (code) {
                        case 'a': new_list = append_to_list(new_list);
                        break;
                        case 'u': update(new_list);
                        break;
                        case 'p': printList(new_list);
                        break;
                        case 'q': clearList(new_list);
                                   return 0;
                        default: printf("Illegal code\n");
                }
                printf("\n");
        }
}

struct request *append_to_list(struct request *list){
        int room_number;

        printf("Enter room number: ");
        scanf("%d", &room_number);
        while (getchar() != '\n');
   
        struct request *ptr = list;
        if(list != NULL) {
           do {
                   if(ptr->room_number == room_number) {
                           printf("A request for this room already exists.\n");
                           printf("Please use update function to update the request.\n");
                           return list;
                   }
                   if(ptr->next != NULL) {
                                ptr = ptr->next;
                   }
           } while(ptr->next != NULL);
        }       
        
        struct request *newReq = malloc(sizeof(struct request));
        newReq->room_number = room_number;
        printf("Enter first name: ");
        read_line(newReq->first, NAME_LEN);
        printf("Enter last name: ");
        read_line(newReq->last, NAME_LEN);
        printf("Enter number of items: ");
        scanf("%d", &newReq->num_items);
        while (getchar() != '\n');
        
        
        if(list == NULL) {
                list = newReq;
        } else {                
                ptr->next = newReq;
        }
        
        return list;
}

void update(struct request *list)
{
        int room_number;
        printf("Enter room number: ");
        scanf("%d", &room_number);
   
        struct request *ptr = list;
        while(ptr != NULL) {
                if(ptr->room_number == room_number) {
                        int n;
                        
                        printf("Enter number of items to be added: ");
                        scanf("%d", &n);
                        while (getchar() != '\n');
                        ptr->num_items += n;
                        printf("updaeted\n");
                        return;
                }
                ptr = ptr->next;
        };
        
        printf("No room exists with this room number\n");
}


void printList(struct request *list){   
        struct request *ptr = list;
        while(ptr != NULL) {
                
                printf("Room: %d\n", ptr->room_number);
                printf("%s %s\n", ptr->first, ptr->last);
                printf("numItems: %d\n", ptr->num_items);
                printf("\n");
                
                ptr = ptr->next;
        };
}

void clearList(struct request *list)
{
        if(list == NULL) {
                return;
        }
        
        clearList(list->next);
        free(list);
}

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;
          
        }
        str[i] = '\0';
        return i;
}
Add a comment
Know the answer?
Add Answer to:
I need this code in C programming. Can anyone pls help me in this? Project 8,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please use C programming to write the code to solve the following problem. Also, please use the i...

    Please use C programming to write the code to solve the following problem. Also, please use the instructions, functions, syntax and any other required part of the problem. Thanks in advance. Use these functions below especially: void inputStringFromUser(char *prompt, char *s, int arraySize); void songNameDuplicate(char *songName); void songNameFound(char *songName); void songNameNotFound(char *songName); void songNameDeleted(char *songName); void artistFound(char *artist); void artistNotFound(char *artist); void printMusicLibraryEmpty(void); void printMusicLibraryTitle(void); const int MAX_LENGTH = 1024; You will write a program that maintains information about your...

  • CAN SOMEONE PLEASE HELP ME WRITE THIS CODE IN C++, PLEASE HAVE COMMENTS IN THE CODE...

    CAN SOMEONE PLEASE HELP ME WRITE THIS CODE IN C++, PLEASE HAVE COMMENTS IN THE CODE IF POSSIBLE!! General Description: Write a program that allows the user to enter data on their friends list. The list holds a maximum of 10 friends, but may have fewer. Each friend has a name, phone number, and email address. The program first asks the user to enter the number of friends (1-10). You are not required to validate the entry, but you may...

  • currentPtr. Use your code to make a boolean function findAndDelete that is given a pointer to...

    currentPtr. Use your code to make a boolean function findAndDelete that is given a pointer to a linked list and a value, and if the value is in the linked list, it is deleted and the function returns true. If it is not in the list, the function returns false. 8. Expand your menu to test your three new functions. When choosing to test the find or findAndDelete functions, the user will be first asked to enter the value to...

  • Project Description: In this project, you will combine the work you’ve done in previous assignments to...

    Project Description: In this project, you will combine the work you’ve done in previous assignments to create a separate chaining hash table. Overview of Separate Chaining Hash Tables The purpose of a hash table is to store and retrieve an unordered set of items. A separate chaining hash table is an array of linked lists. The hash function for this project is the modulus operator item%tablesize. This is similar to the simple array hash table from lab 5. However, the...

  • In this project you will create a console C++ program that will have the user to...

    In this project you will create a console C++ program that will have the user to enter Celsius temperature readings for anywhere between 1 and 365 days and store them in a dynamically allocated array, then display a report showing both the Celsius and Fahrenheit temperatures for each day entered. This program will require the use of pointers and dynamic memory allocation. Getting and Storing User Input: For this you will ask the user how many days’ worth of temperature...

  • C++ Programming

    PROGRAM DESCRIPTIONIn this project, you have to write a C++ program to keep track of grades of students using structures and files.You are provided a data file named student.dat. Open the file to view it. Keep a backup of this file all the time since you will be editing this file in the program and may lose the content.The file has multiple rows—each row represents a student. The data items are in order: last name, first name including any middle...

  • In this assignment, you will implement a Memory Management System(MMS). Using C Programming Language..... MAKE SURE...

    In this assignment, you will implement a Memory Management System(MMS). Using C Programming Language..... MAKE SURE YOU USE C PROGRAMMING Your MMS will handle all requests of allocation of memory space by different users (one thread per user) …. HINT(You will use Pthreads and Semaphores). Your MMS will provide the user with an interface for making memory requests and also for freeing up memory that is no longer needed by the user. One of the jobs of your memory management...

  • I need help with my C coding homework. If possible can you please comment to help...

    I need help with my C coding homework. If possible can you please comment to help me understand whats going on in the code. Thanks in advance. Create a C program (3 points): o At the top of the file include the following comments: Your name The purpose (core concept found below) of the program The date created o Create two int variables: Initialize each to different values. o Create three pointers: Point the first two pointers to the first...

  • In C++ syntax please Write a program that implements and demonstrates a linked list using functions....

    In C++ syntax please Write a program that implements and demonstrates a linked list using functions. Your program should first dehne a node that stores an integer and then your program will include the following functions appendo- This function accepts the head pointer (by reference) of a linked list and an integer as it's only arguments. It then creates a node, stores the integer argument in the node, and adds it to the end of the list. findo-This function accepts...

  • Create the program in C++ please. The new operator as outlined in 10.9 . Create a...

    Create the program in C++ please. The new operator as outlined in 10.9 . Create a Test class that includes the following Data members • First Name, last name, test1, test2, test3 o Methods • Accessor methods for each of the data members' • Mutator methods for each of the data members . A Default Constructor . A constructor that will accept arguments for each of the 5 data members • A method that will calculate the average of the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT