'C code collecting info through a function and passing it back, of datatype struct
//Program is to collect input from user so that can pass the array of inputs from the user to another print function but that are quite some Errors in the code that I cannot fix
#include <stdio.h>
#include<stdlib.h>
#define or ||
#define print printf
#define StudentPtr Person *student = NULL;
#define limit 999
//Just trying out some Pre-Processor tricks so Not particularly a big deal
typedef struct
{
char name[20];
int age;
union
{
char gender[20];
};
}Person[limit];
//Set Srtuct & Union to a size limit of 999
enum {seven = 7,eight,nine, ten, eleven};
int numofPersons()
{
int num;
printf("How many student will be accounted for: ");
scanf("%d",&num);
return num;
}
int Print_info(Person student[], int num);
int collInfo(Person Worker, int num)
{
for(int i =0; i < num;i++)
{
print("\nEnter your name: ");
scanf("%s", Worker[i].name);
print("Enter your age: ");
scanf("%d",& Worker[i].age);
print("Enter your gender(Male {or} Female): ");
scanf("%s", Worker[i].gender);
}
}
int main()
{
int num;
Person Worker[num];
num = numofPersons();
collInfo(*Worker, num);
Person *student[1] = NULL;
student[1] = &Worker[1];
Print_info(*Worker, num);
return 0;
}
//The Error occurs when I try to pass The Struct of pointer to this function i Keep gitting Errors no matter what u try
int Print_info(Person student[], int num)
{
for(int i =0; i < num;i++)
{
if (student[i]->age <= 11 or student[i]->age <= 12)
{
print("\n%s is a %d, %s from grade {%d}",student[i]->name,student[i]->age ,student[i]->gender, seven);
}
else if (student[i]->age < 12 or student[i]->age <= 13)
{
print("\n%s is a %d, %s from grade {%d}",student[i]->name,student[i]->age ,student[i]->gender, eight);
}
else if (student[i]->age < 13 or student[i]->age <= 14)
{
print("\n%s is a %d, %s from grade {%d}",student[i]->name,student[i]->age ,student[i]->gender, nine);
}
else if (student[i]->age < 14 or student[i]->age <= 15)
{
print("\n%s is a %d, %s from grade {%d}",student[i]->name,student[i]->age ,student[i]->gender, ten);
}
else if (student[i]->age < 15 or student[i]->age <= 16)
{
print("\n%s is a %d, %s from grade {%d}",student[i]->name,student[i]->age ,student[i]->gender, eleven);
}
else
{
print("Invalid /...!");
}
// student++;
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
