0
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int record_purchase();
int record_sale();
void view_all();
int main_menu();


struct Brand{ /*Created to store shoe data */
int num;
char brand[10];
int count;
};

int main()
{
printf("****************************Welcome to Paul's Shoe   Zone!!*********************** \n \n \n");
printf("*******************Where serving you is our pleasure :)************************ \n \n \n \n \n");
getchar(printf("Hit enter to continue..."));
main_menu();

}

int main_menu() /* Calls on a specific function, depending on the option  choosen by user*/
{
int option = 0;

do
{
    printf("\n What would you like to do today? \n 1. Update Inventory \n 2. Record a Sale \n 3. Display Inventory \n --->:");
    scanf("%d", &option);
    getchar();

    switch (option)
    {
    case 1:
        if (record_purchase() == 1){
            printf("\n\nYour new shoes have been added to inventory.\n\n");
            getchar(printf("Hit enter to continue..."));
        }
        break;
    case 2:         if (record_sale() == 1){
            printf("\n\nYour sale has been recorded.\n\n");
            getchar(printf("Hit enter to continue..."));
        }
        break;
    case 3:
        view_all();
        break;
    default:
        option = 0;
    }
   } while (option != 0);

  return 0;
  }

/*Record new purchases. The brand and quantity are
*provided at the keyboard and written to the shoe file.
*If successful, return 1, otherwise return 0.
*/
int record_purchase()
{
FILE *cfPtr;
int proceed = 1;
struct Brand newShoe;
struct Brand oldShoe;

//open file for writing
if ((cfPtr = fopen("stock.txt", "rb+")) == NULL)
{
    printf("Cannot open file");
    return 0;
}
else
{  //get brand and amount
    printf("What is the brand of the shoe? \n \n --->:");
    scanf("%s", &(newShoe.brand));
    getchar();

    printf(" How many are you adding? \n \n --->:");
    scanf("%d", &(newShoe.count));
    getchar();

    while ((!feof(cfPtr)) && proceed == 1)
    {  //get record from file
        fread(&oldShoe, sizeof(struct Brand), 1, cfPtr);

        if (strcmp(oldShoe.brand, newShoe.brand) == 0)
        { /*Updates data then writes new value to file*/
            newShoe.num = oldShoe.num;
            newShoe.count += oldShoe.count;
            fseek(cfPtr, (oldShoe.num)*sizeof(struct Brand), SEEK_SET);

            fwrite(&newShoe, sizeof(struct Brand), 1, cfPtr);

            proceed = 0;
        }
    }

    fclose(cfPtr);
    return 1;
  }
}
/*Records new sales.The brand and quantity are
*provided at the keyboard and written to the shoe file.
*If successful, return 1, otherwise return 0.
*/
int record_sale()
{
FILE *cfPtr;
int proceed = 1;
struct Brand newShoe;
struct Brand oldShoe;

//open file for writing
if ((cfPtr = fopen("stock.txt", "rb+")) == NULL)
{
    printf("Cannot open file");
    return 0;
}
else
{  //get brand and amount
    printf("What is the brand of the shoe? \n \n --->:");
    scanf("%s", &(newShoe.brand));
    getchar();

    printf(" How many are you selling? \n \n --->:");
    scanf("%d", &(newShoe.count));
    getchar();

    while ((!feof(cfPtr)) && proceed == 1)
    {  /*get record from file*/
        fread(&oldShoe, sizeof(struct Brand), 1, cfPtr);

        if (strcmp(oldShoe.brand, newShoe.brand) == 0)
        {   /*Updates data in file then writes new values to file*/
            newShoe.num = oldShoe.num;
            newShoe.count -= oldShoe.count;
            fseek(cfPtr, (oldShoe.num)*sizeof(struct Brand), SEEK_SET);

            fwrite(&newShoe, sizeof(struct Brand), 1, cfPtr);

            proceed = 0;
        }
    }

    fclose(cfPtr);
    return 1;
  }
}


void view_all() /*Opens the file and prints all containing information*/
{
FILE *cfPtr;
cfPtr = fopen("stock.txt", "r");
char i[100]; /* A dummy value for the lenght of the file, so that all entries are printed*/

while (!feof(cfPtr))
{

    fgets(i, 100, cfPtr);
    puts(i);
}
fclose(cfPtr);
}
  1. The code is for a store
  2. It takes the inventory of the store
  3. New inventory is added to the existing ones.
  4. The inventory is displayed as 1 Nike 234 2 Adidas 343 3 Jordan 23 etc..
  5. When a sale is made i am not seeing it being updated in the file or when i display it.
  6. I would like a password login added to access the main menu
  • Well, a good start would be to add error checking code for your `fwrite` and `fread` calls. Also see [Why is “while ( !feof (file) )” always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – kaylum Mar 18 '16 at 03:54
  • I'd like to point out that it's better to manipulate structures with pointers instead. – akinfermo Mar 18 '16 at 05:11
  • `getchar(printf("Hit enter to continue..."));` won't even compile. – Spikatrix Mar 18 '16 at 06:23

0 Answers0