Thread: Returning from function - switch selects default

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    1ST » R. vd Kooij
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    154

    Returning from function - switch selects default

    Hi guys,

    I've been searching this forum and the FAQ for a solution for this problem but I can't find anything that works. Either I'm not applying it in the right way, or the solution is simply not on the forum yet.
    Hope you can help me out. I call a filecopy function by using a switch menu. Now when Im finished copying the file it returns to the switch menu. Problem is that when it comes back to the switch menu, it automatically activates the default option (which I defined as closing the application). Now after reading here and there I think the input buffer is not yet empty when I get back to the menu, though I have no idea how to correct this. I've tried fflush but that didn't change anything. Also I've tried using fgets() instead of scanf() in my file copy function though then my program isn't able to open the filename I enter.

    Hope that's clear enough, here are the code snippets:

    The file copy function:
    Code:
    int analyse_data(void)
    {
       FILE *fpin, *fpout;
       char flin[256],flout[256];
       int ih;
       
       clrscr();
       puts(program_title);    
       puts(break_line);
       
       puts(source_file);
    //   fgets(flin, sizeof(flin), stdin);
       scanf("%s",flin);   
    //   flin[strlen(flin)-1]='\0'; // delete \n
       if((fpin = fopen(flin, "rb"))== NULL)
       {
          puts(source_file_error);
          return 0;
       }
       
       puts(destination_file);
       scanf("%s",flout);
    //   fgets(flout, sizeof(flout), stdin);
       fpout=fopen(flout,"wb");
       while((ih=fgetc(fpin))!=EOF)
       {
          fputc(ih,fpout);
      	  putch(ih);
       }
       
       fclose(fpin);
       fclose(fpout);
    
       return 0;
    }
    The menu in main:
    Code:
        switch(menu_switch)
        {
            case 1:
                 exit_flag=FALSE;  
                 test_procedure();
                 getch();
                 clrscr();
            break;
            
            case 2:
                 exit_flag=FALSE;
    //             puts("You chose 2");
                 analyse_data();
                 getch();
                 clrscr();
            break;
            
            case 3:
                 exit_flag=FALSE;
                 puts("You chose 3");
                 getch();             
                 clrscr();
            break;
            
            case 4:
                 exit_flag=TRUE;
            break;
            
            default: exit_flag=TRUE;
        }
    }
    while(exit_flag==FALSE);
    You'll probably be able to see from the code that I'm not an expert programmer yet. Off topic tips are always welcome

    Thanks a lot in advance, René
    Last edited by rkooij; 03-08-2006 at 03:41 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  2. Recursion: base case returning 1, function returning 0
    By yougene in forum C Programming
    Replies: 5
    Last Post: 09-07-2007, 05:38 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Replies: 5
    Last Post: 02-08-2003, 07:42 PM