0

I absolutely hate hate hate hate HATE to be doing this, because I've figured out every other program I've done relatively alone with my C Book.

I'm writing a program that declares an array of structs, each with a name and age field. The data is read from a file arranged like:

Randomname 15
Othername 72

With the name and age field being (hopefully) obviously ordered.

My task is to rearrange each individual part of the array of structs alphabetically. I've written a program that should work (in my head, it did, at least) but my output is nonsense which means I've gone horribly wrong somewhere.

Usually I'm pretty good at debugging this sort of thing but my experience with structs is limited. Yes, this is a homework question, so a full answer isn't what I'm looking for. Here's my code (it's short)

#include <stdio.h>
#include <string.h>
  

int main( int argc, char *argv[] ) {

  struct people{ 
    char name[21];
	int age;
  } person[100], temp;
  
  char tempst[21];
  int tempint, k, i = 0;
  FILE *fin;
  
  fin = fopen( "testdata95", "r" );
  
  for( k = 0; k <= 21; k++ ) person[k].age = 0; /* fills the structs array of ages with for end print */
  
  while( (fscanf( fin, "%c", tempst )) != EOF ){
    
	strcpy( person[i].name, tempst );
	fscanf( fin, "%d", &tempint );
	person[i].age = tempint;
	
	i++;
	
  }
	
  for(i = 0; i <= 21; i++){
    for(k = i + 1; k <= 21; k++){
	
	  if( (person[i].name[0]) < (person[k].name[0]) ){
	
	    temp = person[i];
	    person[i] = person[k];
	    person[k] = temp;
	
	  }
	}
  }
  
  i = 0;

  while( person[i].age != 0 ){

    printf( "%s %d", person[i].name, person[i].age );
	i++;
	
  }

  return 0 ;
  
}
2
Contributors
1
Reply
3
Views
7 Years
Discussion Span
Last Post by sree_ec
0

I absolutely hate hate hate hate HATE to be doing this, because I've figured out every other program I've done relatively alone with my C Book.

I'm writing a program that declares an array of structs, each with a name and age field. The data is read from a file arranged like:

Randomname 15
Othername 72

With the name and age field being (hopefully) obviously ordered.

My task is to rearrange each individual part of the array of structs alphabetically. I've written a program that should work (in my head, it did, at least) but my output is nonsense which means I've gone horribly wrong somewhere.

Usually I'm pretty good at debugging this sort of thing but my experience with structs is limited. Yes, this is a homework question, so a full answer isn't what I'm looking for. Here's my code (it's short)

#include <stdio.h>
#include <string.h>
  

int main( int argc, char *argv[] ) {

  struct people{ 
    char name[21];
	int age;
  } person[100], temp;
  
  char tempst[21];
  int tempint, k, i = 0;
  FILE *fin;
  
  fin = fopen( "testdata95", "r" );
  
  for( k = 0; k <= 21; k++ ) person[k].age = 0; /* fills the structs array of ages with for end print */
  
  while( (fscanf( fin, "%c", tempst )) != EOF ){
    
	strcpy( person[i].name, tempst );
	fscanf( fin, "%d", &tempint );
	person[i].age = tempint;
	
	i++;
	
  }
	
  for(i = 0; i <= 21; i++){
    for(k = i + 1; k <= 21; k++){
	
	  if( (person[i].name[0]) < (person[k].name[0]) ){
	
	    temp = person[i];
	    person[i] = person[k];
	    person[k] = temp;
	
	  }
	}
  }
  
  i = 0;

  while( person[i].age != 0 ){

    printf( "%s %d", person[i].name, person[i].age );
	i++;
	
  }

  return 0 ;
  
}

To start with, put prints on this loop and see what you are reading.
#

while( (fscanf( fin, "%c", tempst )) != EOF ){
#
 
#
strcpy( person[i].name, tempst );
#
fscanf( fin, "%d", &tempint );
#
person[i].age = tempint;
#
 
#
i++;
#
 
#

}
Remember, you have %s to read strings....

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.