Thread: Problem with loop to end program.

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    13

    Problem with loop to end program.

    I tried creating a loop that would put a menu on the screen, let the user select something, and put the menu after the whateveryoucallthem runs, and keep doing this until the user selects to quit, at which point the loop should end and then the program. However, every time I don't select to quit the program, the loop printf's the menu twice before it prompts me to enter something. I haven't been able to find anything online that explains why this is happening or how to prevent it, and while experimenting I've made the thing a complete, pathetic mess without actually fixing the problem. For some reason while blindly experimenting I also added a simple "put in your number: _ This is your number: _" which causes the program to crash for some other unknown reason. I don't know what to do or what I should have done, and I'm sick of looking at it. Please help me.
    Code:
    #define _CRT_SECURE_NO_DEPRECATE
    #include <stdio.h>
    #include <string.h>
    #define SIZE 20
    
    typedef struct{
     char string1[SIZE];
     int int1;
     int int2;
     double double1;
     double double2;
    }abc;
    
    int menu(int *checkquit);
    
    int main()
    {
    	int testingvalue=1;
    	printf("Hello. This program is for BLANK\n");
    	do {
    		menu(&testingvalue);
    	} while(testingvalue>0);
    	printf("Goodbye.\n");
    }
    
    int menu(int *checkquit)
    {
    	int numberthingy=0;
    	char menuselection;
    	printf("A-Add an entry\nD-Delete an entry\nP-Print the entire catalog\n");
    	printf("S-Save the current catolog\nC-Clear the current catolog\nQ-Quit the program\nSelect an item from the menu");
    	scanf("%s", &menuselection);
    	if (menuselection=='q')
    	{
    		printf("Youquit");
    		*checkquit=0;
    	}
    	else if (menuselection=='Q')
    	{
    		printf("Youquitagain");
    		*checkquit=0;
    	}
    	else
    	{
    		printf("Repeating program");
    		*checkquit=1;
    	}
    	printf("\nEnter a number:\n");
    	scanf("%d", &numberthingy);
    	printf("This is your number: %d\n", numberthingy);
    	return 0;
    }
    The reason for the stuff that never comes up is because it's an assignment for school, and everything I've made is just a small part of what I'm supposed to do.

  2. #2
    Registered User
    Join Date
    Sep 2010
    Location
    Boston, MA
    Posts
    97
    Code:
    	else
    	{
    		printf("Repeating program");
    		*checkquit=1;
    	}
    	printf("\nEnter a number:\n");
    	scanf("%d", &numberthingy);
    	printf("This is your number: %d\n", numberthingy);
    Think about those last 3 statements and if they really belong where they are currently. That placement is why your program doesnt quit immediately after a "q" is entered.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    13
    Quote Originally Posted by omGeeK View Post
    Code:
    	else
    	{
    		printf("Repeating program");
    		*checkquit=1;
    	}
    	printf("\nEnter a number:\n");
    	scanf("%d", &numberthingy);
    	printf("This is your number: %d\n", numberthingy);
    Think about those last 3 statements and if they really belong where they are currently. That placement is why your program doesnt quit immediately after a "q" is entered.
    Those last three things are useless crap I was throwing around. I wasn't sure if scanf programs caused the loop to have errors or not, and I'm none the wiser for it. I don't even know why that causes the program to crash when I run it. The point is, it's useless crap and I still don't know why when the loop runs the Menu thing again it prints the entire menu twice before it asks me for input.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    This is one of C's little "gotcha" dealies... scanf leaves the newline (when you press Enter) in the input queue and the next time around it sees it and thinks you pressed enter on a blank line...

    Try it like this...
    Code:
    int numberthingy=0;
    char menuselection;
    printf("A-Add an entry\nD-Delete an entry\nP-Print the entire catalog\n");
    printf("S-Save the current catolog\nC-Clear the current catolog\nQ-Quit the program\nSelect an item from the menu");
    
    getchar();
    
    scanf("%c", &menuselection);  <-- note %c not %s for characters
       if (menuselection=='q')
    You may need to add getchar() before each scanf that reads a string to get proper operation.

  5. #5
    Registered User
    Join Date
    Sep 2010
    Location
    Boston, MA
    Posts
    97
    Quote Originally Posted by inverse9 View Post
    Those last three things are useless crap I was throwing around. I wasn't sure if scanf programs caused the loop to have errors or not, and I'm none the wiser for it. I don't even know why that causes the program to crash when I run it. The point is, it's useless crap and I still don't know why when the loop runs the Menu thing again it prints the entire menu twice before it asks me for input.
    Well they are the right code for scanning in a integer just need to be put in the else statement. And while dealing with a looping menu like that, You will want to check out the "fflush" function, which clears the buffer of your keyboard, if you pass stdin. So putting in
    Code:
    fflush(stdin);
    before each time you scan a character is a safe way of making sure no accidental characters are read.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by omGeeK View Post
    You will want to check out the "fflush" function, which clears the buffer of your keyboard, if you pass stdin.
    BZZZZT... wrong! FFlush() flushes an output stream... i.e. a disk buffer. It does not flush stdin.

    Cprogramming.com FAQ > Why fflush(stdin) is wrong

  7. #7
    Registered User
    Join Date
    Sep 2010
    Location
    Boston, MA
    Posts
    97
    Haha well I never knew that and to think my professor lived by flushing the buffer as he said "When in doubt, flush the buffer..." it would work in this situation though I'd assume.

    Code:
    /*
     *  This C implementation will clear the input buffer.
     *  The chances are that the buffer will already be empty,
     *  so the program will wait until you press [Enter].
     */
    
    #include <stdio.h> 
    
    int main(void)
    {
      int   ch;
      char  buf[BUFSIZ];
      
      puts("Flushing input");
      
      while ((ch = getchar()) != '\n' && ch != EOF);
      
      printf ("Enter some text: ");
      
      if (fgets(buf, sizeof(buf), stdin))
      {
        printf ("You entered: %s", buf);
      }
      
      return 0;
    }
    Heres the actual code for flushing the input buffer.
    Last edited by omGeeK; 04-25-2011 at 07:38 PM.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by omGeeK View Post
    Haha well I never knew that and to think my professor lived by flushing the buffer as he said "When in doubt, flush the buffer..." it would work in this situation though I'd assume.
    He probably uses a Microsoft compiler then. (Assuming he was actually talking about the input stream.) Flushing input streams with fflush is non-standard.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Sep 2010
    Location
    Boston, MA
    Posts
    97
    Yea we are forced to use VS...

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by omGeeK View Post
    Yea we are forced to use VS...
    My condolences to you and your classmates.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by CommonTater View Post
    My condolences to you and your classmates.
    It could be worse. They could be the other 50% of the people posting here, using Turbo C!


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    It could be worse. They could be the other 50% of the people posting here, using Turbo C!
    True enough.

    They should be given lifetime memberships in the Society for Creative Anachronism...

  13. #13
    Registered User
    Join Date
    Mar 2011
    Posts
    13
    Code:
    fflush(stdin);
    That works, and it's simple. I want to go through and figure out how that big code block works, but I'm so fed up with this I can't work on my program at all. My assignment is due in 38 hours but I'm so frustrated at these obscure errors (that I can't ever find answers to without asking for help) popping up out of nowhere in absolutely everything I've ever tried to to program that I just cant do anything right now.

    Sorry. Thank you for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 11-22-2006, 12:20 PM
  2. Need help with a loop program
    By hieugene in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2006, 02:42 AM
  3. how: loop program..
    By pico in forum C++ Programming
    Replies: 12
    Last Post: 03-09-2004, 01:52 AM
  4. Need Help on Program Using For Loop
    By Unregistered in forum C++ Programming
    Replies: 10
    Last Post: 02-26-2002, 06:54 PM
  5. loop problem in program
    By Wjahvces in forum C++ Programming
    Replies: 2
    Last Post: 11-25-2001, 04:01 AM