Thread: saving contents of a function

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    13

    Unhappy saving contents of a function

    This might sound complicated but PLEASE bear with me!
    What I am doing is giving the user the choice to save a file in a number of ways...

    if (Select==1)
    BasicSaveStructure(), SaveNormal();
    else if (Select==2)
    BasicSaveStructure(), Save90();
    else if (Select==3)
    BasicSaveStructure(), Save180();
    else if (Select==4)
    BasicSaveStructure(), Save270();
    else if (Select==5)
    BasicSaveStructure(), SaveInvert();
    else if (Select==6)
    BasicSaveStructure(), SaveInvert90();
    else if (Select==7)
    BasicSaveStructure(), SaveInvert180();
    else if (Select==8)
    BasicSaveStructure(), SaveInvert270();
    else if (Select==9)
    main();

    ie. what the user selects then it will go to that particular section. All options to go the first section which deals with the basic of saving a file...

    printf("Please enter the name of the file to save your image: ");
    gets(Filename);
    OutputFile=fopen(Filename, "w");
    if (OutputFile==NULL)
    {
    printf("Cannot save file of that name\n");
    printf("The program will now end\n");
    GetInteger();
    exit(1);
    }
    printf("\nImage file has been successfully saved\n");
    printf("Press any key to return to the main menu\n");
    GetInteger();

    after that i need to go to the corresponding function to save the file in the way the user wants it. What I don't know is how to run the contents of a function and save it at the same time. I have included one of the functions that I am using in my program...

    void Rotate90(void)
    {
    clrscr();

    for (i=0;i<ROWS;i++)
    for (j=0;j<COLS;j++)
    Picture2[COLS-1-j][i]=Picture[i][j];
    }

    but i don't know how to call that function and perform the save section at the same time.

    ***THANK YOU SO MUCH FOR ANY HELP ON THIS SUBJECT***

  2. #2
    Unregistered
    Guest
    post once and wait for an answer, don't post a second time if we're not fast enough for you. it's very rude.

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    13
    i posted the first one but it didn't appear and i thought i had posted it to the wrong forum or something so i posted it again, hence the closeness of the two posting times

    )

  4. #4
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,227
    if (Select==1)
    BasicSaveStructure(), SaveNormal();
    else if (Select==2)
    BasicSaveStructure(), Save90();
    else if (Select==3)
    BasicSaveStructure(), Save180();
    else if (Select==4)
    BasicSaveStructure(), Save270();
    else if (Select==5)
    BasicSaveStructure(), SaveInvert();
    else if (Select==6)
    BasicSaveStructure(), SaveInvert90();
    else if (Select==7)
    BasicSaveStructure(), SaveInvert180();
    else if (Select==8)
    BasicSaveStructure(), SaveInvert270();

    that's horrendously wrong. First off, it'd be much cleaner in a switch-case statement, and second of all you can't just seperate two function calls by a comma and call them one line. so anyhow if you were just gonna use this large if statement here (which is slower than switch-case) you'd need to re-write it as

    if(select==1){
    BasicSaveStructure();
    SaveNormal();
    }
    else if(...){
    }
    else if(...){
    }
    .....etc.


    And to have two functions running at the same time, look into multithreading.

    And anyhow, why do what you're asking? can't you just write a function like this:

    Code:
    void Save90(void) 
    { 
         clrscr(); 
    
         for (i=0;i<ROWS;i++) 
              for (j=0;j<COLS;j++) 
                   Picture2[COLS-1-j][i]=Picture[i][j]; 
         Save(Picture2);
    }

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    194
    You should simply call the desired rotating or inverting function THEN save the data

  6. #6
    The Artful Lurker Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633

    Re: saving contents of a function

    Originally posted by speve
    if (Select==1)
    BasicSaveStructure(), SaveNormal();
    else if (Select==2)
    BasicSaveStructure(), Save90();
    else if (Select==3)
    BasicSaveStructure(), Save180();
    I have two suggestions that I hope will be useful to you.

    The first is to not have so many functions that do the same (or very similar) things. With your BasicSaveStructure() preceeding the "rotation save" function it is clear that every save has some things in common, and you also want to be able to save at different rotations. Will something like this work for you?

    Code:
    void SaveFile( short rotation )
    {
      /* Do your 'basic save' tasks here */
    
      /* Do you rotation specific tasks here */
    }
    A rotation value of zero is 'normal'. A negative value for rotation indicates it is inverted (-90 is Invert90).

    My second suggestion is to replace your large if .. else if block with a switch statement:

    Code:
    short rotation;
    
    switch (Select)
    {
      case (1):
        rotation = 0;
        break;
      case (2):
        rotation = 90;
        break;
      case (3):
        rotation = 180;
        break;
      case (4):
        rotation = 270;
        break;
      case (5):
        rotation = -360; /* perhaps you have a better value for this */
        break;
      case (6):
        rotation = -90;
        break;
      case (7):
        rotation = -180;
        break;
      case (8):
        rotation = -270;
        break;  /* not needed, but a nice coding habit to get into */
    }
    
    SaveFile( rotation );
    You have now accomplished your basic and rotation specific tasks with a single function call. If I have misunderstood your question, feel free to point me in the right direction
    Last edited by Deckard; 01-05-2002 at 08:45 PM.
    Jason Deckard

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Post your games here...
    By Hammer in forum Game Programming
    Replies: 132
    Last Post: 02-28-2013, 09:29 AM
  2. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  3. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Replies: 4
    Last Post: 11-23-2003, 07:15 AM