Thread: I have finished my program, one problem

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    31

    I have finished my program, one problem

    Hello guys and gals I have finally finished my program! Everything runs smoothly until the program gets to the last function. Instead of giving me the lowest value it gives me a value that is not even part of the planes data file. Can anyone see what the problem is? And thanks again for all your help.

    anyway here is the program (in code tags)
    Code:
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <math.h>
    #include <time.h>
    //#include <malloc.h>
    
    
    
    /* function Declarations */
       int FunctionPassword(FILE *Fpassword);
       void FunctionPlanes(FILE *Fplanes);
       void FunctionDistout(FILE *Fplanes, FILE *Fdistout);
       void FunctionPrintSound(FILE *Fplanes);
    
    /*start of main */
        int main()
       {
       
          /*declaring files */
          FILE *Fpassword;
          FILE *Fplanes;
          FILE *Fdistout;
         
        /*opening files */
          Fpassword=fopen("/users/williammcfadden/Documents/school/EGR 115/Final Project/password.txt", "r");
          if(Fpassword == NULL)
          {
             printf("Sorry I can not find the password File \n");
             system ("pause");
             exit(0);
          }
          Fplanes=fopen("/users/williammcfadden/Documents/school/EGR 115/Final Project/planes.txt", "w+");
                            /* used "w+' because i want to write over any previous information in that file */
                            /* and not add any more information to the file                                 */
          Fdistout=fopen("/users/williammcfadden/Documents/school/EGR 115/Final Project/distlog.out", "w");
         
       /**************************************************    **********************************************/  
       
       /*password function */
          if (FunctionPassword(Fpassword) == 0)
          {
             printf("Sorry you really should not be using this program \n");
             system ("pause");
             exit(0);
          }
       
       /*Fplanes function this calls the Fplanes function which will */
       /*create a data file                                          */
          FunctionPlanes(Fplanes);
          if(Fplanes == NULL)
          {
             printf("Sorry the inputfile is empty \n");
             system ("pause");
             exit(0);
          }
       
       /* now I am going to call the distlog function                */
          FunctionDistout(Fplanes, Fdistout);
          
       /* Now I am going to call the function which will print to the screan and make sounds */
          FunctionPrintSound(Fplanes);
         
         
          printf("\n\n\n\n");
       
       
       
       
          fclose(Fpassword);
          fclose(Fplanes);
          fclose(Fdistout);
          exit(0);
       }
    
    
    /**************************************************    **********************************************/
    /**************************************************    **********************************************/
    /**************************************************    **********************************************/
    /**************************************************    **********************************************/
    /**************************************************    **********************************************/
            
            
        /*this is the password check function*/    
            
        int FunctionPassword(FILE *Fpassword)
       {
          char  name2[50];
          char name[6][50];
          int password[6];
          int PASSWORD;
          int a = 0; /*scans the file*/
          int b = 0;
          int c = 0;
          int d = 0;
          int e = 6;
          int f = 6;
          int g = 6;
          int h = 6;
       
          for(a=0; a<e; a++)
          {
             fscanf(Fpassword, "%s",name[a]);
             fscanf(Fpassword, "%d",&(password[a]));
          }
            
          printf("Please enter your name \n");
          scanf("%s", name2);
          printf("Please enter your password \n");
          scanf("%d", &PASSWORD);
            
          for(b=0; b<f; b++)
          {
             if(strcmp(name2, name[b])==0 && PASSWORD == password[b])
             {
                return 1;
             }
          }
          printf("please enter a correct name \n");
          scanf("%s", name2);
          printf("please enter a correct password \n");
          scanf("%d", &PASSWORD);
         
          for(c=0; c<g; c++)
          {
             if(strcmp(name2, name[c])==0 && PASSWORD == password[c])
             {
                return 1;
             }
          }
          printf("ONE LAST TIME BUDDY \n");
          printf("please enter your name \n");
          scanf("%s", name2);
          printf("please enter your password \n");
          scanf("%d", &PASSWORD);
          for(d=0; d<h; d++)
          {
             if(strcmp(name2, name[d])==0 && PASSWORD == password[d])
             {
                return 1;
             }
          }
          
          return 0;
       }
        
    /**************************************************    **********************************************/
    /**************************************************    **********************************************/
         /* This is the function which creates the plane coordinates data */
             
        void FunctionPlanes(FILE *Fplanes)
       {
            /*declaring arrays, I am going to use int to keep everything standard */
          int aXaxis[50];
          int aYaxis[50];
          int aZaxis[50];
          int a = 50;
          int b = 50;
          int c = 50;
          int d = 50;
          int x = 0;
          int y = 0;
          int z = 0;
          int q = 0;
                    
          for(x=0; x<a; x++) /*loop through aXaxis indecies */
          {         
             aXaxis[x] = (1 + ((double) rand() / ((double) RAND_MAX + 1.0) * 49.0));
          }
          for(y=0; y<b; y++) /*loop through aYaxis indecies*/
          {
             aYaxis[y] = 1 + ((double) rand() / ((double) RAND_MAX + 1.0) * 49.0);
          }
                    
          for(z=0; z<c; z++) /*loop through aZaxis indecies */
          {
             aZaxis[z] = 1 + ((double) rand() / ((double) RAND_MAX + 1.0) * 49.0);
          }
            
          for(q=0; q < d; q++) //this prints to the file
          {
             fprintf(Fplanes, "%d %d %d \n", aXaxis[q], aYaxis[q], aZaxis[q]);
          }   
    	} /*end of the function*/
    
    /**************************************************    **********************************************/
    /**************************************************    **********************************************/
         /*this is the function that will print to the distout.out file   */
        void FunctionDistout(FILE *Fplanes, FILE *Fdistout)
       {
          int aXaxis[50][3];
          int a = 0;
          int b = 0;
          int c = 0;
          int d = 50;
          int e = 50;
          int f = 55;
          int i = 0;
          int j = 0;
          float k = 0;
          int l = 0;
          int m = 0;
          int n = 0;
            
            
          for(i=0; i<d; i++)
          {
    		 fscanf(Fplanes, "%d, %d %d", a, b, c);
             aXaxis[i][0]=a;
             aXaxis[i][1]=b;
             aXaxis[i][2]=c;
          }
                    
          for(j=0; j<e; j++)
          {
            l = aXaxis[j][0];
            m = aXaxis[j][1];
            n = aXaxis[j][2];
             k = sqrt(l*l+m*m+n*n);
             if(k < f)
             {
                fprintf(Fdistout, "WARNING!!! Aircraft at %d %d %d is %3.2f KM away \n", l, m, n, k);
             }
          }
       }
    /**************************************************    **********************************************/
    /**************************************************    **********************************************/
            /*this is the function which will do the printing to screan and sounds */
        void FunctionPrintSound(FILE *Fplanes)
       {
          int aXaxis[50][3];
          float u = 90;
          int a = 0;
          int b = 0;
          int c = 0;
          int d = 0;
          int e = 0;
          int f = 0;
          int g = 50;
          int h = 50;
          int i = 0;
          int j = 0;
          int k = 0;
          int l = 0;
          int m = 0;
            
            
          for(i=0; i<g; i++)
          {
    	     fscanf(Fplanes, "%d, %d %d", a, b, c);
    		 aXaxis[i][0] = a;
             aXaxis[i][1] = b;
             aXaxis[i][2] = c;
          }
         
          for(j=0; j<h; j++)
          {
             d = aXaxis[j][0];
             e = aXaxis[j][1];
             f = aXaxis[j][2];
          
             if(sqrt(d*d+e*e+f*f) < u)
             {
                k = d;
                l = e;
                m = f;
    			u = sqrt(k*k+l*l+m*m);
             }
             
          }
         
          printf(  "Nearest aircraft at %d %d %d is %3.2f KM away \n", k, l, m, u);
          printf("\a \a \a \a \a \a \a \n");
         
       }
    Here is the password file

    Code:
    leia 12345
    darth 23456
    r2d2 34567
    solo 45678
    jabba 56789
    yoda 67890
    OK here are some more problems, It creates the random numbers file and it looks like this:

    Code:
    1 38 27 
    7 24 23 
    38 12 15 
    23 14 9 
    27 18 8 
    11 9 29 
    3 24 40 
    34 44 2 
    34 45 27 
    46 3 25 
    19 45 47 
    26 25 37 
    41 26 28 
    2 16 44 
    3 49 31 
    26 25 42 
    33 14 8 
    1 5 11 
    19 47 36 
    4 4 7 
    21 25 5 
    34 19 14 
    29 14 1 
    46 45 21 
    42 26 2 
    26 23 35 
    5 47 46 
    33 3 12 
    21 38 9 
    35 38 16 
    45 41 44 
    38 7 32 
    13 1 8 
    3 34 34 
    37 43 19 
    17 31 19 
    31 37 25 
    38 36 8 
    49 49 29 
    18 44 42 
    13 12 29 
    49 16 47 
    36 18 28 
    37 26 8 
    32 29 49 
    4 42 21 
    31 21 7 
    44 42 28 
    14 14 13 
    22 21 24
    But when it does calculations with the data file and prints to the distlog.out file it prints this

    Code:
    WARNING!!! Aircraft at 0 0 0 is 0.00 KM away 
    WARNING!!! Aircraft at 0 0 0 is 0.00 KM away 
    WARNING!!! Aircraft at 0 0 0 is 0.00 KM away 
    WARNING!!! Aircraft at 0 0 0 is 0.00 KM away 
    WARNING!!! Aircraft at 0 0 0 is 0.00 KM away 
    WARNING!!! Aircraft at 0 0 0 is 0.00 KM away 
    WARNING!!! Aircraft at 0 0 0 is 0.00 KM away 
    WARNING!!! Aircraft at 0 0 0 is 0.00 KM away 
    WARNING!!! Aircraft at 0 0 0 is 0.00 KM away 
    WARNING!!! Aircraft at 0 0 0 is 0.00 KM away 
    WARNING!!! Aircraft at 0 0 0 is 0.00 KM away 
    WARNING!!! Aircraft at 0 0 0 is 0.00 KM away 
    WARNING!!! Aircraft at 0 0 0 is 0.00 KM away 
    WARNING!!! Aircraft at 0 0 0 is 0.00 KM away 
    WARNING!!! Aircraft at 0 0 0 is 0.00 KM away 
    WARNING!!! Aircraft at 0 0 0 is 0.00 KM away 
    WARNING!!! Aircraft at 0 0 0 is 0.00 KM away 
    WARNING!!! Aircraft at 0 0 0 is 0.00 KM away 
    WARNING!!! Aircraft at 0 0 0 is 0.00 KM away 
    WARNING!!! Aircraft at 0 0 0 is 0.00 KM away 
    WARNING!!! Aircraft at 0 0 0 is 0.00 KM away 
    WARNING!!! Aircraft at 0 0 0 is 0.00 KM away 
    WARNING!!! Aircraft at 0 0 0 is 0.00 KM away 
    WARNING!!! Aircraft at 0 0 0 is 0.00 KM away 
    WARNING!!! Aircraft at 0 0 0 is 0.00 KM away 
    WARNING!!! Aircraft at 0 0 0 is 0.00 KM away 
    WARNING!!! Aircraft at 0 0 0 is 0.00 KM away 
    WARNING!!! Aircraft at 0 0 0 is 0.00 KM away 
    WARNING!!! Aircraft at 0 0 0 is 0.00 KM away 
    WARNING!!! Aircraft at 0 0 0 is 0.00 KM away 
    WARNING!!! Aircraft at 0 0 0 is 0.00 KM away 
    WARNING!!! Aircraft at 0 0 0 is 0.00 KM away 
    WARNING!!! Aircraft at 0 0 0 is 0.00 KM away 
    WARNING!!! Aircraft at 0 0 0 is 0.00 KM away 
    WARNING!!! Aircraft at 0 0 0 is 0.00 KM away 
    WARNING!!! Aircraft at 0 0 0 is 0.00 KM away 
    WARNING!!! Aircraft at 0 0 0 is 0.00 KM away 
    WARNING!!! Aircraft at 0 0 0 is 0.00 KM away 
    WARNING!!! Aircraft at 0 0 0 is 0.00 KM away 
    WARNING!!! Aircraft at 0 0 0 is 0.00 KM away 
    WARNING!!! Aircraft at 0 0 0 is 0.00 KM away 
    WARNING!!! Aircraft at 0 0 0 is 0.00 KM away 
    WARNING!!! Aircraft at 0 0 0 is 0.00 KM away 
    WARNING!!! Aircraft at 0 0 0 is 0.00 KM away 
    WARNING!!! Aircraft at 0 0 0 is 0.00 KM away 
    WARNING!!! Aircraft at 0 0 0 is 0.00 KM away 
    WARNING!!! Aircraft at 0 0 0 is 0.00 KM away 
    WARNING!!! Aircraft at 0 0 0 is 0.00 KM away 
    WARNING!!! Aircraft at 0 0 0 is 0.00 KM away 
    WARNING!!! Aircraft at 0 0 0 is 0.00 KM away
    Notice that none of these numbers are in the data file. Where did it get these 0's from?

    and when I get to the function which prints to the screen it gives this
    Code:
    [Session started at 2005-11-28 22:04:14 -0500.]
    Please enter your name 
    darth
    Please enter your password 
    23456
    Nearest aircraft at 0 0 0 is 00.00 KM away 
    
    
    
    
    
    
    Final Project has exited with status 0.
    What can be the problem? I keep editing this thread when I edit the program and I keep making things worse.
    Last edited by sloopy; 11-28-2005 at 10:52 PM.

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    31
    Do you guys also like how I cleaned everything up?

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    31
    ok could it be by compliler?

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    http://cboard.cprogramming.com/annou...ouncementid=51

    5. Don't bump threads. (Bumping: posting messages on threads to move them up the list).

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    1. if you're using gcc, at least use -W -Wall as the command line parameters

    foo.c: In function `FunctionDistout':
    foo.c:208: warning: format argument is not a pointer (arg 3)
    foo.c:208: warning: format argument is not a pointer (arg 4)
    foo.c:208: warning: format argument is not a pointer (arg 5)
    foo.c: In function `FunctionPrintSound':
    foo.c:250: warning: format argument is not a pointer (arg 3)
    foo.c:250: warning: format argument is not a pointer (arg 4)
    foo.c:250: warning: format argument is not a pointer (arg 5)

    Your printf/scanf functions are wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multi Thread Program Problem
    By ZNez in forum C Programming
    Replies: 1
    Last Post: 01-03-2009, 11:10 AM
  2. Program Termination Problem
    By dacbo in forum C Programming
    Replies: 3
    Last Post: 01-23-2006, 02:34 AM
  3. Replies: 20
    Last Post: 06-12-2005, 11:53 PM
  4. Console Program Problem
    By Breach23 in forum C++ Programming
    Replies: 3
    Last Post: 10-19-2001, 12:35 AM