0

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

Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265
Crow
  • 3
  • 3
  • 1
    Please copy paste the user input and output as text, not images. You also broke the image display links by modifying the link code after pasting in the image. – Gabriel Staples Mar 05 '22 at 18:58
  • 1
    Be sure to upvote any helpful answer, and mark correct the best answer if it answers your question. This is how you motivate answerers to keep on answering. – Gabriel Staples Mar 05 '22 at 19:04
  • My compiler is complaining big time at me about those printfs, isnt yours? Always read the warnings, the compiler is trying to help you – pm100 Mar 05 '22 at 19:09
  • 2
    and NEVER EVER use gets, use fgets – pm100 Mar 05 '22 at 19:10
  • Regarding what @pm100 said: from `man 3 gets` you'll see: `DESCRIPTION Never use this function.` And, from here (https://en.cppreference.com/w/c/io/gets) you'll see: `Notes Never use gets().` The function has been deprecated and removed entirely in C11 or later. See the Notes section of the link just above for reasons why. – Gabriel Staples Mar 05 '22 at 23:04

1 Answers1

3

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.
Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265
kwsp
  • 1,184
  • 7
  • 26