Hi all, My assignment is to "Define a structure type struct personal that would contain person name, date of joining and
Posted: Sun May 15, 2022 2:00 pm
Hi all,
My assignment is to "Define a structure type struct personal
that would contain person name, date of joining and salary using
this structure to read this information of 5 people and print the
same on-screen." I have most of the code done and I understand I
can simply declare 5 variables in my structure and have the user
enter all 5 users in 1 go, however, I am trying to program the code
in a way that you can add an unlimited number of employees.
My program runs fine until I display the employee information
after running the "newPersonel" function more than once. My
previous entries get overwritten by the new entries and garbage
values are printed at times. I'm not sure how to fix this bug, any
help is appreciated.
Please comment any changes/additions/modifications
done to the code please, thank you!
Below is my code in C .
/* Define a structure type struct personal that would contain
person name, date of joining
and salary using this structure to read this information of 5
people and print the same on screen.*/
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
//Defining structure for personel
struct personel
{
char name[50];
char date[10];
float salary;
};
//Declaring and initializing global variables used throughout
the code
int count=0;
char menuInput=0;
int employeLoopCount = 0;
//Declaring new pointer variable for personel in order to store
values without declaring number of struct personel variables
struct personel *p;
/*newPersonel function is used to add new personel to the
'database'. The function will scan for number of personal to be
added stores it tempCount.
Next, the function will initialize pointer variable 'p' and
allocate the address for the values to be stored in. The loop will
begin and it will run
tempCount number of times, scanning all employee information and
storing the data in the program. The data is stored in the
corresponding variable
and variable address. The 'employeLoopCount' variable is
supposed to work as an index incrementer/counter for the number of
employes that have been added to the database
and is used to avoid any overwritting of the same data over
previous entries in case the function is called multiple times
during the same session.*/
void newPersonel()
{
int tempCount=0;
printf("How many personel will you be
adding?:\n");
scanf("%d", &tempCount);
p = (struct personel*)malloc(tempCount *
sizeof(struct personel));
for(int i = 0; i < tempCount; i++)
{
printf("\n\n------------EMPLOYEE
#%d------------\n", (i+employeLoopCount+1));
printf("Please enter the full
name:\n\n\n");
scanf(" %[^\n]s",
&(p+employeLoopCount)->name);
printf("\n\nPlease enter the date
joined (DD-MM-YYYY):\n\n\n\n");
scanf(" %[^\n]s",
&(p+employeLoopCount)->date);
printf("\n\nPlease enter the
salary:\n\n\n\n");
scanf("%lf",
&(p+employeLoopCount)->salary);
employeLoopCount =
employeLoopCount+1;
}
count = tempCount+count;
}
/*The viewPersoel function is a simple print function that will
print all the stored personel information to the console.
Once all the information is printed the function is delayed for
5 seconds before returning to the main screen. This is so that the
user has enough time to
look at the entries.*/
void viewPersonel()
{
printf("------------DISPLAYING COMPANY
PERSONEL------------\n\n");
printf("NAME\t\tDATE JOINED\t\tSALARY\t\t\n");
for(int i=0;i<count;i++)
{
printf("%s\t", p.name);
printf("%s\t\t", p.date);
printf("$%.2lf\n", p.salary);
}
printf("\n\n");
Sleep(5000);
}
/*mainMenu function is used to have the user select what he
wants to do, server as a main menu.*/
void mainMenu()
{
printf("Greetings! Would you like to view personel
records or add new personel?:\n");
printf("1 - View Personel Records\n");
printf("2 - Add New Personel\n");
printf("3 - Exit\n");
scanf(" %c", &menuInput);
}
/*The main function is composed of switch case statements that
will call the corresponding functions and go through the program as
meant. Main will loop until the
user decides to exit the program*/
int main()
{
mainMenu();
switch (menuInput)
{
case '1':
viewPersonel(1);
main();
break;
case '2':
newPersonel(1);
printf("\nEntries
added to database. Returning to main menu.\n\n\n\n");
main();
break;
case '3':
printf("Exiting the
program, thank you for using.");
exit(1);
break;
}
}
C Codes > C Codes) Assignment4 > C a4p1_personal Structure.c > 1 /* Define a structure type struct personal that would contain person name, date of joining 2 and salary using this structure to read this information of 5 people and print the same on screen. / 3 4 #include <stdio.h> 5 #include <stdlib.h> 6 #include swindows.h> 2 8 //Defining structure for personel 9 struct personel 10 11 char name[50]; 12 char date[10]; 13 float salary; 14 15 }; 16 17 //Declaring and initializing global variables used throughout the code 18 int count=; 19 char menu Input:@; 20 int employeLoopcount = 0; 21 22 //Declaring new pointer variable for personel in order to store values without decLaring number of struct personel variables 23 struct personel Pj 24 25 /*newPersonel function is used to add new personel to the database. The function will scan for number of personal to be added stores it tempcount. 26 Next, the function will initialize pointer variable 'p' and allocate the address for the values to be stored in. The Loop will begin and it will run 27 tempCount number of times, scanning all employee information and storing the data in the program. The data is stored in the corresponding variable 28 and variable address. The employeLoop Count' variable is supposed to work as an index incrementer/counter for the number of employes that have been added to the 29 database bind is used to avoid any overwritting of the same data over previous entries in case the function is called multiple times during the same session." 30 31 void newPersonel 32 { 33 int tempCountee 34 35 printf("How many personel will you be adding?:\n"); 35 scanf("%d", &tempCount); 37 38 P = (struct personel)malloc(tempcount sizeof(struct personel)); 39 for(int i = 0; i < tempCount; i++) 48 { 41 printf("\n\n------------EMPLOYEE ------------\n", (1+employeLoopcount+1)); (\n- NXd ++ 42 printf("Please enter the full names\n\n\n"); 43 scanf(" Tins", &(p+employeloopCount)->name); 44 printf("\n\nPlease enter the date joined (DD-MM-YYYY): An\n\n\n"); 45 scanf("\n]s", &(p+employéLoopCount)->date); 46 printf("\n\nPlease enter the salarytinininin"); 47 scanf("f", &(p+employeLoopCount) ->salary); 48 employeLoop Count = employeLoop Count+1; 49 } se 51 count = tempCount+count 52 } 53 54 /*The viewPersoel function is a simple print function that will print all the stored personel information to the console. 55 Once all the information is printed the function is delayed for 5 seconds before returning to the main screen. This is so that the user has enough time to 56 Look at the entries. 57 58 void viewPersonel() 59 { 68 printf("-----DISPLAYING COMPANY PERSONEL---------\n\n"); 61 printf("NAME\t\EDATE JOINEDYE USALARYIt\t\n"); 62 for(int 1=0; iccount; i++) 63 64 printf("s\t", P[].name); 65 printf("%s\t\t", P.date); 66 printf("%.21f\n", P.salary); 67 68 } 69 printf("\n\n"); 70 Sleep(5080); 21 } } 72 73 /*mainMenu function is used to have the user select what he wants to do, server as a main menu. 74 void maintenu() 75 { 76 printf("Greetings! Would you like to view personel records or add new personei?: \n"); 77 printf("1 - View Personel Records\n"); 78 printf("2 - Add New Personel\n"); 79 printf("3. Exit\n"); 82 scanf("%c", &menuInput); 81 } 82 83 /*The main function is composed of switch case statements that will call the corresponding functions and go through the program as meant. Main will Loop until the 84 user decides to exit the program/ 85 int main() 86 { { 87 88 mainMenu(); 89 switch (menuInput) 90 { 91 case '1': 92 93 view Personel(1); 94 main(); 95 96 97 case 2: 98 99 newPersonel(1); 108 printf("\nEntries added to database. Returning to main senu. Anin\n\n"); 101 main(); 102 break; 103 184 case 3: 105 printf("Exiting the program, thank you for asing."); 106 exit(1); 107 break; 108 } 109 } } 118 break;
My assignment is to "Define a structure type struct personal
that would contain person name, date of joining and salary using
this structure to read this information of 5 people and print the
same on-screen." I have most of the code done and I understand I
can simply declare 5 variables in my structure and have the user
enter all 5 users in 1 go, however, I am trying to program the code
in a way that you can add an unlimited number of employees.
My program runs fine until I display the employee information
after running the "newPersonel" function more than once. My
previous entries get overwritten by the new entries and garbage
values are printed at times. I'm not sure how to fix this bug, any
help is appreciated.
Please comment any changes/additions/modifications
done to the code please, thank you!
Below is my code in C .
/* Define a structure type struct personal that would contain
person name, date of joining
and salary using this structure to read this information of 5
people and print the same on screen.*/
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
//Defining structure for personel
struct personel
{
char name[50];
char date[10];
float salary;
};
//Declaring and initializing global variables used throughout
the code
int count=0;
char menuInput=0;
int employeLoopCount = 0;
//Declaring new pointer variable for personel in order to store
values without declaring number of struct personel variables
struct personel *p;
/*newPersonel function is used to add new personel to the
'database'. The function will scan for number of personal to be
added stores it tempCount.
Next, the function will initialize pointer variable 'p' and
allocate the address for the values to be stored in. The loop will
begin and it will run
tempCount number of times, scanning all employee information and
storing the data in the program. The data is stored in the
corresponding variable
and variable address. The 'employeLoopCount' variable is
supposed to work as an index incrementer/counter for the number of
employes that have been added to the database
and is used to avoid any overwritting of the same data over
previous entries in case the function is called multiple times
during the same session.*/
void newPersonel()
{
int tempCount=0;
printf("How many personel will you be
adding?:\n");
scanf("%d", &tempCount);
p = (struct personel*)malloc(tempCount *
sizeof(struct personel));
for(int i = 0; i < tempCount; i++)
{
printf("\n\n------------EMPLOYEE
#%d------------\n", (i+employeLoopCount+1));
printf("Please enter the full
name:\n\n\n");
scanf(" %[^\n]s",
&(p+employeLoopCount)->name);
printf("\n\nPlease enter the date
joined (DD-MM-YYYY):\n\n\n\n");
scanf(" %[^\n]s",
&(p+employeLoopCount)->date);
printf("\n\nPlease enter the
salary:\n\n\n\n");
scanf("%lf",
&(p+employeLoopCount)->salary);
employeLoopCount =
employeLoopCount+1;
}
count = tempCount+count;
}
/*The viewPersoel function is a simple print function that will
print all the stored personel information to the console.
Once all the information is printed the function is delayed for
5 seconds before returning to the main screen. This is so that the
user has enough time to
look at the entries.*/
void viewPersonel()
{
printf("------------DISPLAYING COMPANY
PERSONEL------------\n\n");
printf("NAME\t\tDATE JOINED\t\tSALARY\t\t\n");
for(int i=0;i<count;i++)
{
printf("%s\t", p.name);
printf("%s\t\t", p.date);
printf("$%.2lf\n", p.salary);
}
printf("\n\n");
Sleep(5000);
}
/*mainMenu function is used to have the user select what he
wants to do, server as a main menu.*/
void mainMenu()
{
printf("Greetings! Would you like to view personel
records or add new personel?:\n");
printf("1 - View Personel Records\n");
printf("2 - Add New Personel\n");
printf("3 - Exit\n");
scanf(" %c", &menuInput);
}
/*The main function is composed of switch case statements that
will call the corresponding functions and go through the program as
meant. Main will loop until the
user decides to exit the program*/
int main()
{
mainMenu();
switch (menuInput)
{
case '1':
viewPersonel(1);
main();
break;
case '2':
newPersonel(1);
printf("\nEntries
added to database. Returning to main menu.\n\n\n\n");
main();
break;
case '3':
printf("Exiting the
program, thank you for using.");
exit(1);
break;
}
}
C Codes > C Codes) Assignment4 > C a4p1_personal Structure.c > 1 /* Define a structure type struct personal that would contain person name, date of joining 2 and salary using this structure to read this information of 5 people and print the same on screen. / 3 4 #include <stdio.h> 5 #include <stdlib.h> 6 #include swindows.h> 2 8 //Defining structure for personel 9 struct personel 10 11 char name[50]; 12 char date[10]; 13 float salary; 14 15 }; 16 17 //Declaring and initializing global variables used throughout the code 18 int count=; 19 char menu Input:@; 20 int employeLoopcount = 0; 21 22 //Declaring new pointer variable for personel in order to store values without decLaring number of struct personel variables 23 struct personel Pj 24 25 /*newPersonel function is used to add new personel to the database. The function will scan for number of personal to be added stores it tempcount. 26 Next, the function will initialize pointer variable 'p' and allocate the address for the values to be stored in. The Loop will begin and it will run 27 tempCount number of times, scanning all employee information and storing the data in the program. The data is stored in the corresponding variable 28 and variable address. The employeLoop Count' variable is supposed to work as an index incrementer/counter for the number of employes that have been added to the 29 database bind is used to avoid any overwritting of the same data over previous entries in case the function is called multiple times during the same session." 30 31 void newPersonel 32 { 33 int tempCountee 34 35 printf("How many personel will you be adding?:\n"); 35 scanf("%d", &tempCount); 37 38 P = (struct personel)malloc(tempcount sizeof(struct personel)); 39 for(int i = 0; i < tempCount; i++) 48 { 41 printf("\n\n------------EMPLOYEE ------------\n", (1+employeLoopcount+1)); (\n- NXd ++ 42 printf("Please enter the full names\n\n\n"); 43 scanf(" Tins", &(p+employeloopCount)->name); 44 printf("\n\nPlease enter the date joined (DD-MM-YYYY): An\n\n\n"); 45 scanf("\n]s", &(p+employéLoopCount)->date); 46 printf("\n\nPlease enter the salarytinininin"); 47 scanf("f", &(p+employeLoopCount) ->salary); 48 employeLoop Count = employeLoop Count+1; 49 } se 51 count = tempCount+count 52 } 53 54 /*The viewPersoel function is a simple print function that will print all the stored personel information to the console. 55 Once all the information is printed the function is delayed for 5 seconds before returning to the main screen. This is so that the user has enough time to 56 Look at the entries. 57 58 void viewPersonel() 59 { 68 printf("-----DISPLAYING COMPANY PERSONEL---------\n\n"); 61 printf("NAME\t\EDATE JOINEDYE USALARYIt\t\n"); 62 for(int 1=0; iccount; i++) 63 64 printf("s\t", P[].name); 65 printf("%s\t\t", P.date); 66 printf("%.21f\n", P.salary); 67 68 } 69 printf("\n\n"); 70 Sleep(5080); 21 } } 72 73 /*mainMenu function is used to have the user select what he wants to do, server as a main menu. 74 void maintenu() 75 { 76 printf("Greetings! Would you like to view personel records or add new personei?: \n"); 77 printf("1 - View Personel Records\n"); 78 printf("2 - Add New Personel\n"); 79 printf("3. Exit\n"); 82 scanf("%c", &menuInput); 81 } 82 83 /*The main function is composed of switch case statements that will call the corresponding functions and go through the program as meant. Main will Loop until the 84 user decides to exit the program/ 85 int main() 86 { { 87 88 mainMenu(); 89 switch (menuInput) 90 { 91 case '1': 92 93 view Personel(1); 94 main(); 95 96 97 case 2: 98 99 newPersonel(1); 108 printf("\nEntries added to database. Returning to main senu. Anin\n\n"); 101 main(); 102 break; 103 184 case 3: 105 printf("Exiting the program, thank you for asing."); 106 exit(1); 107 break; 108 } 109 } } 118 break;