变量未更新

问题描述 投票:0回答:1

我遇到了一个小问题。我有一个变量 actual_account_balance,我需要将其设置为全局变量,以便它的值可以在函数之间共享和更新,但我不知道该怎么做,这样我就不必在代码中更改太多东西。

我尝试将变量 actual_account_balance 设置为全局

这是我的代码:

#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
c linux console
1个回答
0
投票

我有一个变量 actual_account_balance 我需要将其设置为全局变量,以便它的值可以在函数之间共享和更新

您可以通过将声明移出 main 来做到这一点:

int actual_account_balance=1000000;

并删除您在所有其他函数中拥有的该变量的所有声明。

© www.soinside.com 2019 - 2024. All rights reserved.