This program asks for employee name, age and Salary. There is one slight problem and i don't know what this wrong. When i enter name without space or just the first name, then program works correctly but as soon as i enter full name with space. The program just skips some values without asking and jumps to other values. I know their is something wrong with scant of "worker[i].name" and i have tried %c too but with no success.
struct employee {
int pAge;
float salary;
char name[30];
};
int main(void) {
struct employee worker[2];
for (int i = 0; i < 2; i++) {
printf("Enter Name of %d employee: ", (i+1));
scanf(" %s", worker[i].name);
printf("Enter Age of %d employee: ", (i+1));
scanf("%d", &worker[i].pAge);
printf("Enter Salary of %d employee: ", (i+1));
scanf("%f", &worker[i].salary);
}
printf("\n");
printf("List of All workers\n\n");
printf( "Age\tSalary\t\tName\n");
for (int i = 0; i < 2; i++) {
printf("%d\t%.2f\t\t%s\n", worker[i].pAge, worker[i].salary, worker[i].name);
}
}
Its very simple just use this instead:
scanf(" %[^\n]s", worker[i].name);
Related
I am trying to build a program which takes user input of employee details and prints it. I have separate functions for both of them.
Structure is as follows:
struct employee
{
int empId;
char name[20];
char empType[10];
int dd, mm, yyyy;
};
struct employee input()
{
struct employee e;
printf("Enter Employee ID: \n");
scanf("%d", &e.empId);
printf("Enter Employee name: \n");
scanf("%s", &e.name);
printf("Enter employee type: \n");
scanf("%s", &e.empType);
printf("Enter joining date: \n");
scanf("%d/%d/%d", &e.dd, &e.mm, &e.yyyy);
}
void display(struct employee emp[], int n)
{
printf("Employee ID \t Name \t Employee Type \t Joining date\n");
int i;
for (i = 0; i < n; i++)
{
printf("%d %s %s %d/%d/%d", emp[i].empId, emp[i].name, emp[i].empType, emp[i].dd, emp[i].mm, emp[i].yyyy);
}
}
int main()
{
int n = 5;
struct employee emp[n];
int i;
for(i = 0; i < n ;i++)
{
emp[i] = input();
}
display(emp, n);
return 0;
}
I am able to take the input properly but while printing I am getting all values as 0.
Requesting help!
Thanks!
You forgot about the return statement in the function input
//...
return e;
Also the arguments of these calls of scanf
printf("Enter Employee name: \n");
scanf("%s", &e.name);
printf("Enter employee type: \n");
scanf("%s", &e.empType);
are incorrect. You need to write
printf("Enter Employee name: \n");
scanf("%19s", e.name);
printf("Enter employee type: \n");
scanf("%9s", e.empType);
In general you should check that inputs were successful.
That for loop is unneccessary - you would end up printing the output 5 times, for some reason.
Also use typedef struct, it's convenient.
And don't type & when dealing with struct's string elements.
This code is a mess, by the way.
I am writing a program where if the user chooses "2" the program will add a new student record and if he chooses "4" then it will compare his surname with all the other surnames, it will find everyone that has the same surname and print their school information. The problems are the following:
When the user selects "2" my program actually shows all the prompts that are needed and you can input all the information however when I try to print it just gives me 0 0 0.000000 .When it should have given me 2(id) B(name) b(surname) 1(semester) 4(grade).
When the user selects "4" it reads the surname but when it goes to compare it with the others to see if it exists or not it just crashes.
Here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct student{
int id;
char name[50];
char surname[50];
int semester;
float grade;
}student;
int main()
{
int x,std,i;
x=0;
int car=0;
int flag=1;
struct student *ptr = NULL;
while (x!=8)
{
printf("\n1. Initialize student list\n2. Add a student record\n3. Delete a student record\n4. Display a student record by student surname\n5. Display students passed\n6. Display students failed\n7. Display all student records\n8. Exit");
printf("\nYour choice: ");
scanf("%d",&x);
if (x==1)
{
printf("How many new students? ");
scanf("%d",&std);
ptr = (student*)malloc(std*sizeof(student));
for(i = 0; i < std; i++){
printf("Enter detail of student #%d\n", (i + 1));
ptr[i].id=i+1;
printf("Enter first name: ");
scanf("%s", ptr[i].name);
printf("Enter last name: ");
scanf("%s", ptr[i].surname);
printf("Enter semester: ");
scanf("%d", &ptr[i].semester);
printf("Enter grade: ");
scanf("%f", &ptr[i].grade);
}
}
else if (x==2)
{
ptr=realloc(ptr,100* sizeof(student));
std=std+1;
ptr[std].id=std+1;
printf("Enter first name: ");
scanf("%s", ptr[std].name);
printf("Enter last name: ");
scanf("%s", ptr[std].surname);
printf("Enter semester: ");
scanf("%d", &ptr[std].semester);
printf("Enter grade: ");
scanf("%f", &ptr[std].grade);
flag=0;
}
else if (x==3)
{
/* code */
}
else if (x==4)
{
printf("Give the surname: ");
const char *sur;
scanf("%s", sur);
for (i = 0; i < std; i++)
{
if(strcmp(sur, ptr[i].surname)==0){
printf("%d ", ptr[i].id);
printf("%s ", ptr[i].name);
printf("%s ", ptr[i].surname);
printf("%d ", ptr[i].semester);
printf("%f ", ptr[i].grade);
printf("\n");
car=1;
}
}
if (car=0)
{
printf("That surname does not exist");
}
}
I may have missed a "{" but this is only part of my code
else if (x==7)
{
for(i = 0; i < std; i++){
printf("%d ", ptr[i].id);
printf("%s ", ptr[i].name);
printf("%s ", ptr[i].surname);
printf("%d ", ptr[i].semester);
printf("%f ", ptr[i].grade);
printf("\n");
}
}
}
}
For when the user chooses "2" everything goes well until its time to print so i tried increasing std by 1 two times.One when the program goes to option "2" and another time when it goes back to main. It didnt do anything so i returned it to how it was in the beginning.
For when the user chooses "4" I tried going with strcmp but it would just throw errors because of sur. I had it written as char sur. After I wrote it as const char *sur it stopped throwing errors but it would just crash when it went to compare it with the other.
QUESTION:
WAP to enter id, name, age and basic salary of n number of employees. Calculate the gross salary of all the employees and display it along with all other details in a tabular form, using pointer to structure.
--->
#include <stdio.h>
#include <conio.h>
struct employee {
char name[100];
int id, age;
int salary;
};
int main(){
struct employee emp, *ptr;
int i,n;
printf("Enter the no of employees\n");
scanf("%d",&n);
printf("****************INPUT EMPLOYEE DETAILS******************\n");
for(i=0;i<n;i++)
{
printf("\nEnter employee id of employee %d : ",i+1);
scanf("%d", &emp.id);
printf("Enter name of employee %d : ", i+1);
scanf("%s", &emp.name);
printf("Enter age of employee %d : ", i+1);
scanf("%d", &emp.age);
printf("Enter salary of employee %d : ", i+1);
scanf("%d", &emp.salary);
}
printf("\n");
printf(" DISPLAYING EMPLOYEE DETAILS \n");
printf("*********************************************************************\n");
ptr = &emp;
printf("\nEMP_ID\t\tEMP_NAME\tEMP_AGE\t\tGROSS_SAL\n");
for(i=0;i<n;i++){
printf("%d\t\t%s\t\t%d\t\t%d\n",ptr->id, ptr->name, ptr->age, ptr->salary);
}
return 0;
}
First of all you need to save each of your employees in variable so you can refer them later. Second because your variable is structure and you want loop through it your best choice is to use array.
I changed your code and highlighted them it worked for me i hope it will help you.
#include <stdio.h>
#include <conio.h>
struct employee {
char name[100];
int id, age;
int salary;
};
int main(){
struct employee *ptr; //One change is here
int i,n;
printf("Enter the no of employees\n");
scanf("%d",&n);
struct employee emp[n];
printf("****************INPUT EMPLOYEE DETAILS******************\n");
for(i=0;i<n;i++)
{
printf("\nEnter employee id of employee %d : ",i+1);
scanf("%d", &emp[i].id);
printf("Enter name of employee %d : ", i+1);
scanf("%s", emp[i].name); // Another change is here "%s" doesn't need '&'
printf("Enter age of employee %d : ", i+1);
scanf("%d", &emp[i].age);
printf("Enter salary of employee %d : ", i+1);
scanf("%d", &emp[i].salary);
}
printf("\n");
printf(" DISPLAYING EMPLOYEE DETAILS \n");
printf("*********************************************************************\n");
printf("\nEMP_ID\t\tEMP_NAME\tEMP_AGE\t\tGROSS_SAL\n");
for(i=0;i<n;i++){
ptr = &emp[i]; // This yet another change i grab "ptr" down here and assign it to "i" in each loop
printf("%d\t\t%s\t\t%d\t\t%d\n",ptr->id, ptr->name, ptr->age, ptr->salary);
}
return 0;
}
This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
Closed 4 years ago.
I wrote this C program to enter names and ages of 3 people. But the output wasn't my expectation. It was able to enter name and age for the first person, but it wasn't able for second and third persons. Please help.
#include <stdio.h>
#include <string.h>
int main()
{
int i, age;
char name[20];
for(i=0; i<3; i++)
{
printf("\nEnter name: ");
gets(name);
printf("Enter age: ");
scanf(" %d", &age);
puts(name);
printf(" %d", age);
}
return 0;
}
In short: Your 2nd puts is processing the '\n' from your scanf.
Fix by adding getchar(); after scanf
Explanation:
1st iteration:
printf("\nEnter name: ");
gets(name); // line is read from input
printf("Enter age: ");
scanf(" %d", &age); // a number is read from input, and the newline char ('\n') remains in buffer
puts(name);
printf(" %d", age);
2nd iteration:
printf("\nEnter name: ");
gets(name); // previously buffered newline char is read, thus "skipping" user input
printf("Enter age: ");
scanf(" %d", &age);
puts(name);
printf(" %d", age);
Same goes for 3rd iteration, and this is why you lose user input
The best way to store information of more than one person is to use struct, like
struct person {
int age;
char name[20];
};
and make array of struct, like
struct person people[3];
than use loop with accessing people[i].age and people[i].name, e.g.:
#include <stdio.h>
#include <string.h>
struct person {
int age;
char name[20];
};
#define ARR_SIZE 3
int main(int argc, char* argv[])
{
struct person people[ARR_SIZE];
int i;
char *lastpos;
for(i = 0; i < ARR_SIZE; i++)
{
printf("\nEnter name: ");
scanf(" %s", people[i].name);
if ((lastpos=strchr(people[i].name, '\n')) != NULL) *lastpos = '\0'; // remove newline from the end
printf("Enter age: ");
scanf(" %d", &people[i].age);
}
printf("This is the people you entered:\n");
for(i = 0; i < ARR_SIZE; i++)
{
printf("%d : %s : %d\n", i+1, people[i].name, people[i].age);
}
return 0;
}
UPDATE:
As you see I use scanf(" %s", people[i].name); instead of gets(people[i].name); to read name from stdin. Try both option for the following cases:
enter short name (e.g. John) and correct age (e.g. 15)
enter two word name (e.g. John Smith) and correct age (e.g. 17)
enter short name and uncorrect age (e.g. five)
Then read articles about value returned by scanf and cleaning input buffer
Here im working on my first coding in c programming. I've got the problem when I want to get the user input and display the output from the user input. here my code:
#include <stdio.h>
int main(){
printf("Enter the number : ");
int hallo = 0;
scanf("%d", hallo);
printf("hallo, %d", hallo);
}
after executing the code the last line not appear where is prinf("hallo, %d", hallo);. Which is to display the user input.
The 4th line of the code: scanf("%d", hallo);
Here &hallo should be used instead of just the variable name hallo.
The significance of the & sign is that it gives the address of a particular variable. So whatever the value is entered by the user, it will be stored at the address of the variable (in this case at the address of variable hallo).
Use the & after the , in scanf
Like this
scanf("%d", & hallo);
#include <stdio.h>
#include <stdlib.h>
typedef struct{
char name[30];
int id;
double salary;
} Employee;
int main()
{
int n;
printf("Number of employee to process: ");
scanf("%d",&n);
Employee employees[n];
printf("Enter %d Employee Details \n \n",n);
for(int i=0; i<n; i++){
printf("Employee %d:- \n",i+1);
printf("Name: ");
scanf("%[^\n]s",employees[i].name);
printf("Id: ");
scanf("%d",&employees[i].id);
printf("Salary: ");
scanf("%lf",&employees[i].salary);
char ch = getchar();
printf("\n");
}
printf("-------------- All Employees Details ---------------\n");
for(int i=0; i<n; i++){
printf("Name \t: ");
printf("%s \n",employees[i].name);
printf("Id \t: ");
printf("%d \n",employees[i].id);
printf("Salary \t: ");
printf("%.2lf \n",employees[i].salary);
printf("\n");
}
return 0;
}