Thread: Returning from function - switch selects default

  1. #16
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    and if u dont use fflush(stdin),
    just go to your analyse_data function, and make this correction;
    Code:
    flin[strlen(flin)-1]='\0'; // delete \n
       if((fpin = fopen(flin, "rb"))== NULL)
       {
          puts(source_file_error);
          getch();    
          return 0;
       }

    just change that getch() to getchar() and it will work fine without the fflush .

    again i have no clue exactly what getch() is all about,but i do know getchar().

  2. #17
    1ST » R. vd Kooij
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    154
    Quote Originally Posted by cwr
    Yeah I read it just after I posted.
    And I had seen the page with the right way to flush the input before (this one) Though that's when I got stuck... Can't get any of those to work.

  3. #18
    1ST » R. vd Kooij
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    154
    Quote Originally Posted by qqqqxxxx
    again i have no clue exactly what getch() is all about,but i do know getchar().
    I've learned to use getch() as an easy way to create a "Press any key to continue" function.

  4. #19
    1ST » R. vd Kooij
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    154
    Hmmm with getchar() it works indeed.... could anyone explain that for me please? I have no idea why...

    Edit: ignore the above. getchar() doesn't wait for an input. I guess I'll have to search for another "Any key to continue" function. getch() seems to have been the problem
    Last edited by rkooij; 03-08-2006 at 05:28 AM.

  5. #20
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    getchar waits for an input,it also waits until the user hits the enter key,
    in your case the user input the file name and then hit an enter key,so getchar got the value of '\n'.

    if u use the same program and try using someother key except the enterkey the screen will wait till u hit an enterkey.


    but getch doesnt wait for an enterkey.

  6. #21
    1ST » R. vd Kooij
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    154
    OK, here's my solution:

    Code:
       if((fpin = fopen(flin, "rb"))== NULL)
       {
          puts(source_file_error);
          
          while ((ch = getchar()) != '\n' && ch != EOF);
          if (fgets(buf, sizeof(buf), stdin));
          return 0;
       }
    Thank you so much for helping me in the right direction. Hope it's a watertight solution this time, if not I'd appreciate someone letting me know.

    Thanks!

  7. #22
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    getch() gets a character from the keyboard and not from the input stream.

    so when the user hit a filename ,because u used scanf,the program was waiting for an enter key to be pressed,but that enterkey was not included in the scanf string,so an extra enter character was left in the input stream,if u used getchar() ,getchar takes this extra character as it reads input from the input stream,,but getch() doesnt read input from the stream,so it waited for the user to press a key,after that there was fgets which again takes input from the stream and in the stream there was already a \n character so it went to the defautl case.

  8. #23
    1ST » R. vd Kooij
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    154
    Quote Originally Posted by qqqqxxxx
    getch() gets a character from the keyboard and not from the input stream.

    so when the user hit a filename ,because u used scanf,the program was waiting for an enter key to be pressed,but that enterkey was not included in the scanf string,so an extra enter character was left in the input stream,if u used getchar() ,getchar takes this extra character as it reads input from the input stream,,but getch() doesnt read input from the stream,so it waited for the user to press a key,after that there was fgets which again takes input from the stream and in the stream there was already a \n character so it went to the defautl case.
    Thanks, that's a very clear explanation! Makes me understand the differences between the various 'read input' functions better. :thumbsup:

  9. #24
    ex-DECcie
    Join Date
    Dec 2005
    Posts
    125
    Quote Originally Posted by rkooij
    Yeah, I have been thinking about removing the default case, though that would mean I didn't learn anything, just how to avoid trouble in a lame way

    Well, you've got that right

    Among other things, I think you need to look at some of your error checking, or really lack there of. You call fopen in your analyze_data function, but you never check to see if the file SUCCESSFULLY opened. Now, that might not be a problem in this particular instance, but somewhere down the road, in another program, it could be, and you'll be chasing your tail trying to find the problem.

    You return an int from analyze_data, but you never check the return value. Of course, you're only returning 0 anyway, so you might want to consider making that function return void.

    As something to try, try putting some printf statements before the call to analyze_data and after, and see what happens when you return from analyze data....

    I have not taken the time to dig into your code (I'm at work), but start with simple thing -- are you really returning from analyze_data ok? and go from there.
    Mr. Blonde: You ever listen to K-Billy's "Super Sounds of the Seventies" weekend? It's my personal favorite.

  10. #25
    1ST » R. vd Kooij
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    154
    Thanks for warning me. I must admit I haven't paid any attention to error checking yet. I've seen some good examples now, how to use return in error checking. I'll have a look!
    http://www.f1rstracing.nl/
    OS: Windows XP
    Compiler: Dev-C++

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