Click here to Skip to main content
15,923,015 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is just a part of my code, I don't get any errors, but it appears that function register_user doesn't assign values properly.

Is it because strcpy(user[n].username, username); and user[n].age = user[n].age + age; are not correct?

I realize that the values could've been easily assigned to with gets(user->username); and scanf("%d", &user->age);, but I'm trying to do it this other way.

What I have tried:

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

typedef struct {
    char username[30];
    int age;
} User;

int functions() {
    int option;
    printf("\n");
    printf("1. Register\n");
    printf("2. Display all users\n");

    printf("Choose a function: ");
    scanf("%d", &option);
    getchar();

    return option;
}

void display_user(const User user) {
    printf("\n");
    printf("Username: %s\n", user.username);
    printf("Age : %d\n", user.age);
}

void register_user(User *user, int n) {
    int i, age;
    char username[30];
    
    printf("\nUsername: ");
    scanf("%s", &username);
    printf("Age : ");
    scanf("%d", &age);

    strcpy(user[n].username , username);
    user[n].age = user[n].age + age;

    printf("\nRegistration complete\n");

    }

void display_all_users(const User user[], const int n) {
    int i;

    for (i = 0; i < n; i++) {
        display_user(user[i]);
    }
}


int main()
{
    User users[100];
    User user;
    int n = 0;

    while (1) {
        int option = functions();

        switch (option) {
            case 1:
                register_user(&user, n);
                users[n++] = user;
                break;
            case 2:
                display_all_users(users, n);
                break;  
            default:
                return 0;
        }
    }

    return 0;
}
Posted
Updated 11-Jan-22 7:51am

Additional what @k5054 suggested correctly you need to adjust void register_user

void register_user(User *user) {
   int i, age;
   char username[30];

   printf("\nUsername: ");
   scanf("%s", username);
   printf("Age : ");
   scanf("%d", &age);

   //strcpy(user[n].username , username);
   strcpy(user->username , username);
   //user[n].age = user[n].age + age;
   user->age = age;

   printf("\nRegistration complete\n");
}

And call it then this way in the main loop
register_user(&user);
 
Share this answer
 
v3
Comments
pacifistatheart 11-Jan-22 14:40pm    
Thank you, that's it.

I was trying to do it using "user[n].something", but I guess that just wouldn't work in this case.
0x01AA 11-Jan-22 14:42pm    
Thank you for accepting.

Yep, in resgister_user you pass an instance of a user for your main loop. Therefore [n] makes no sense.
For string variables, either char* or char[], when doing a scanf you do not pass the address of the variable in to scanf, just the variable itself e.g.
C
char username[30];
scanf("%s", username);
By passing in &username you're passing in the address of username, not its contents.
 
Share this answer
 
Comments
pacifistatheart 11-Jan-22 13:26pm    
Correct, as I was doing copy-paste and changing the naming of everything in order to post this question here, I missed to see that. But unfortunately even without "&", it doesn't work...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900