crazy output

This is a discussion on crazy output within the C Programming forums, part of the General Programming Boards category; Please look at my code: #include <stdio.h> #include <stdlib.h> int get_menu_choice (void); int main () { int choice; choice = ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    5

    Question crazy output

    Please look at my code:

    #include <stdio.h>
    #include <stdlib.h>

    int get_menu_choice (void);

    int main ()
    {
    int choice;
    choice = get_menu_choice();
    printf("you chose menu option %d\n", choice);

    return 0;
    }

    int get_menu_choice (void)
    {
    unsigned int selection;


    do
    {
    printf("\n");
    printf("\n1 - add record");
    printf("\n2 - change record");
    printf("\n3 - Delete a record");
    printf("\n4 - Quit");
    printf("\n");
    printf("\nEnter a selection: ");

    scanf("%u", &selection);
    }
    while (selection < 1 || selection > 4);

    return selection ;
    }


    when a char is entered as a menu choice i get crazy output. Is there a simple way that i can stop this from happening?
    I've tried adding if statements, etc but nothing has seemed to work.

    Can anyone please help me thanku.

  2. #2
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    You need to put parenthesis' around your compound conditionals, is my guess, since I think || supercedes <
    hasafraggin shizigishin oppashigger...

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    5
    Adding parenthesis' doesnt work.

    Any other ideas?

    How can i check the return value of scanf?

  4. #4
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    hmm... i'm guessing it's your usage of scanf then... give me an example of your input and output...
    hasafraggin shizigishin oppashigger...

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    10

    that will work i think

    i think
    %d in place of %u will work perfectly in the following statement.
    scanf("%u", &selection);

  6. #6
    Unregistered
    Guest
    Just a guess, but I noticed that you scan an unsigned int and then it gets coerced into an int. Try keeping it an int all the way through.

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    5
    I've made everytinh int all the way through but it doesnt work...

    input


    1 - add record
    2 - change record
    3 - Delete a record
    4 - Quit

    Enter a selection: y

    OUPUT:

    Enter a selection:

    1 - add record
    2 - change record
    3 - Delete a record
    4 - Quit

    Enter a selection:

    1 - add record
    2 - change record
    3 - Delete a record
    4 - Quit

    Enter a selection:

    1 - add record
    2 - change record
    3 - Delete a record
    4 - Quit

    Enter a selection:

    1 - add record
    2 - change record
    3 - Delete a record
    4 - Quit
    .
    .
    .
    .

    This happens until i hit control X.

    Thats the crazy output. I'm thinking i have to check the return status of scanf?

  8. #8
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    ah i see, i think you need to clear out your input buffer when doing this at each iteration so it waits for new input instead of running on the old. maybe there's a better way of doing this... perhaps using another method to handle the input...
    hasafraggin shizigishin oppashigger...

  9. #9
    Registered User
    Join Date
    Apr 2002
    Posts
    5
    how do i do that?

  10. #10
    Unregistered
    Guest
    In the do-while loop of the function, maybe print selection after you scan it, just to see what it is.

  11. #11
    Registered User
    Join Date
    Apr 2002
    Posts
    5
    I added a print statement but it shows the same value as the final print statement in main.

    I also added that check = scanf...

    i made it so that if check != "%d" exit.
    I dont know why this work but their is no crazy output but the do while loop doesnt go around again after this:


    I get this output instead:

    1 - add record
    2 - change record
    3 - Delete a record
    4 - Quit

    Enter a selection: u
    3344you chose menu option 3344


    No crazy out put. Is the "%d" allowed?

    here is what i did...

    #include <stdio.h>
    #include <stdlib.h>

    int get_menu_choice (void);

    int main ()
    {
    int choice;
    choice = get_menu_choice();
    printf("you chose menu option %d\n", choice);

    return 0;
    }

    int get_menu_choice (void)
    {
    int selection, check;


    do
    {
    printf("\n");
    printf("\n1 - add record");
    printf("\n2 - change record");
    printf("\n3 - Delete a record");
    printf("\n4 - Quit");
    printf("\n");
    printf("\nEnter a selection: ");

    check = scanf("%d", &selection);

    printf("%d", selection);
    return selection ;
    }
    while ((check != "%d")||(selection < 1) || (selection > 4));

    }

    The problem isnt yet fixed.It works on one compiler but not another

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Try this:
    Code:
    #include <stdio.h> 
    
    int get_menu_choice (void); 
    
    int main () 
    { 
      int choice; 
      choice = get_menu_choice(); 
      printf("you chose menu option %d\n", choice); 
      return 0; 
    } 
    
    int get_menu_choice (void) 
    { 
      int selection; 
      do 
      { 
        printf("\n"); 
        printf("\n1 - add record"); 
        printf("\n2 - change record"); 
        printf("\n3 - Delete a record"); 
        printf("\n4 - Quit"); 
        printf("\n");
        printf("\nEnter a selection: "); 
        scanf("%d", &selection);
        while ( getchar() != '\n' ); /* Clear the input buffer */
      } 
      while (selection < 1 || 4 < selection );
      return selection; 
    }
    -Prelude
    My best code is written with the delete key.

  13. #13
    Unregistered
    Guest
    Prelude, you've added code to ignore the newline. I don't understand why the newline would cause an infinite loop?

  14. #14
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I don't understand why the newline would cause an infinite loop?
    The program uses scanf to read an integer, if the user enters an integer then all is well. If the user enters a character then scanf fails, and because of the newline in the buffer, scanf can't read anything valid and will continually fail. Thus the infinite loop. I cleared the input buffer because if it is empty the program will wait for user input instead of attempting to read what is there and failing if it is invalid.

    -Prelude
    My best code is written with the delete key.

  15. #15
    Unregistered
    Guest
    Prelude, I've always been pretty confused about buffers. From your reply, I gather that characters only leave the buffer when scanf is successful, otherwise they just stay there?
    I've noticed that you are one of the main moderators here and you seem to really know your stuff. Do you do this while you're programming?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 03:08 PM
  2. execl()/fork() output
    By tadams in forum C Programming
    Replies: 19
    Last Post: 02-04-2009, 03:29 PM
  3. Digital Logic + trees=getting crazy! :)
    By smoking81 in forum C Programming
    Replies: 8
    Last Post: 09-11-2008, 08:49 AM
  4. Replies: 4
    Last Post: 11-30-2005, 04:44 PM
  5. Formatting output into even columns?
    By Uncle Rico in forum C Programming
    Replies: 2
    Last Post: 08-16-2005, 06:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21