Question

create case 4 do Method 1:copy item by item and skip the item you want to...

create case 4 do Method 1:copy item by item and skip the item you want to delete after you are done, delete the old file and rename the new one to the old name.

#include 
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;




                }

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

If you have any doubts, please give me comment...

#include <stdio.h>

#include <string.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.

            */

            {

                char data[100][100];

                printf("Enter item to delete: ");

                scanf("%s", item);

                fp = fopen("list.txt", "r");

                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", data[counter]);

                    if (strcmp(data[counter], item) != 0)

                        counter++;

                }

                //close the file

                fclose(fp);

                fp = fopen("list.txt", "w");

                int i;

                for (i = 0; i < counter; i++)

                {

                    //write to the file

                    fprintf(fp, "\n%s", data[i]);

                }

                //close the file

                fclose(fp);

            }

            break;

        case 5: //end program

            return 0;

            break;

        }

    }

}

Add a comment
Know the answer?
Add Answer to:
create case 4 do Method 1:copy item by item and skip the item you want to...
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
  • Identify and list all the User defined Functions to be used in the system #include <stdio.h>...

    Identify and list all the User defined Functions to be used in the system #include <stdio.h> ///for input output functions like printf, scanf #include <stdlib.h> #include <conio.h> #include <windows.h> ///for windows related functions (not important) #include <string.h> ///string operations /** List of Global Variable */ COORD coord = {0,0}; /// top-left corner of window /** function : gotoxy @param input: x and y coordinates @param output: moves the cursor in specified position of console */ void gotoxy(int x,int y) {...

  • Create another program that will prompt a user for input file. The user will choose from...

    Create another program that will prompt a user for input file. The user will choose from 4 choices: 1. Display all positive balance accounts. 2. Display all negative balance accounts. 3. Display all zero balance accounts. 4. End program. C PROGRAMMING my code so far #include<stdlib.h> #include<stdio.h> int main(void) { int request; int account; FILE *rptr; char name[20]; double balance; FILE *wptr;    int accountNumber;    if((wptr = fopen("clients.dat","w")) == NULL) { puts("File could not be opened"); } else {...

  • Hardware Inventory – Write a database to keep track of tools, their cost and number. Your...

    Hardware Inventory – Write a database to keep track of tools, their cost and number. Your program should initialize hardware.dat to 100 empty records, let the user input a record number, tool name, cost and number of that tool. Your program should let you delete and edit records in the database. The next run of the program must start with the data from the last session. This is my program so far, when I run the program I keep getting...

  • Convert C to C++ I need these 4 C file code convert to C++. Please Convert...

    Convert C to C++ I need these 4 C file code convert to C++. Please Convert it to C++ //////first C file: Wunzip.c #include int main(int argc, char* argv[]) { if(argc ==1){ printf("wunzip: file1 [file2 ...]\n"); return 1; } else{ for(int i =1; i< argc;i++){ int num=-1; int numout=-1; int c; int c1;    FILE* file = fopen(argv[i],"rb"); if(file == NULL){ printf("Cannot Open File\n"); return 1; } else{ while(numout != 0){    numout = fread(&num, sizeof(int), 1, file);    c...

  • Programming in C: I am trying to modify this linked list to be doubly linked list....

    Programming in C: I am trying to modify this linked list to be doubly linked list. I’m also trying to add a print in reverse function. I’m really struggling with how to change the insert function to doubly link the nodes without effecting the alphabetical sorting mechanism. Example of desired output: Enter your choice: 1 to insert an element into the list. 2 to delete an element from the list. 3 to end. ? 1 Enter a character: a The...

  • /* I want to fix void make_flight(int counter, flight_t flights[]) Enter flight code> Enter departure info...

    /* I want to fix void make_flight(int counter, flight_t flights[]) Enter flight code> Enter departure info for the flight leaving SYD. Enter month, date, hour and minute separated by spaces> Enter arrival city code> Enter arrival info. Enter month, date, hour and minute separated by spaces> It should do all of this: 1-Flight - left aligned, MAX_FLIGHTCODE_LEN (i.e. 6) chars at most. 2-City - left aligned, MAX_CITYCODE_LEN 3 chars at most . For example( VA1 or LAX ) 3- Month,...

  • Fix the errors in C code #include <stdio.h> #include <stdlib.h> void insertAt(int *, int); void Delete(int...

    Fix the errors in C code #include <stdio.h> #include <stdlib.h> void insertAt(int *, int); void Delete(int *); void replaceAt(int *, int, int); int isEmpty(int *, int); int isFull(int *, int); void removeAt(int *, int); void printList(int *, int); int main() { int *a; int arraySize=0,l=0,loc=0; int choice; while(1) { printf("\n Main Menu"); printf("\n 1.Create list\n 2.Insert element at particular position\n 3.Delete list.\n4. Remove an element at given position \n 5.Replace an element at given position\n 6. Check the size of...

  • Need this in C The starter code is long, if you know how to do it...

    Need this in C The starter code is long, if you know how to do it in other way please do. Do the best you can please. Here's the starter code: // ----------------------------------------------------------------------- // monsterdb.c // ----------------------------------------------------------------------- #include #include #include // ----------------------------------------------------------------------- // Some defines #define NAME_MAX 64 #define BUFFER_MAX 256 // ----------------------------------------------------------------------- // Structs typedef struct { char name[NAME_MAX]; int hp; int attackPower; int armor; } Character; typedef struct { int size; Character *list; } CharacterContainer; // ----------------------------------------------------------------------- //...

  • Finish the code below (using bubble sort) to sort the datafile in ascending order. each number...

    Finish the code below (using bubble sort) to sort the datafile in ascending order. each number should stop after "t" #include #include #include int main() { char letter[1400]; int length; FILE *fptr; if ((fptr = fopen("datafile1.txt","r")) == NULL) { printf("Error! opening file"); return (1); } if (fgets(letter,5000,fptr) != NULL) { printf("The string is in the file is\n\n"); puts(letter); } fclose(fptr); printf("\n\n"); length = strlen(letter); sortascending(length,letter); puts(letter); return 0; } ***datafile1.txt*** 1804289383t846930886t681692777t1714636915t1957747793tn424238335t719885386t1649760492t596516649t1189641421tn1025202362t1350490027t783368690t1102520059t2044897763tn1967513926t1365180540t1540383426t304089172t1303455736tn35005211t521595368t294702567t1726956429t336465782tn861021530t278722862t233665123t2145174067t468703135tn1101513929t1801979802t1315634022t635723058t1369133069tn1125898167t1059961393t2089018456t628175011t1656478042tn1131176229t1653377373t859484421t1914544919t608413784tn756898537t1734575198t1973594324t149798315t2038664370tn1129566413t184803526t412776091t1424268980t1911759956tn749241873t137806862t42999170t982906996t135497281tn511702305t2084420925t1937477084t1827336327t572660336tn1159126505t805750846t1632621729t1100661313t1433925857tn1141616124t84353895t939819582t2001100545t1998898814tn1548233367t610515434t1585990364t1374344043t760313750tn1477171087t356426808t945117276t1889947178t1780695788tn709393584t491705403t1918502651t752392754t1474612399tn2053999932t1264095060t1411549676t1843993368t943947739tn1984210012t855636226t1749698586t1469348094t1956297539tn update** I'm supposed to write a code that scans...

  • Convert this code from C to C++ include <stdio.h> include <locale.h> void filtre (double x. double yll, double yol, int , int N) int Nn-10000 int main) setlocale (LC ALL,") double...

    Convert this code from C to C++ include <stdio.h> include <locale.h> void filtre (double x. double yll, double yol, int , int N) int Nn-10000 int main) setlocale (LC ALL,") double x(Nm) , y [ Nm],yo Nml®(0.0); int m; FILE dosyal-fopen("D:/veri/673.txtr FILE dosya2-fopen("D:/veri/data2.txt int i-0 while( feof (dosyal)) fscanf (dosyal, " ",xi sytil) fclose (dosyal) int Nij printf("Pencere Genişligi scanf() 3 filtre(x,y.Yo,m,N) for(int k-0keNkt+)fprintf (dosya2, 10.41 10.411 0.41fn",x[k],y[k].yo[k]) fclose(dosya2) void filtre (double x double y. double yoll, int m, int...

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