hi guys,
iam a beginner in c and i have got a project to make a program to keep the track of stock and cash of a shop i had done my work but i want to add purchase and iam unable to keep the work in history as a software works in shops so plz help me how i can make it i have to submit the program by tommorrow.

here is my program:

#include<stdio.h>
#include<conio.h>
void main()
{
    int stock=0,quan,ch;
    float mrp,cash=0;
    clrscr();
    while(1) {
        printf("\n1)SALE\n:");
        printf("\n2)TOTAL STOCK\n:");
        printf("\n3)TOTAL CASH IN HAND\n:");
        printf("\n5)EXIT\n:");
        printf("ENTER YOUR CHOICE:");
        scanf("%d",&ch);
        if(ch==1) {
            printf("\nENTER THE QUANTITY:");
            scanf("%d",&quan);
            printf("\nENTER THE TOTAL STOCK:");
            scanf("%d",&stock);
            stock=stock-quan;
            printf("\nENTER PRICE:");
            scanf("%f",&mrp);
            cash=mrp*quan;
            printf("\nTOTAL AMOUNT IS :%f",cash);
        } else if(ch==2) {
            printf("\nTHE TOTAL STOCK IS:%d",stock);
        } else if(ch==3) {
            printf("\nTHE TOTAL CASH IN HAND IS:%f",cash);
        } else if(ch==5) {
            exit(0);
        }
    }
    getch();
}

Dani AI

Generated

Your program needs two small design changes to behave like shop software: keep a single in-memory state (stock + cash) instead of re-asking for total stock on each sale, and persist that state plus a transaction history to files so data survives program exit. the code you posted overwrites cash on every sale (use accumulation) and asks for total stock inside the Sale branch — that makes the program forget the running totals. and are right: use files. Below is a compact, standard-C example showing a simple, correct approach (load state at start, update state on Purchase/Sale, append a human-readable history entry, and save after each change).

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define STATE_FILE "store.txt"
#define LOG_FILE   "history.txt"

int load_state(int *stock, double *cash) {
    FILE *f = fopen(STATE_FILE, "r");
    if (!f) { *stock = 0; *cash = 0.0; return 0; }
    if (fscanf(f, "%d %lf", stock, cash) != 2) { *stock = 0; *cash = 0.0; }
    fclose(f); return 1;
}

void save_state(int stock, double cash) {
    FILE *f = fopen(STATE_FILE, "w");
    if (!f) return;
    fprintf(f, "%d %.2f\n", stock, cash);
    fclose(f);
}

void log_tx(const char *type, int q, double price, int stock, double cash) {
    FILE *f = fopen(LOG_FILE, "a");
    if (!f) return;
    time_t t = time(NULL);
    fprintf(f, "%ld %s q=%d p=%.2f stock=%d cash=%.2f\n", (long)t, type, q, price, stock, cash);
    fclose(f);
}

/* main: menu loop, call load_state/save_state/log_tx as needed */

Practical tips: check scanf return values, prevent selling when stock < quantity, save immediately after each transaction to avoid data loss, and avoid nonstandard headers like conio.h (use standard stdio.h/stdlib.h/time.h). For money, prefer storing cents in an integer type to avoid floating-point rounding when the app grows.

Recommended Answers

All 3 Replies

plz tell me how can i add purchase in choice menu and it gets added to the program and it works as a software and keep the record feeded in it.

why don't you use files so even if your program exits, Data will be available

plz tell me how can i add purchase in choice menu ...

Do you seriously not know how to add PURCHASE to the menu?

...and it gets added to the program ...

Follow the pattern set forth for the other items.

...and it works as a software ...

No idea what this means.

...and keep the record feeded in it.

Output the record to a file to be read next time the program starts.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.