Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Variable not being updated

I am quite stuck with a little problem. I have a variable actual_account_balance which I need to set as global so that it’s value can be shared and updated between functions, but I have no idea how to do that so that i wouldn’t have to change too many things in the code.

I try to set variable actual_account_balance as global

Here is my code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <string.h>




void addRecord();
void viewRecord();
void deleteRecord();
void clear();
void delay(int);
void printTitle(bool clearContent, int titleType);
void printError(int errorType);
void printChoices();
void login();

struct diary
{
    char date[15];
    char time[10];
    char place[25];
    char msg[500];
    //int actual_account_balance;
    char spent_today[15];
} d1;
 
 int actual_account_number ;
int main()
{
int actual_account_balance=1000000;
    login();
    int choice;
    bool run = true;
    while(run){
        printTitle(true, 2);
        printChoices();
        printf("\n\t\t            P R E S S  Y O U R  C H O I C E  K E Y\n\t\t\n");
        scanf("%d",&choice);
        getchar();
        switch (choice)
        {
            case 1:
                addRecord();
                break;
            case 2:
                viewRecord();
                break;
            case 3:
                deleteRecord();
                break;
            case 4:
                printf("\n\nClosing the system...\n");
                delay(2);
                clear();
                run = false;
                break;
            default:
                printTitle(true, 2);
                printf("\n\n\t\t\t\t     [X]:Invalid Input :(\n\n\t\t");
                delay(1);
                printf("\n\t\t            P R E S S  Y O U R  C H O I C E  K E Y\n\t\t");
                delay(1);
                printf("\n");
                delay(7);
                break;
        }
    }
}

void delay(int a)
{
    sleep(a);
}

void addRecord()
{
    FILE *fp;
    fp = fopen("diary.txt", "a");
    if (fp == NULL)
    {
        printError(1);
        exit(1);
    }
    char *spent_today;
    int actual_account_balance = 10000 ;

    printTitle(true, 2);
    printf("\n\t # Enter_Date(dd/mm/yyyy)     : ");
    fgets(d1.date, 15, stdin);
    printf("\n\t # Enter_Time(hh:mm am/pm)    : ");
    fgets(d1.time, 10, stdin);
    printf("\n\t # Enter_Location(place name) : ");
    fgets(d1.place, 25, stdin);
    printf("\n\t # Enter_Message : ");
    fgets(d1.msg, 500, stdin);
    printf("\n\t # Spent_today : ");
    fgets(d1.spent_today, 15, stdin);
    int spent_today_int = atoi(d1.spent_today);
    actual_account_balance -= (spent_today_int);
    printf("\n\t # Actual_Account_Balance :%d ", actual_account_balance);

    //fgets(d1.msg,500,stdin);
    printf("\n\nSaving...\n");
    fwrite(&d1, sizeof(d1), 1, fp);
    delay(2);
    fclose(fp);
}

void viewRecord()
{
    int actual_account_balance;
    FILE *fp;
    int ch, j = 1;
    fp = fopen("diary.txt", "rb");
    if (fp == NULL)
    {
        printError(1);
        exit(1);
    }
    clear();
    printTitle(true, 2);
    for (int i = 1; fread(&d1, sizeof(d1), 1, fp) != 0; i++)
    {
        printf("\n\t   # Record %d) : %s \t %s \t %s \t %s \t", i, d1.date, d1.time, d1.place, d1.spent_today);
    }
    fclose(fp);
    printf("\n\n\t  Which record you want to see from above?\n\t   Enter record number : ");
    scanf("%d", &ch);
    getchar();
    fp = fopen("diary.txt", "rb");
    if (fp == NULL)
    {
        printError(1);
        exit(1);
    }
    printTitle(true, 2);
    for (j; fread(&d1, sizeof(d1), 1, fp) != 0; j++)
    {
        if (j == ch)
        {
            printf("\n\t\t                              Date : %s", d1.date);
            printf("\n\t\t                              Time : %s", d1.time);
            printf("\n\t\t                          Location : %s", d1.place);
            printf("\n# Record,\n   %s", d1.msg);
            printf("\n\t\t                              Spent today : %s", d1.spent_today);

            printf("\n\t\t                              Account balance : %d", actual_account_balance);
            break;
        }
    }
    fclose(fp);
    if (j != ch)
    {
        printf("\n\t     The record is not match, please check and try again.");
    }
    printf("\n\n\t\t\t\tPress any key..!");
    getchar();
}

