Thread: saving a file

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    5

    saving a file

    Please may you follow the link below and advise why the program is saving a 0 value of magnitude.Where did i go wrong in the saving function
    Code:
    /*
      Purpose     :A software application is required that will calculate and 
                   display the equivalent rectangular co ordinates of a vector whose
                   polar form is given, or the equivalent polar co ordinates of a 
                   vector whose rectangular form is given.
      Inputs      :rectangular and polar values
      Processes   :
      Outputs     :             
      Date: 15 August 2013
      
      Author:  Collen Charinya(211064971)
    Version        :1
    */
    //------------------------------------------------------------------------------
    //Pre-Processors
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    
    
    
    //------------------------------------------------------------------------------
    //Constant
    #define MAX 50;
    #define PI 3.142857143
    #define RA 57.2957795
    
    
    //------------------------------------------------------------------------------
    //user define type
      struct vComponents
      {
         float x;
         float y;
         float angle;
         float magn;    
      }; 
    //------------------------------------------------------------------------------
    //prototypes
    //function declaration
    void discription(void);
    float polRecCon(struct vComponents vctr);
    float recPolCon(struct vComponents vctr);
    void save(struct vComponents vctr);
    
    
    //------------------------------------------------------------------------------
    //******************************************************************************
    //******************************************************************************
    
    
    //main programe
    
    
    
    
    int main(int argc, char *argv[])
    {
        //declaring variables
        struct vComponents vctr = {0,0,0,0};
        int counter = 0;
        int choice;
        int again = 1;
        float horizontal;
        float vertical;
        int reenter;
        float magn = 0;
      //printing the menu  
      puts("-----------------MENU--------------------\n");
      printf("1.Discription of Application\n");
      printf("2.Polar to rectangular Conversion\n");
      printf("3.Rectangular to Polar Conversion\n");
      printf("4.Saving to file\n");
      printf("5.Exit Application\n");
      
      printf("\n");
     // while (again == 1)
      //{
        printf("Please enter your choice\n");//entering choice
       scanf("%d",& choice);
       system("cls");
       
       while ( choice != 5)
       {
       //case structure
       switch (choice)
       {
          case 1 :
               discription();
          break;
          
          case 2 :
               polRecCon(vctr);
          break;
          
          case 3 :
               recPolCon(vctr);
          break;
          
          case 4 :
               save(vctr);
          break;
          
          case 5:
               exit(0);
          
          default : printf("you have entered an invalid input,please check the input and reenter.\n");
          //scanf("%d",&reenter);
          puts("-----------------MENU--------------------\n");
      printf("1.Discription of Application\n");
      printf("2.Polar to rectangular Conversion\n");
      printf("3.Rectangular to Polar Conversion\n");
      printf("4.Saving to file\n");
      printf("5.Exit Application\n");
          
          
         // printf("Please enter your choice\n");//entering choice
        //  scanf("%d",& choice);
    }  
        
         printf("\n");                  
       // printf("If you want to continue please press 1 or 0 to quit\n");
       // scanf("%d",& again);
        
        system("cls");
        printf("\n"); 
        puts("-----------------MENU--------------------\n");
        printf("1.Discription of Application\n");
        printf("2.Polar to rectangular Conversion\n");
        printf("3.Rectangular to Polar Conversion\n");
        printf("4.Saving to file\n");
        printf("5.Exit Application\n");
        
         
       printf("Please enter your choice\n");//entering choice
        scanf("%d",& choice);
      
             
       }
       
       
        printf("\n"); 
        printf("Please enter your choice\n");
       scanf("%d",& choice);
       system("cls");
     // }         
      system("PAUSE");
         
      return 0;
    }
    //------------------------------------------------------------------------------
    //******************************************************************************
    //******************************************************************************
    
    
    /*MENU
    
    
    
    
    
    
    */
    /*DESCRIPTION FUNCTION
    */
    void discription(void)
    {
       printf("The software application will calculate and display the equivalent \
       rectangular coodinates of a vector whose polar form is given, or the equivalent \
       polar coodinates of a vector whose rectangular form is given \n\n");
       system("PAUSE");
       system("cls");
       
     //  return 0;                
    }
    
    
    /*POLAR TO RECTANGULAR CONVERSION*/
    float polRecCon(struct vComponents vctr)
    {
       //declaring variables
       float horizontal;
       float vertical;
       
       
       //prompting angle and magnitude
       printf("Please enter angle\n"); 
       scanf("%f",&vctr.angle);
       printf("Please enter the magnitude\n");
       scanf("%f",&vctr.magn);
       //calculating the value of x and y
       horizontal = vctr.magn * cos(vctr.angle*PI/180);
       vertical = vctr.magn * sin(vctr.angle*PI/180);
       
       vctr.x = horizontal;
       vctr.y = vertical;
       
       printf("The value of the x axis is %.2f\n",vctr.x);
       printf("The value of the y axis is %.2f\n",vctr.y);
       system("PAUSE");   
      // system("cls");    
            
    }
    
    
    /*RECTANGULAR TO POLAR CONVERSION
    
    
    */
    float recPolCon(struct vComponents vctr)
    {
      //declaring variables
      
      float magnitude;
      float angl;
      
      //prompting horizontal and vertical
      printf("Please enter the value of the x-axis \n");
      scanf("%f",&vctr.x);
      printf("Please enter the value of the y-axis\n");
      scanf("%f",&vctr.y);
      
      //calculating the value of magnitude and angle
      magnitude = sqrt ( pow((vctr.x),2) + pow((vctr.y),2));
      angl = RA * atan ( (vctr.y) / (vctr.x) ); 
      
      //assigning magnitude and angle.
      vctr.magn = magnitude;
      vctr.angle = angl;
      
      printf("The magnitude of the vector is %.2f\n",vctr.magn);
      printf("The angle of the vector is %.2f\n",vctr.angle);
      
      
       system("PAUSE");
       system("cls");
     //  return 0;
    }
    
    
    //saving file
    void save(struct vComponents vctr)
    {
    
    
       FILE *myFile;//pointer to file
       
       if (( myFile = fopen("vector.txt","w")) == NULL)
         {
           printf("Cannot open the file\n");        
         }
         else
           {
              fprintf(myFile,"%.2f",vctr.magn);
              printf("The magnitude is %.2f ",vctr.magn);
              system("pause");
             /* fprintf(myFile,"%.2f %.2f %.2f %.2f\n",vctr.magn,vctr.angle,vctr.x,vctr.y);
              scanf("%f%f%f%f",&vctr.magn,&vctr.angle,&vctr.x,&vctr.y); 
              fclose(myFile);*/   
              fprintf(myFile,"\n");           
           }
         fclose(myFile);
       
       //system("PAUSE");  
         
         
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,537
    > float polRecCon(struct vComponents vctr);
    > float recPolCon(struct vComponents vctr);
    The problem is that these functions receive a value copy of your struct.
    So any changes you make are local to the function itself, and these changes are NOT reflected in the copy you have in main (which save will ultimately use).

    The typical approach is to pass a pointer to your data, if you intend to make permanent changes to that data.

    float polRecCon(struct vComponents *vctr);
    float recPolCon(struct vComponents *vctr);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File handing and saving records to file problem
    By loveable in forum C Programming
    Replies: 18
    Last Post: 12-29-2012, 09:53 AM
  2. Saving a file
    By Isaiah in forum Game Programming
    Replies: 2
    Last Post: 02-09-2012, 03:04 PM
  3. Saving File
    By peckitt99 in forum C++ Programming
    Replies: 11
    Last Post: 09-05-2006, 10:08 AM
  4. Saving to a .txt file
    By adr in forum C++ Programming
    Replies: 1
    Last Post: 04-23-2006, 02:14 AM
  5. Saving a File
    By Krush in forum C Programming
    Replies: 1
    Last Post: 11-23-2002, 02:08 AM