Hey everyone,
I am trying to complete an assignment for school and I've run into a pretty large roadblock. I have multiple errors when attempting to compile it. Here is the assignment:
First, we need to create a Header file.
- The file will contain three constants NAME_SIZE 80, SSN_SIZE 13, NUM_PEOPLE 10.
- The file needs a typedef structure named Person which has a field for name(NAME_SIZE), social security number with dashes (SSN_SIZE), and an integer for year of birth.
- The file will need prototypes (function declaration minus the body) for the functions to be created in the .c file.
Second, we need to create our .c file which includes the functions, with bodies, listed below:
- getOnePerson: This takes a reference to a Person as a parameter. This function should get a single name, ssn, and year of birth from the terminal using scanf and put that data into the Person Structure.
- printOnePerson: This takes a Person (by value, not by reference) as a parameter. This function should print the name, ssn, and year from the structure in the following format: John_Heimershmidt:111-11-1111:1862 There are no spaces before and after the colons.
- getPeople: This takes an array of Person structures and number of people to get. Call getOnePerson for each Person in the array.
- printPeople: This takes an array of Person structures and a number of people to print. This method should print that many Person object from the array.
Third, a test program has been provided to test out our code. It may not be altered.
Here is the header file I created. It is called assign3.h:
Code:
#ifndef assign3_h
#define assign3_h
/*
#define NAME_SIZE 80;
#define SSN_SIZE 12;
#define NUM_PEOPLE 10;
*/
typedef struct
{
char name[80];
char ssn[12];
int yob;
}Person;
void getOnePerson(Person *person);
void printOnePerson(Person person);
void getPeople(Person *person[], int people);
void printPeople(Person person[], int people);
#endif
*****Notice I have commented out the global variable definitions. I was receiving errors when I attempted to define them in my header file; however I included them in my .c file and it worked. This is not a priority but if someone knows how I can fix this that would be great.
Here is my assign3.c file:
Code:
#include <stdio.h>
#include <string.h>
#include "assign3.h"
#define NAME_SIZE 80;
#define SSN_SIZE 12;
#define NUM_PEOPLE 10;
void getOnePerson(Person *person)
{
Person p;
char *name;
char ........n;
int *yob;
scanf("%s", name);
strncpy(p.name, name, NAME_SIZE);
scanf("%s", ssn);
strncpy((p.ssn), ssn, SSN_SIZE);
scanf("%d", yob);
p.yob = &yob;
*person = &p;
}
void printOnePerson(Person p)
{
printf("%s:%s:%d\n", p.name, p.ssn, p.yob);
}
void getPeople(Person *person[], int num)
{
int i;
for(i=0; i<num; i++)
{
getOnePerson(*person[i]);
}
}
void printPeople(Person person[], int num)
{
int i;
for(i=0; i<num; i++)
{
printOnePerson(*person[i]);
}
}
Here is the assign3Test.c file. This file may not be altered but I figured I'd include it anyway.
Code:
#include <stdio.h>
#include <string.h>
#include "assign3.h"
int main()
{
Person people[NUM_PEOPLE];
printf("Getting people\n");
getPeople(people, 3);
printf("\nPrinting people\n");
printPeople(people, 3);
}
Here are the errors I receive when I attempt to compile the program. Also, I am running short I am time so any suggestions to lead me in the right direction are greatly appreciated.
Code:
assign3Test.c: In function âmainâ:
assign3Test.c:7: error: âNUM_PEOPLEâ undeclared (first use in this function)
assign3Test.c:7: error: (Each undeclared identifier is reported only once
assign3Test.c:7: error: for each function it appears in.)
assign3Test.c:7: warning: unused variable âpeopleâ
assign3.c: In function âgetOnePersonâ:
assign3.c:21: error: expected â)â before â;â token
assign3.c:24: error: expected â)â before â;â token
assign3.c:27: warning: assignment makes integer from pointer without a cast
assign3.c:29: error: incompatible types when assigning to type âPersonâ from type âstruct Person *â
assign3.c: In function âgetPeopleâ:
assign3.c:42: error: incompatible type for argument 1 of âgetOnePersonâ
assign3.c:13: note: expected âstruct Person *â but argument is of type âPersonâ
assign3.c: In function âprintPeopleâ:
assign3.c:51: error: invalid type argument of âunary *â (have âPersonâ)
I know this is a lot of errors. This is only my third c program and my first one involving structures. I also seem to become confused when attempting to pass by value versus passing by reference. I have spent hours on this already and have gotten to the point where anything new I am trying just produces more errors.
Please let me know if you have any suggestions. Thank you all for your time!