Home > OS >  random value is not the same in the txt file
random value is not the same in the txt file

Time:12-17

I'm creating a sales project for a specific product in c. What happens is that each customer is required to register to have a customer number, but I have a problem because the program manages the number, but when it goes to save it in the txt file, it doesn't give the correct one. However I'm having another problem with the client's name, if I write the full name it gives an error. How can I solve these problems? Thanks

This is my code:

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

typedef struct client CLIENT;

struct client {
    char name[80];
    char country[10];
    int tin;
};

void createprofile() {
    char tin_present;
    int i;
    
    
    FILE* bd;
    bd = fopen("bd.txt", "a ");
    CLIENT c;


    printf("\t *** Create customer profile *** \n");

    printf("Client code: ");

    for (i = 0; i < 1; i  ) {
        printf("%d\n ", rand() % 1000000); 
    }

    printf("Type your name: ");
    scanf("%s", &c.name);

    printf("TIN present?? ");
    scanf("%s", &tin_present);
    if (tin_present == 's' || tin_present == 'S') {
        while (1) {
            printf("\n\tEnter your TIN: ");
            scanf("%d", &c.tin);

            if (c.tin >= 999999999) {
                printf("\tNumber with more than 9 digits is not allowed. Please try again.");

            } else if (c.tin <= 99999999) {

                printf("\tNumber with less than 9 digits is not allowed. Please try again.");
            } else
                break;
        }
        printf("\tValid.\n");
    } else {
        printf("TIN not entered\n");
    }

    printf("What is your country? ");
    scanf("%s", &c.country);

    fprintf(bd, "code: %d | name: %s | tin: %i | country: %s \n", c.name, c.tin, c.country);
    fclose(bd);
}

CodePudding user response:

  1. What happens is that each customer is required to register to have a customer number, but I have a problem because the program manages the number, but when it goes to save it in the txt file, it doesn't give the correct one.

In this case, as ryyker already commented, the problem is that you're not storing your created number anywhere, you're just printing it. So, your program is just creating a value that is never read, or used.

You can do as ryyker suggested:

  • Create a new "info" inside your CLIENT object, where you can store the value of client-id (your randomized number);

So, consider changing this:

typedef struct client CLIENT;

struct client {
    char name[80];
    char country[10];
    int tin;
};

To this:

/*    If you want to, you can do this, change your struct prototype
and condense it into the struct declaration    */

typedef struct client {
    char name[80];
    char country[10];
    int tin;
    int client_code; // or any other variable name your prefer to
} CLIENT; // and condensing, you write here the struct shortcut/typedef-name
  • Now, you can store the generated value inside your client information too, so this:

for(i = 0; i < 1; i  ) {
    printf("%d", rand() % 1000000);
}

Should be transformed into this (note that as already commented before, there's no need to use this for with just one iteration, then just rip it off):

c.client_code = rand() % 1000000;
  • And then add the new value into your bd.txt file:

fprintf(bd, "code: %d | name: %s | tin: %i | country: %s \n", c.client_code, c.name, c.tin, c.country);

  1. I'm having another problem with the client's name, if I write the full name it gives an error.

I guess the problem you're having maybe is on the scanf function. In C, vectors are treated as pointers. This being said, when you write the vector's name in some place of your program, what the program receives is the memory address of the first position of the vector.

A char is datatype that expects just one character, so when we want to use strings in C, we need to create an array of characters, so we can create words with more than a single character.

The scanf function expects a pointer when we want to read a string, then if a string is a vector of characters and a vector passes a memory address to the function/program we'll not use the & in scanf, 'cause this symbol means passing the memory address of a variable to the function.

Consider making this change:

printf("Type your name: ");
scanf("%s", &c.name);

To this (the same applies to your country scanning):

printf("Type your name: ");
scanf("%s", c.name); // without the '&'

OBS: if you want to read a string/fullname with spaces, you should use this format for scanf:

printf("Type your name: ");
scanf("%[^\n]s", c.name); // [^\n] tells to the program to ignore newline characters until "Enter" be pressed

CodePudding user response:

Add client code member to struct, i.e. int client_code;

Then consider changing this:

for (i = 0; i < 1; i  ) {
    printf("%d\n ", rand() % 1000000); 
}

To this:

c.client_code = rand() % 1000000;
printf("Client Code: %d\n ", c.client_code);

Note on using rand(). If not already done, (in the code you have not shown) srand() should be called early in your program, one time only, Prior to calling rand(), to ensure that your pseudo-random values are truly pseudo-random. (The link provides examples how.)

Then in your file output, add client code:

fprintf(bd, "code: %d | name: %s | tin: %i | country: %s \n", c.client_code, c.name, c.tin, c.country);

Other suggestions might include either using an array of struct to contain more than one client

#define MAX_CUSTOMERS 100 //or what makes sense for your need

typedef struct {
    char name[80];
    char country[10];
    int tin;
    int client_code;
}CLIENT_s;

CLIENT_s client[MAX_CUSTOMERS];

...or use Linked Lists in which to read your client file, add, delete clients, then update file.

  •  Tags:  
  • c
  • Related