Reputation: 47
i'm trying to learn new algorythm with struct in c, then i try to make some test. But the code is too long and i want to make it more simple.
struct employee{
char ID[6];
char name[20];
char address[50];
long salary;
long T;
}casher1,casher2;
int main(int argc, char const *argv[]) {
int ch;
printf("Choose : ");
scanf("%d",&ch );
if (ch == 1) {
printf("Input Name : " );
scanf("%s",casher1.name );
printf("Input ID : " );
scanf("%s",casher1.ID );
printf("Input Salary : " );
scanf("%d",&casher1.salary);
printf("Input T : " );
scanf("%d",&casher1.T );
printf("\n");
casher1.salary = casher1.salary + casher1.T;
printf("ID : %s\n",casher1.ID );
printf("Name : %s\n",casher1.name );
printf("Salary : %d\n",casher1.salary );
}
else if(ch == 2) {
printf("Input Name : " );
scanf("%s",casher2.name );
printf("Input ID : " );
scanf("%s",casher2.ID );
printf("Input Salary : " );
scanf("%d",&casher2.salary);
printf("Input T : " );
scanf("%d",&casher2.T );
printf("\n");
casher2.salary = casher2.salary + casher2.T;
printf("ID : %s\n",casher2.ID );
printf("Name : %s\n",casher2.name );
printf("Salary : %d\n",casher2.salary );
}
return 0;
}
I'd expected the output like this for every casher ID : 12345 Name : test Salary : $2000
Upvotes: 1
Views: 59
Reputation: 1775
If you haven't learn functions yet, you should read a bit about them. I don't know what exactly you are trying to do, because you don't use the chasher's again, so as the code is now I don't know if you need to store them (but I will assume you do want to store them).
In that case, without the need of putting the cashers in an array you can create a function that accepts a casher as argument,
#include <stdio.h>
struct employee{
char ID[6];
char name[20];
char address[50];
long salary;
long T;
}casher1,casher2;
int casherFunction(struct employee *casher);
int main(int argc, char const *argv[]) {
int ch;
printf("Choose : ");
scanf("%d",&ch );
if( 1 == ch ){
casherFunction(&casher1);
}else if( 2 == ch ){
casherFunction(&casher2);
}
return 0;
}
int casherFunction(struct employee *casher){
printf("Input Name : " );
scanf("%s",casher->name );
printf("Input ID : " );
scanf("%s",casher->ID );
printf("Input Salary : " );
scanf("%ld",&casher->salary);
printf("Input T : " );
scanf("%ld",&casher->T );
printf("\n");
casher->salary = casher->salary + casher->T;
printf("ID : %s\n",casher->ID );
printf("Name : %s\n",casher->name );
printf("Salary : %ld\n",casher->salary );
return 0;
}
So here you first declare the function:
int casherFunction(struct employee *casher);
and then after the main you define it:
int casherFunction(struct employee *casher){
printf("Input Name : " );
scanf("%s",casher->name );
printf("Input ID : " );
scanf("%s",casher->ID );
printf("Input Salary : " );
scanf("%ld",&casher->salary);
printf("Input T : " );
scanf("%ld",&casher->T );
printf("\n");
casher->salary = casher->salary + casher->T;
printf("ID : %s\n",casher->ID );
printf("Name : %s\n",casher->name );
printf("Salary : %ld\n",casher->salary );
return 0;
}
the return 0 is a standard return value when there are no errors.
Note also that the function expects a pointer casherFunction(struct employee *casher)
that's why in the call to the function we write: casherFunction(&casher1)
with the &
.
Furthermore, elements in a struct pointer are under casher->ID
instead of casher.ID
.
Again, this is useful if you want the changes to be recorded in the cashers. then, you can for example, create a function to see a casher (is this case you won't need to pass a pointer, just a copy of the struct would be suffice).
The code would then be:
#include <stdio.h>
struct employee{
char ID[6];
char name[20];
char address[50];
long salary;
long T;
}casher1,casher2;
int casherFunction(struct employee *casher);
int casherShow(struct employee casher);
int main(int argc, char const *argv[]) {
int ch;
printf("Choose : ");
scanf("%d",&ch );
if( 1 == ch ){
casherFunction(&casher1);
}else if( 2 == ch ){
casherFunction(&casher2);
}
printf("%ld\n\n",casher1.salary);
casherShow(casher1);
return 0;
}
int casherFunction(struct employee *casher){
printf("Input Name : " );
scanf("%s",casher->name );
printf("Input ID : " );
scanf("%s",casher->ID );
printf("Input Salary : " );
scanf("%ld",&casher->salary);
printf("Input T : " );
scanf("%ld",&casher->T );
printf("\n");
casher->salary = casher->salary + casher->T;
printf("ID : %s\n",casher->ID );
printf("Name : %s\n",casher->name );
printf("Salary : %ld\n",casher->salary );
return 0;
}
int casherShow(struct employee casher){
printf("Name: ");
printf("%s\n",casher.name );
printf("Input ID : " );
printf("%s\n",casher.ID );
printf("Salary : " );
printf("%ld\n",casher.salary);
return 0;
}
Upvotes: 1