Kunj
Kunj

Reputation: 3

C Structure Program showing garbage in numeric values

This code is about structures and includes basic input from the user but somehow that is not working properly with numeric values such as age, salary etc. I have tried fflush() but got no luck from using that and I can't find any other solution on the internet, everything except the garbage value works like a charm in this code

#include<stdio.h>
#include<conio.h>
#include<string.h>
struct employee
{
    unsigned int id;
    char name[30];
    int age;
    char address[50];
    char department[30];
    unsigned int salary;    
};
void main()
{
    int i,num;
    struct employee emp[50];

    printf("\nEnter number of employees whose data you want to enter : ");
    scanf("%d", &num);

    for(i=0;i<num;i++)
    {
        printf("\n||| EMPLOYEE NUMBER %d |||\n",i+1);
        
        printf("\nEnter your employee ID (5 Digits) : ");
        fflush(stdin);
        scanf("%u", &emp[i].id);
        
        printf("\nEnter your name : ");
        fflush(stdin);
        gets(emp[i].name);

        printf("\nEnter your age : ");
        fflush(stdin);
        scanf("%d", &emp[i].age);

        printf("\nEnter your address : ");
        fflush(stdin);
        gets(emp[i].address);

        printf("\nEnter your department : ");
        fflush(stdin);
        gets(emp[i].department);

        printf("\nEnter your salary : ");
        fflush(stdin);
        scanf("%u", &emp[i].salary);
    }

    for(i=0;i<num;i++)
    {
        printf("\n||| EMPLOYEE %d DETAILS ARE |||", i+1);
        printf("\nID : %u", &emp[i].id);
        printf("\nNAME : %s", emp[i].name);
        printf("\nAGE : %d", &emp[i].age);
        printf("\nADDRESS : %s", emp[i].address);
        printf("\nDEPARTMENT : %s", emp[i].department);
        printf("\nSALARY : %u", &emp[i].salary);
    }
getch();
}

USER INPUT

||| EMPLOYEE NUMBER 1 |||
Enter your employee ID (5 Digits) : 12345
Enter your name : The Crow
Enter your age : 24
Enter your address : 25/45 New York
Enter your department : HR
Enter your salary : 23000

OUTPUT

||| EMPLOYEE 1 DETAILS ARE |||
ID : 6612448
NAME : The Crow
AGE : 6612484
ADDRESS : 25/45 New York
DEPARTMENT : HR
SALARY : 6612568

Input:
USER INPUT

Output:
OUTPUT

Upvotes: 0

Views: 60

Answers (1)

user12734467
user12734467

Reputation:

You are printing the address of id, age, and salary. Delete the & in front of them in your printf statements to print their values.

        printf("\n||| EMPLOYEE %d DETAILS ARE |||", i+1);
        printf("\nID : %u", emp[i].id);
        printf("\nNAME : %s", emp[i].name);
        printf("\nAGE : %d", emp[i].age);
        printf("\nADDRESS : %s", emp[i].address);
        printf("\nDEPARTMENT : %s", emp[i].department);
        printf("\nSALARY : %u", emp[i].salary);

On another note, main has return type int so you should define it like this

int main(int argc, char** argv) {
    // your code goes here

    return 0;
}

As @pm100 mentioned in the comments:

  1. Never use gets. Use fgets.
  2. Read the outputs of the compiler carefully. They are often very informative.

Upvotes: 3

Related Questions