awesome... thought so but wasn't sure where and how.
I like the additional loop idea, i'll try that for my next programs.
So i tried out the subscripts ... it feels good, but i think my loop is not working or i have the wrong loop in there. it looks right but my output is wrong.
Code:
/*Name of Programmer: [email protected]
date written: 03125009
Purpose: A programm that collects and keeps student information.
Written for CSC_2473_290_31001
Complied on Centrino Duo T2250 1.73GHz machine running MS Win XP and complied
with MS Visual C++ 2008 Express
*/
#pragma warning(disable:4996)
/*that will stop the warning on scanf being deprecated */
#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 students [MAX_STUDENTS]);
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 last name:%s\n",students[0].lastName);
printf("student 2 last name:%s\n",students[1].lastName);*/
student_output (students, how_many) ;
return 0 ;
}
/*****fx******/
int get_info( struct student_data students[MAX_STUDENTS] )
{
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(students->lastName, input_string, MAX_SIZE);
students[i].lastName[MAX_SIZE];
printf("Enter First Name: ");
fgets(input_string,MAX_SIZE,stdin);
input_string[strlen(input_string-1)] = '\0';
strncpy(students->firstName, input_string, MAX_SIZE);
students[i].firstName[MAX_SIZE];
printf("Enter Major Code: ");
fgets(input_string,MAX_SIZE,stdin);
input_string[strlen(input_string-1)] = '\0';
strncpy(students->majCode, input_string, 4);
students[i].majCode[4] = '\0';
printf("Enter Year in School: ");
fgets(input_string,MAX_SIZE,stdin);
input_string[strlen(input_string-1)] = '\0';
strncpy(students->yearInSchool, input_string, MAX_SIZE);
students[i].yearInSchool[MAX_SIZE];
printf("Enter Credit Hours: ");
fgets(input_string,MAX_SIZE,stdin);
students[i].crHrCompl = atof(input_string);
printf("Enter GPA: ");
fgets(input_string,MAX_SIZE,stdin);
students[i].gPa = atof(input_string);
i++;
printf("Add another? Press 'y' to add another, otherwise press n to exit. \n");
scanf("%c%*c",&goAgain);
if (goAgain != 'y') {
break;
}
}/*doend braklet*/
while(i != MAX_STUDENTS);
count = i;
return count;
}
void student_output (struct student_data students [MAX_STUDENTS], int how_many)
{
int i = 0;
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);
i++;
if (i = how_many){
break;
}
}/*for end*/
return;
}
i enter 2 sets of data for two students and
for some reason i get only one set of data from the 2nd student entered, but with the 1st students gpa and credits.
Last Name: students[1].lastName value
First Name: students[1].firstName value
Major Code: students[1].mjaCode value
Year in school: students[1].yearInSchool value
Credits completed: students[0].crHrCompl value <--- why?
GPA : students[0].gPA value <------ why?