Google

Thursday, April 1, 2010

CSC202: Lab exercise April 1, 2010

Write a program that can store 2 student record using the following
structures:

#include
#include "string.h"

typedef struct name {
char firstname[16];
char lastname[16];
}NAME;

typedef struct student{
char id;
NAME studentname;
}STUDENT;

void main(){
STUDENT csc202[2]; //use an array

...
}

The program will use a loop to input 2 records into the array, then use
a separate loop to print out the contents of the array.

SOLUTION:
#include "stdio.h"
#include "string.h"

typedef struct name {
char firstname[16];
char lastname[16];
}NAME;

typedef struct student{
char id;
NAME studentname;
}STUDENT;

void main(){
STUDENT csc202[2];

//--input---
for(int i=0; i<2; i++){
puts("enter id: ");
scanf("%d",&csc202[i].id);
fflush(stdin);
puts("enter firstname: ");
scanf("%s",csc202[i].studentname.firstname);
fflush(stdin);
puts("enter lastname: ");
scanf("%s",csc202[i].studentname.lastname);
fflush(stdin);
}

//---output---
for(int i=0; i<2; i++){
printf("id: %d\nfirstname: %s\nlastname: %s\n",
csc202[i].id,csc202[i].studentname.firstname,
csc202[i].studentname.lastname);
}

}