C struct array input and output - arrays

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.

Related

1.How can I compare 2 chars 2.How can I increase the size of my dynamic array using realloc and print the information it has

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.

MY code is only displaying the input which i entered at the very end. I have attached my full code. PLEASE help me find where i am doing it wrong

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;
}

I have a problem initializing a variable connected to a struct array

I have this project where I have to create a struct array with C language for students' data, and I can't seem to get past this warning by my variable arr_student. The warning says that I have not initialized the variable, and whenever I try to debug it the IDE says that it has a memory error involving where it goes. I want to be able to declare it and use it as a way to get to the variables created in my struct array. If anyone knows what I could be missing, that would be greatly appreciated!
#include <stdio.h>
#include <string.h>
#define numero 2 //This number is the limiter for the number of students
struct Student
{ //Define struct array with student information
char id[50];
char gpa[50];
char address[50];
char phone_number[50];
char first_name[50];
char last_name[50];
};
int main(struct Student arr_student[numero])
{
int i; //Counter
char tempvalue[50]; //Temporary variable to store data in the array
char search[50]; //Search value input by user
int result = 1; //Initialized result to false
for (i = 0; i < numero; i++)
{
//Asks user for input on student information.
printf("\nEnter the information for student %d\n\n", i + 1);
printf("\nEnter first name: ");
scanf("%s", tempvalue);
printf("%s", tempvalue);
//prints value to verify if tempvalue recieved
strcpy(arr_student[i].first_name, tempvalue);
//error begins with the arr_student being underlined
printf("\nEnter last name: ");
scanf("%s", tempvalue);
strcpy(arr_student[i].last_name, tempvalue);
printf("\nEnter student id: ");
scanf("%s", tempvalue);
strcpy(arr_student[i].id, tempvalue);
printf("\nEnter student gpa: ");
scanf("%s", tempvalue);
strcpy(arr_student[i].gpa, tempvalue);
printf("\nEnter student address: ");
scanf("%s", tempvalue);
strcpy(arr_student[i].address, tempvalue);
printf("\nEnter student phone number: ");
scanf("%s", tempvalue);
strcpy(arr_student[i].phone_number, tempvalue);
}
printf("Enter the last name of the student you wish to examine data for: ");
//Asks input from the user for a name to search the data for
scanf("%s", search);
for (i = 0; i < numero; i++)
{
result = strcmp(search, arr_student[i].last_name);
}
if (result == 0) //A match is found in the array
{
printf("Here is the data on the student: %s", search);
printf("First Name\t Last Name\t ID\t GPA\t Address\t Phone Number\n");
//Prints out student information
printf("%s\t%s\t%s\t%s\t%s\t%s\n",
arr_student[i].first_name, arr_student[i].last_name, arr_student[i].id,
arr_student[i].gpa, arr_student[i].address, arr_student[i].phone_number);
}
else
{
printf("The name you have entered is not in our system, please try again");
return;
}
return 0;
}
Change the invalid main() definition from
int main(struct Student arr_student[numero])
{
to
int main(void) {
struct Student arr_student[numero] = {0};
Use width limited input.
char tempvalue[50];
// scanf("%s", tempvalue);
scanf("%49s", tempvalue);

Printing structures using loop

How to print structure contents using for-loop? If pointers, how to assign pointer to structure? I have attached code that I made that makes a structure using data that user inputs, and prints them out in an orderly fashion. My problem is that you have to write a lot of printf() and gets_s() statements to receive and print input. I feel like it would be easier to do this using a for-loop. I tried to make a for-loop as you can see in the code using pointers, but it doesn't compile, and I'm pretty sure that it is the wrong way to assign pointers to structures.
TL;DR: How to print structure contents using for-loop? If pointers, how to assign pointer to structure?
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <conio.h>
#define maxName 50
#define maxNation 50
#define maxAge 100
struct astronaut
{
char firstName[maxName];
char lastName[maxName];
char nation[maxNation];
int age = 0;
char missionName[maxAge];
int missionYear[50];
};
int main()
{
int i = 0;
struct astronaut candidate1;
struct astronaut candidate2;
struct astronaut candidate3;
struct astronaut mission1;
struct astronaut mission2;
struct astronaut mission3;
printf("Please enter the first name of the candidate: ");
gets_s(candidate1.firstName);
printf("Please enter the last name of the candidate: ");
gets_s(candidate1.lastName);
printf("Please enter the nationality of the candidate: ");
gets_s(candidate1.nation);
printf("Please enter the age of the candidate: ");
scanf("%d", &candidate1.age);
printf("Please enter the first name of the candidate: ");
gets_s(candidate2.firstName);
printf("Please enter the last name of the candidate: ");
gets_s(candidate2.lastName);
printf("Please enter the nationality of the candidate: ");
gets_s(candidate2.nation);
printf("Please enter the age of the candidate: ");
scanf("%d", &candidate2.age);
printf("Please enter the first name of the candidate: ");
gets_s(candidate3.firstName);
printf("Please enter the last name of the candidate: ");
gets_s(candidate3.lastName);
printf("Please enter the nationality of the candidate: ");
gets_s(candidate3.nation);
printf("Please enter the age of the candidate: ");
scanf("%d", &candidate3.age);
printf("Enter a Mission Name: ");
gets_s(mission1.missionName);
printf("Enter the year the mission was conducted: ");
scanf("%d", &mission1.missionYear);
printf("Enter a Mission Name: ");
gets_s(mission2.missionName);
printf("Enter the year the mission was conducted: ");
scanf("%d", &mission2.missionYear);
printf("Enter a Mission Name: ");
gets_s(mission3.missionName);
printf("Enter the year the mission was conducted: ");
scanf("%d", &mission3.missionYear);
struct astronaut *ptr;
struct candidate *ptr;
// printf("\n\tAstronaut Candidate Database\n");
// printf("Name: %s %s \t Age: %d \t Nationality: %s\n", candidate1.firstName, candidate1.lastName, candidate1.age, candidate1.nation);
// printf("Name: %s %s \t Age: %d \t Nationality: %s\n", candidate2.firstName, candidate2.lastName, candidate2.age, candidate2.nation);
// printf("Name: %s %s \t Age: %d \t Nationality: %s\n", candidate3.firstName, candidate3.lastName, candidate3.age, candidate3.nation);
// printf("\n\tAstronaut Mission Database\n");
// printf("Mission Name: %s \t Year: %d\n", mission1.missionName, mission1.missionYear);
// printf("Mission Name: %s \t Year: %d\n", mission2.missionName, mission2.missionYear);
// printf("Mission Name: %s \t Year: %d\n", mission3.missionName, mission3.missionYear);
for (int index = 0; index < 5; index++)
{
printf("Name: %s %s \t Age: %d \t Nationality: %s\n", *ptr.firstName, *ptr.lastName, *ptr.age, *ptr.nation);
ptr++;
}
_getch();
return 0;
}
I think you need 2 structs. An astronaut and a mission aren't the same thing. As it is now, you have several unused fields depending on whether you're entering data for an astronaut or a mission. You could do something like this:
#include <stdio.h>
#define maxName 50
#define maxNation 50
#define maxAge 100
struct astronaut
{
char firstName[maxName];
char lastName[maxName];
char nation[maxNation];
int age;
};
struct mission
{
char missionName[maxAge];
int missionYear; // why do you have an array of 50 ints for the missionYear?
};
void enter_candidate_data(struct candidate* cand)
{
// not famliar with gets_s .. I would use fgets here, you can read the manpage on that if you desire
printf("Please enter the first name of the candidate: ");
gets_s(cand->firstName);
printf("Please enter the last name of the candidate: ");
gets_s(cand->lastName);
printf("Please enter the nationality of the candidate: ");
gets_s(cand->nation);
printf("Please enter the age of the candidate: ");
scanf("%d", &(cand->age));
}
void enter_mission_data(struct mission* mis)
{
printf("Enter a Mission Name: ");
gets_s(mis->missionName);
printf("Enter the year the mission was conducted: ");
scanf("%d", &(mis->missionYear));
}
int main()
{
int i;
struct astronaut candidates[3];
struct mission missions[3];
for (i=0; i<3; i++)
{
enter_candidate_data(&(candidates[i]));
// you can put this in a separate loop if you want to enter all
// candidate data first
enter_mission_data(&(missions[i]));
}
// you could also write functions to print the data instead, depends on
// how you want it all presented
for (int i=0; i<3; i++)
{
printf("Name: %s %s \t Age: %d \t Nationality: %s\n",
candidates[i].firstName, candidates[i].lastName, candidates[i].nation);
printf("Mission Name: %s, year %d\n", missions[i].missionName,
missions[i].missionYear);
}
return 0;
}
You may want a number of missions (or even just one) associated with a particular astronaut. If so, I would define the data structures like this
#define MAX_ASTRONAUT_MISSIONS 20
struct mission
{
char missionName[maxAge];
int missionYear;
};
struct astronaut
{
char firstName[maxName];
char lastName[maxName];
char nation[maxNation];
int age;
struct mission missions[MAX_ASTRONAUT_MISSIONS];
};
This will allow you to associate MAX_ASTRONAUT_MISSION missions with each astronaut. Or, more realistically, a single mission could be associated with several astronauts. In that case, you may want data structures more like this
struct mission
{
char missionName[maxAge];
int missionYear;
};
struct astronaut
{
char firstName[maxName];
char lastName[maxName];
char nation[maxNation];
int age;
// using a pointer to missions will allow you to create one mission, and
// all the astronauts on that mission could get a pointer to it,
// designating they were all on that singular mission.
struct mission* missions[MAX_ASTRONAUT_MISSIONS];
};

Unable to enter Name with Spaces in C program?

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);

Resources