Thread: Controlling Repetition in a Structure

  1. #1
    Silence
    Guest

    Question Controlling Repetition in a Structure

    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.

    Code:
    #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++;
    
    	}
    
    }
    Thanks again

  2. #2
    Silence
    Guest
    Sorry about double posting, but

    atm in the read_studentRecord function, the while loop works
    (because studentNum is of type int)

    but if i change the studentNum member to a char and change %d's to %s's it wont work for me

    it may have something to do with the while condition waiting for a '0', oh well, you guys see what i cant so...

    I hope i've explained that properly.

    Thanks

  3. #3
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    Originally posted by Silence
    Sorry about double posting, but

    atm in the read_studentRecord function, the while loop works
    (because studentNum is of type int)

    but if i change the studentNum member to a char and change %d's to %s's it wont work for me

    it may have something to do with the while condition waiting for a '0', oh well, you guys see what i cant so...

    I hope i've explained that properly.

    Thanks
    If you are using a char then it should be %c not %s. %s is for a string.

    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem referencing structure elements by pointer
    By trillianjedi in forum C Programming
    Replies: 19
    Last Post: 06-13-2008, 05:46 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. repetition with the "for" structure
    By Lillian in forum C Programming
    Replies: 3
    Last Post: 10-19-2002, 09:28 PM
  4. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM
  5. C structure within structure problem, need help
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 11-30-2001, 05:48 PM