great!
this is where i was when i began. I tried to do just that but kept getting errors. I restored the original code for the day and implemented your suggestion of where to apply the subscript.
Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define MAX_STUDENTS 30
#define MAX_SIZE 48
/*Type Descriptions*/
struct student_data {
char lastName [MAX_SIZE];
char firstName [MAX_SIZE];
char majCode [5];
char yearInSchool [MAX_SIZE];
double crHrCompl;
double gPa;
};
/*fx prototypes*/
int get_info(struct student_data *studentPtr);
void student_output (struct student_data students [MAX_STUDENTS], int how_many);
int how_many = 0;
int main( void )
{
struct student_data students[MAX_STUDENTS];
how_many = get_info (students);
printf("student 1 name:%s\n",students[0].firstName);
printf("student 2 name:%s\n",students[1].firstName);
student_output (students, how_many) ;
return 0 ;
}
/*****fx******/
int get_info(struct student_data *studentPtr )
{
char input_string [MAX_SIZE];
char goAgain = 'y';
int i = 0;
int count = 0;
do{
printf("Enter Last Name: ");
fgets(input_string,MAX_SIZE,stdin);
input_string[strlen(input_string-1)] = '\0';
strncpy(studentPtr[i]->lastName, input_string, MAX_SIZE);
studentPtr->lastName[MAX_SIZE];
printf("Enter First Name: ");
fgets(input_string,MAX_SIZE,stdin);
input_string[strlen(input_string-1)] = '\0';
strncpy(studentPtr->firstName, input_string, MAX_SIZE);
studentPtr->firstName[MAX_SIZE];
printf("Enter Major Code: ");
fgets(input_string,MAX_SIZE,stdin);
input_string[strlen(input_string-1)] = '\0';
strncpy(studentPtr->majCode, input_string, 4);
studentPtr->majCode[4] = '\0';
printf("Enter Year in School: ");
fgets(input_string,MAX_SIZE,stdin);
input_string[strlen(input_string-1)] = '\0';
strncpy(studentPtr->yearInSchool, input_string, MAX_SIZE);
studentPtr->yearInSchool[MAX_SIZE];
printf("Enter Credit Hours: ");
fgets(input_string,MAX_SIZE,stdin);
studentPtr->crHrCompl = atof(input_string);
printf("Enter GPA: ");
fgets(input_string,MAX_SIZE,stdin);
studentPtr->gPa = atof(input_string);
i++;
printf("Press Enter to add another, otherwise press x to exit. \n");
scanf("%c%*c",&goAgain);
if (goAgain != 'y') {
break;
}
}/*do end braklet*/
while (i != MAX_STUDENTS);
count = i;
return count;
}
void student_output (struct student_data students [MAX_STUDENTS], int how_many)
{
int i = 0;
do{
for(i = 0; i < how_many; i++){
printf("Last Name : %s", students[i].lastName);
printf("First Name : %s", students[i].firstName);
printf("Major Code : %s\n", students[i].majCode);
printf("Year in School: %s", students[i].yearInSchool);
printf("Credit Hours : %4.2f\n", students[i].crHrCompl);
printf("GPA : %4.2f\n", students[i].gPa);
}
}
while( i !=how_many);
return;
}
ive been gettin these errors:
Code:
error C2232: '->lastName' : left operand has 'struct' type, use '.'
warning C4047: 'function' : 'const char *' differs in levels of indirection from 'int'
warning C4024: 'strncpy' : different types for formal and actual parameter 2
error C2198: 'strncpy' : too few arguments for call
...getting burned out....phew...