INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

i need help. something wrong with for loop that prints

i need help. something wrong with for loop that prints

(OP)
i need help i dont knwo why this program keeps on crashing  and not organizing the file




/*
 *
 *Thomas Santamaria
 *This program will sort the by lastname, firstname, and score
 *
 */
 
 
 
 #include<stdio.h>
 #include<string.h>                   /*string library that allows the usage of many built in functions*/
 #define NMAX 120
 #define MAXLEN 50
 
 int rateSort( const int nump1, const int score1[], int I[]);
 
 
 
 int main(void)
 {
 
          
 int nump, moves,k;                                         /* definitions are from a the notes*/
 char filename[25],c;
 int I[NMAX];
 int score[NMAX];
 char last [NMAX][MAXLEN];
 char first [NMAX][MAXLEN];
 int i;                                           /*this for the message looop*/
 FILE *ifile;
 
 
 printf("What would you like to call the file containing the information(less than 20 characters)?>");
 
 i = 0;
 while ( i < 80 && ( c = getchar())   != '\n')
 {
     filename[i] =  c;                                /*store the characters entered */
     ++i;
     
 }
 
 filename[i] = '\0';                                        /*terminate the string*/
 
 printf("The file name is:     ");
 
 puts(filename);
 
 
 
 ifile = fopen(filename, "r");
 
 nump = 0;                                           /*intializing this variable before using it*/
 
 while(fscanf(ifile,"%s %s %d", last[nump], first[nump], &score[nump]) != EOF)
 {
                /*printf("%-20s %-20s %-8d\n", last[nump], first[nump], score[nump]);*/
 
 nump++;        
 }
 
     
 
 
 printf("%d\n", nump);
 
 
 moves = rateSort ( nump, score, I);
 
 /*   for( k = 0; k < nump; k++){
 
     printf(" %-8d\n", score[I[k]]);
     }*/
  
      
     
 for( k = 0; k < nump - 1; ++k){
 
     printf("%-20s %-20s %-8d",last [I[k]], first [I[k]], score[I[k]]);
 }    
 
 
 
 return (0);
 
 }
 
 
 
 
 
 int rateSort( const int nump1,  const int score1[], int Ia[] )
 
 {                                                                     /*bubble sort from Bronson Book*/
     
     
     int i, j, scorea, moves = 0;
     
     
     
     for( i = 0; i <  (nump1 - 1); i++)
     {    
         for (j = 1; j < nump1; j++)
         {
             if (score1[j] < score1[j-1])
             {
                 scorea = Ia[j];
                 Ia[j] = Ia[j-1];
                 Ia[j-1] = scorea;
                 moves++;
         }
             
     }
 }
     return (moves);
 }

RE: i need help. something wrong with for loop that prints

This is not a C forum. Also, when posting code, use the [code][/code] tags.

Tim

RE: i need help. something wrong with for loop that prints

This is a "Code Reuse & Migration" forum, so any language should be appropriate; but I do agree that this is a question best suited to the "C" forum since it has to do with a code problem rather than a question about improving a working program.
Re-post your question with code in the appropriate [code][/code] tags, in the C forum.

RE: i need help. something wrong with for loop that prints

Quote (cpjust):


This is a "Code Reuse & Migration" forum, so any language should be appropriate

Well that's true enough. It does seem, though, that this forum gets a significant proportion of posts which are just asking for code help rather than issues of how to make best use of code through reuse and related issues.

Tim

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close