void deleteRecord()
{
    int flage = 0, ch;
    FILE *fp, *tptr;
    fp = fopen("diary.txt", "rb");
    if (fp == NULL)
    {
        printError(1);
        exit(1);
    }
    printTitle(true,1);
    for (int i = 1; fread(&d1, sizeof(d1), 1, fp) != 0; i++)
    {
        printf("\n\t\t# Record %d) : %s     %s     %s", i, d1.date, d1.time, d1.place);
    }
    printf("\n\n\t   Which record you want to delete from above?\n\t   Enter record number : ");
    scanf("%d", &ch);
    fclose(fp);
    fp = fopen("diary.txt", "rb");
    if (fp == NULL)
    {
        printf("\nFile is not found");
        exit(1);
    }
    tptr = fopen("temp.dat", "ab");
    for (int i = 1; fread(&d1, sizeof(d1), 1, fp) != 0; i++)
    {
        if (ch != i)
        {
            fwrite(&d1, sizeof(d1), 1, tptr);
        }
        else
        {
            flage = 1;
        }
    }
    fclose(fp);
    fclose(tptr);
    remove("diary.txt");
    rename("temp.dat", "diary.txt");
    if (flage == 0)
    {
        printf("\n\n\t\tRecord is not found..!");
    }
    else
    {
        printf("\n\t    *Record deleted successfully, press any key...");
    }
    getchar();
}

void clear(){
    system("clear");
}

void printTitle(bool clearContent, int titleType){
    if(clearContent){
        clear();
    }
    switch (titleType) {
        case 1:
            printf("\n\n\t\n\t P E R S O N A L  D I A R Y  M A N A G E M E N T  S Y S T E M  :)\n");
            break;
        case 2:
            printf("\n\n\n\t\t\n\t\t P E R S O N A L  D I A R Y  M A N A G E M E N T  S Y S T E M\n\t\t");
            break;
    }
}

void printError(int errorType){
    switch (errorType) {
        case 1:
            printf("\nFile is not found");
            break;

    }
}

void printChoices(){

    printf("\n\t\t\t\t PRESS[1]:Add Record");
    printf("\n\t\t\t\t PRESS[2]:View Record");
    printf("\n\t\t\t\t PRESS[3]:Delete Record");
    printf("\n\t\t\t\t PRESS[4]:Exit\n\t\t");
}


void login() {
    char username[20], *password;
    while (1) {
        clear();
        printf("\n\t\t P E R S O N A L  D I A R Y  M A N A G E M E N T  S Y S T E M\n\t\t");
        for (int i = 0; i < 62; i++)
        {
            printf("%c", 22);
        }
        printf("\n\n\t\t\tUsername: ");
        scanf("%s", username);
        // vyprázdnění bufferu stdin
        int c;
        while ((c = getchar()) != '\n' && c != EOF);
        password = getpass("\n\t\t\tPassword: ");
        if (strcmp(username, "user") == 0 && strcmp(password, "123") == 0){
            printf("\n\n\t\t\tWelcome. Login Success!\n");
            break;
        } else {
            printf("\n\n\t\t\tInvalid username or password. Try again.\n");
            sleep(3);
        }
    }
}```





ps I run it in ubuntu console

>Solution :

I have a variable actual_account_balance which I need to set as global so that it’s value can be shared and updated between functions

You can do that by moving the declaration OUT of main:

int actual_account_balance=1000000;

and also removing all declaration of that variables that you are having in every other functions.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading