Ok I have amended my code:
Code:
#include <stdio.h>
#include <string.h>
struct StockCtrl
{
int CatNum; // Declare Catalogue Number
char title[20]; // Declare Book Title
int Stock; // Declare Stock Number
};
int main(void)
{
StockCtrl List[50];
int menu_choice; //User choice in menu
int i=0; //For loop variable
int x=0; //While loop variable : Determines whether the user wants to do more after last action
int stock_list=5; //Loop value for amount of records
while(0==x) //While x=0 continue to allow the user to run the program
{
printf("\nStock Control Program\n"); //Title of program
printf("What would you like to do?\n\n");
printf("1. Display Stock\n"); //Press 1 to display stock
printf("2. Add Item to Stock\n"); //Press 2 to add item to stock
printf("3. Delete Item from Stock\n"); //Press 3 to delete item from stock
printf("4. Edit Stock\n"); //Press 4 to edit items in stock
printf("5. Write to File\n"); //Press 5 to write items to stock
printf("6. Exit\n\n"); //Press 6 to exit program
printf("Please enter your choice by number: "); //Prompt for user's choice
scanf("%d",&menu_choice); //Scans users choice
switch(menu_choice) //Switch statement to correspond to users choice
{
case 1: //User choice is 1
int do_more1;
FILE *fp; //Declare pointer
printf("\nYou chose 1. Display Stock\n\n"); //Verify what the user chose
fp=fopen("Stock.txt","r"); //Opens .txt file
for(i=0;i<stock_list;i++) //For loop to scan all data in the .txt file
{
fscanf(fp,"%d",&List[i].CatNum); //Scan for Catalogue Number
fscanf(fp,"%s",&List[i].title); //Scan for Title
fscanf(fp,"%d",&List[i].Stock); //Scan for Stock
}
fclose(fp); //Closes the .txt file
for(i=0;i<stock_list;i++) //Loop to print all the scanned data
{
printf("%d %s %d\n",List[i].CatNum,List[i].title,List[i].Stock); //Prints all scanned data
}
printf("\nWould you like to do anything else?\n"); //Prompt the user whether they want to do more with the program.
printf("1. Yes\n2. No\n");
scanf("%d",&do_more1); //Scans decision
if(1==do_more1)
{
0==x; //Sets X=0 meaning the program can continue
}
else
{
1==x; //Sets X=1 meaning the program will close
printf("You chose 2. No\n");
printf("Goodbye.\n\n");
return 1;
}
break;
case 2: //User choice is 2
int new_catnum; //To store new items catalogue number in memory
char new_title[20]; //To store new items title in memory
int new_stock; //To store new items stock amount in memory
int do_more2;
printf("\nYou chose 2. Add Item to Stock\n"); //Verify users choice
printf("\nPlease enter your new item's Catalogue Number\n"); //Prompt for catalogue number
scanf("%d",&new_catnum); //Scans catalogue number
printf("\nPlease enter your new item's Title\n"); //Prompt title
scanf("%s",&new_title); //Scans title
printf("\nPlease enter your new item's Stock level\n"); //Prompt for stock level
scanf("%d",&new_stock); //Scans stock level
stock_list=stock_list+1; //Sets the read loop to +1 everytime a new item of stock is input so it shows up on Display Stock.
printf("Would you like to do anything else?\n"); //Prompt the user whether they want to do more with the program
printf("1. Yes\n2. No\n\n"); //Choice
scanf("%d",&do_more2); //Scans choice
if(1==do_more2)
{
0==x; //Sets X=0 meaning the program can continue
}
else
{
1==x; //Sets X=1 meaning the program will close
printf("You chose 2. No\n"); //Verify choice
printf("Goodbye.\n\n");
return 1;
}
break;