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; } }
#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;
}
}
}
Get Answers For Free
Most questions answered within 1 hours.