Hey all, having a bit of a problem with controlling the repetition of input into a structure.
It seems that if i use a while loop with an integer as its condition then it will work fine, but if i want the while condition to be with a string, or char, then it seems to stay in the loop and i can't get out.
Anyway i've posted the code, so if anyone can help me it would be great.
Thanks againCode:#include <stdio.h> #define MAX_STUDENT 10 /* size of structure array */ typedef struct { int studentNum; char lastName[ 15 ]; char firstName[ 10 ]; double balance; } studentData; void read_studentRecord( studentData [] ); void print_studentRecord( studentData []); int main() { studentData student[MAX_STUDENT]; /* define a structure array of size MAX_CLIENT */ read_studentRecord( student ); print_studentRecord( student ); return 0; } /* Read client records and store them in a structure */ void read_studentRecord( studentData student[] ) { int i = 0; printf( "Enter student number( E.g S00023456, (0 to end input) )\n? " ); scanf( "%d", &student[i].studentNum ); while ( student[i].studentNum != 0 ) { printf( "Enter lastname, firstname, Phone #\n? " ); scanf( "%s%s%lf",student[i].lastName, student[i].firstName, &student[i].balance ); i++; printf( "Enter account number\n? " ); scanf( "%d", &student[i].studentNum ); } } /* Print client records stored in the structure */ void print_studentRecord( studentData student[] ) { int i = 0; if (student[0].studentNum != 0) printf( "\n%-6s%-16s%-11s%10s\n\n", "Acct", "Last Name", "First Name", "Balance" ); while ( student[i].studentNum != 0 ) { printf( "%-6d%-16s%-11s%10.2f\n", student[i].studentNum, student[i].lastName, student[i].firstName, student[i].balance ); i++; } }



LinkBack URL
About LinkBacks


