Question

create case 4 #include <stdio.h> int main(void) { int counter; int choice; FILE *fp; char item[100];...

create case 4 

#include <stdio.h>
int main(void) {
        int counter;
        int choice;
        FILE *fp;
        char item[100];
        while(1) {
                printf("Welcome to my shopping list\n\n");
                printf("Main Menu:\n");
                printf("1. Add to list\n");
                printf("2. Print List\n");
                printf("3. Delete List\n");
                printf("4. Remove an item from the List\n");
                printf("5. Exit\n\n");


                scanf("%i", &choice);
                switch(choice) {
                        case 1: //add to list
                                //get the input from the user
                                printf("Enter item: ");
                                scanf("%s", item);
                                //open the file
                                fp = fopen("list.txt","a");
                                //write to the file
                                fprintf(fp, "\n%s", item);
                                //close the file
                                fclose(fp);
                        break;
                        case 2: //print list
                                //open the file
                                fp = fopen("list.txt","r");
                                //while we are not at the end of the file
                                counter = 1;

                                //cheat to make feof work! 
                                char c = fgetc(fp);
                                ungetc(c, fp);
                                while (!feof(fp)) {
                                        //read in a line of data
                                        fscanf(fp, "%s", item);
                                        //print the data
                                        printf("%i. %s\n", counter,item);
                                        counter++;
                                }
                                //close the file
                                fclose(fp);
                        break;
                        case 3: //erase list
                                //truncate the file
                                fp = fopen("list.txt","w");
                                fclose(fp);


                        break;
                        case 4: //homework: remove an item from the list;

                        /*
                                Method 1:
                                        0) Get the item number the user wants to delete
                                        char data[100][100];
                                        1) Create an array of strings
                                        2) Loop over the items in the list, loading into the array
                                                -skip if it's the item the user wants to delete
                                        3) Loop over the array, writing out to the file. 


                        */

                        break;
                        case 5: //end program
                                return 0; 
                        break;




                }

        }

Homework Answers

Answer #1

#include <stdio.h>
int main(void) {
        int counter;
        int choice;
        int item_no;
        FILE *fp;
        char c;
        char item[100];
        while(1) {
                printf("Welcome to my shopping list\n\n");
                printf("Main Menu:\n");
                printf("1. Add to list\n");
                printf("2. Print List\n");
                printf("3. Delete List\n");
                printf("4. Remove an item from the List\n");
                printf("5. Exit\n\n");


                scanf("%i", &choice);
                switch(choice) {
                        case 1: //add to list
                                //get the input from the user
                                printf("Enter item: ");
                                scanf("%s", item);
                                //open the file
                                fp = fopen("list.txt","a");
                                //write to the file
                                fprintf(fp, "\n%s", item);
                                //close the file
                                fclose(fp);
                        break;
                        case 2: //print list
                                //open the file
                                fp = fopen("list.txt","r");
                                //while we are not at the end of the file
                                counter = 1;

                                //cheat to make feof work!
                                c = fgetc(fp);
                                ungetc(c, fp);
                                while (!feof(fp)) {
                                        //read in a line of data
                                        fscanf(fp, "%s", item);
                                        //print the data
                                        printf("%i. %s\n", counter,item);
                                        counter++;
                                }
                                //close the file
                                fclose(fp);
                        break;
                        case 3: //erase list
                                //truncate the file
                                fp = fopen("list.txt","w");
                                fclose(fp);


                        break;
                        case 4: //homework: remove an item from the list;

                                fp = fopen("list.txt", "r");    //open the required file
                                printf("Enter the item no.");
                                scanf("%d", &item_no);          //take input for item number to be deleted

                                c = fgetc(fp);
                                ungetc(c, fp);
                                counter = 0;
                                char item_list[100];            //initialise an array of items

                                //read all items from the list
                                while (!feof(fp)) {
                                        //read in a line of data
                                        fscanf(fp, "%s", item);
                                        if(item_no != counter)      //condition to include all the items except the item to be deleted
                                            item_list[counter] = item;
                                        counter++;
                                }
                                //truncate the file
                                fp = fopen("list.txt","w");

                                fp = fopen("list.txt", "a");
                                //write to the file

                                int i = 0;
                                while(i < counter){
                                    fprintf(fp, "\n%s", item_list[i]);
                                    i++;
                                }
                                //close the file
                                fclose(fp);
                                /*
                                Method 1:
                                        0) Get the item number the user wants to delete
                                        char data[100][100];
                                        1) Create an array of strings
                                        2) Loop over the items in the list, loading into the array
                                                -skip if it's the item the user wants to delete
                                        3) Loop over the array, writing out to the file.
                            */

                        break;
                        case 5: //end program
                                return 0;
                        break;
                }

        }

}

Know the answer?
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
Similar Questions
why's is my csis.txt saving ? #include <stdio.h> FILE*fp; void calculateBMI(void); int main(void) {    fp...
why's is my csis.txt saving ? #include <stdio.h> FILE*fp; void calculateBMI(void); int main(void) {    fp = fopen("csis.txt", "w"); for (int i= 0; i<4; ++i){ calculateBMI(); } fclose(fp); return 0; } void calculateBMI(void){ double weight,height,bmi;    printf("Enetr your weight in pounds:\n\n"); fprintf(fp,"Enter your weight in pounds:\n\n"); scanf("%lf",&weight); printf("Enter height in inches:\n\n"); fprintf(fp,"Enter height in inches:\n\n"); scanf("%lf",&height); bmi=(703*weight)/(height*height);    if (bmi<18.5){ printf("You have a BMI of %.lf, and your weight status is underweight\n",bmi);    fprintf(fp,"You have a BMI of %.lf, and...
Design flow chart of this code and make sure it is not handwritten it must on...
Design flow chart of this code and make sure it is not handwritten it must on some software. struct user {    int id;    int age;    bool annualClaim;    int plan;    char name[30];    char contactNum[15];    char address[50]; }; #define MAX_LENGTH 500 struct user users[100]; int userCount = 0; struct claim {    int id;    int claimedYear;    int amountClaimed;    int remaininigAmount; }; struct claim claims[100]; void loadData() {    char line[MAX_LENGTH];    const...
Construct a flowchart based on this code and write its equivalent algorithms. #include <stdio.h> int main()...
Construct a flowchart based on this code and write its equivalent algorithms. #include <stdio.h> int main() { int x,y; float result; char ch; //to store operator choice printf("Enter first number: "); scanf("%d",&x); printf("Enter second number: "); scanf("%d",&y); printf("Choose operation to perform (+,-,*,/): "); scanf(" %c",&ch); result=0; switch(ch) { case '+': result=x+y; break; case '-': result=x-y; break; case '*': result=x*y; break; case '/': result=(float)x/(float)y; break; case '%': result=x%y; break; default: printf("Invalid operation.\n"); } printf("Result: %d %c %d = %.2f\n",x,ch,y,result); // Directly...
C CODE PLZ! Need all TO DO sections finished thanks #include <stdio.h> int main(int argc, char...
C CODE PLZ! Need all TO DO sections finished thanks #include <stdio.h> int main(int argc, char **argv) { const int BUF_LEN = 128; char str[BUF_LEN]; int i; char c; int is_binary; int d, n; /* Get the user to enter a string */ printf("Please enter a string made of 0s and 1s, finishing the entry by pressing Enter.\n"); for (i=0; i<BUF_LEN-1; i++) { scanf("%c", &c); if (c == '\n') { break; } str[i] = c; } str[i] = '\0'; /*...
Explain this code function by function in detail int menu() {   int choice;   do {     system("cls");...
Explain this code function by function in detail int menu() {   int choice;   do {     system("cls");     printf("1-Insurence Plan Subscription\n");     printf("2-Claim Processing\n");     printf("3-Accounts Information\n");     printf("4-Searching Functionalities\n");     printf("5-Exit\n");     scanf("%d", &choice);   } while (choice > 5 || choice < 1);   return choice; void subscribe() system("cls");   victims[userCount].id = userCount + 1;   printf("Enter age: ");   scanf("%d", &victims[userCount].age);   printf("\n\n%-25sHealth Insurence Plan\n\n", " ");   printf("-----------------------------------------------------------------------------------------------\n");   printf("|%-30s|%-20s|%-20s|%-20s|\n", " ", "Plan 120(RM)", "Plan 150(RM)", "Plan 200(RM)");   printf("-----------------------------------------------------------------------------------------------\n");   printf("|%-30s|%-20d|%-20d|%-20d|\n", "Monthly Premium", 120, 150, 200);   printf("|%-30s|%-20d|%-20d|%-20d|\n", "Annual Claim Limit", 120000,150000,200000);   printf("|%-30s|%-20d|%-20d|%-20d|\n",...
#include <unistd.h> #include <fcntl.h> #include <stdlib.h> #include <stdio.h> int main(int argc, char *argv[]){ int fd1, fd2;...
#include <unistd.h> #include <fcntl.h> #include <stdlib.h> #include <stdio.h> int main(int argc, char *argv[]){ int fd1, fd2; char buffer[100]; long int n1; if(((fd1 = open(argv[1], O_RDONLY)) == -1) || ((fd2 = open(argv[2], O_CREAT|O_WRONLY|O_TRUNC,0700)) == -1)){ perror("file problem "); exit(1); } while((n1=read(fd1, buffer, 512) > 0)) if(write(fd2, buffer, n1) != n1){ perror("writing problem "); exit(3); } // Case of an error exit from the loop if(n1 == -1){ perror("Reading problem "); exit(2); } close(fd2); exit(0); } There is an issue with this...
The code is in C programming language pls convert it into python. Thanks. Program --> #include...
The code is in C programming language pls convert it into python. Thanks. Program --> #include <stdio.h> #include <stdlib.h> void main() { //declare variables FILE *fileptr; char filename[15]; char charRead; char filedata[200],searchString[50]; int i=0,j=0,countNoOfWord=0,count=0; //enter the filename to be opened printf("Enter the filename to be opened \n"); scanf("%s", filename); /* open the file for reading */ fileptr = fopen(filename, "r"); //check file exit if (fileptr == NULL) { printf("Cannot open file \n"); exit(0); } charRead = fgetc(fileptr); //read the string...
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 extern char **environ;    5 void output(char *a[], char...
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 extern char **environ;    5 void output(char *a[], char *b[]) { 6 int c = atoi(a[0]); 7 for (int i = 0; i < c && b[i]; ++i) { 8 printf("%s", b[i]+2); 9 } 10 } 11 12 void main(int argc, char *argv[]) { 13      14 switch (argc) { 15 case 1: 16 for (int i = 0; environ[i]; ++i) {    17 printf("%s\n", environ[i]); 18 } 19 break; 20 default: 21 output(argv +...
Design flow chart on software not handwritten. void claimProcess() {    int id;    bool found...
Design flow chart on software not handwritten. void claimProcess() {    int id;    bool found = false;    system("cls");       printf("Enter user ID for which you want to claim insurrence: ");    scanf("%d", &id);    int i;    for (i = 0; i < userCount; i++)    {        if (users[i].id == id)        {            found = true;            break;        }    }    if (found == false)   ...
#include <stdio.h> int main( void ) {     double height, weight, bmi;     int bmievalcode;    ...
#include <stdio.h> int main( void ) {     double height, weight, bmi;     int bmievalcode;     while( 1 )     {         /* --> add code to input weight and height between here */         scanf("%lf %lf\n" , &weight &height)         bmi = weight * 703 / (height * height);         /* <-- and here */         /* --> add code to calculate bmi and bmievalcode between here */         if (bmi <= 00.0){             bmievalcode = -1;         else...