Search code examples
cfiletextauthenticationsystem

Assigning point to customer C programming


I would like to get assistance in my coding. I created a customer loyalty system using C (using file) and now I need to assign some called points to my customer, but what happenned is it will not assign any points to them.

{
for (int i = 0; i < (count / 3); i++)
    {
        fscanf(reward_file, "%s\n%s\n%d\n", user[i].Username, user[i].Password, &user[i].Points);
        if (Point = user[i].Points)
        {
            strcpy(user[i].Points, Reward);
            Point = Point + Reward;
            printf("%d", Point);
            system("pause");;
        }
    }
    fclose(reward_file);
    FILE *new_file = fopen("CustomerRegistration.txt", "w");
    for (int i = 0; i < NumOfCust; i++)
    {
        fprintf(new_file, "%s\n%s\n%d\n", user[i].Username, user[i].Password, user[i].Points);
    }
    fclose(new_file);
    printf("You Have Successfully Added Your Point!!!\n");
    printf("You Will be Redirected Back to Customer Privilege Menu");
    system("TIMEOUT \t 5");
}

I am using struct for my customer and here is the code

struct USER
{
    char Username[255];
    char Password[255];
    int Points;
};
struct USER user[100];

Data is obtained from a function called "Login"

FILE *LOGINFILE = fopen("CustomerRegistration.txt", "r");
if (LOGINFILE) 
{
    system("cls");
    printf("!!!Hello Customer!!!\n");
    printf("Please type in your Username and Password\n");
    printf("Username\t: ");
    //while ((c = getchar()) !='\n');
    gets(UN);
    printf("Password\t: ");
    gets(Pswd);

I also assigned global variable called "Point"

int Point = 0;

Your help will be highly appreciated.


Solution

  • From what I am understanding from your posted code you want to add a Reward to the Customer Points.

    Firstly you just need to add to user.Points the Reward, using strcpy makes no sense because that function is used for copying strings.

    that if( Point = user[i].Points ) also makes no sense firstly because C equality condition is represented by a double equal sign ( "==" ) and you don't need to make that check.

    The .Points member is an int and Reward is also an int ,so you can do arithmethic operations and there is no need to use another Point auxiliar.

        for (int i = 0; i < (count / 3); i++)
            {
                fscanf(reward_file, "%s\n%s\n%d\n", user[i].Username, user[i].Password, &user[i].Points);
    
                    user[i].Points += Reward;
                    printf("%d", user[i].Points);
    
                    system("pause");;
    
            }
     .....