Help with loop

This is a discussion on Help with loop within the C Programming forums, part of the General Programming Boards category; Here's my code: Code: #include<stdio.h> int tix=0; main() { do { printf("\nDo you want to reserve another ticket?[1.YES 2.NO]\n"); scanf("%d", ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    24

    Help with loop

    Here's my code:

    Code:
    #include<stdio.h>
    int tix=0;
    
    main()
    {
    	do
    	{
    		
    		printf("\nDo you want to reserve another ticket?[1.YES 2.NO]\n");
    		scanf("%d", &tix);
    
    		if (tix == 2)
    		{
    			printf("out of system");
    		}
            }
    	while (tix < 1 || tix > 2);
    }
    What it is supposed to do is, when anything except 1 or 2 is entered, the loop will repeat. If 1 or 2 is entered, the program ends.

    But now:
    if you enter anything except "1" and "2", the program will keep repeating the message

    "Do you want to reserve another ticket?[1.YES 2.NO]" but it would not stop to scan the answer. Please help.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Add this:

    Code:
    int puller;
    
    puller+ = getchar();
    And put the int puller, at the top of your function, along with tix.

    Put the puller+ = getchar() line, right after your scanf() line for tix. What that does is pull
    one word off the keyboard buffer, cleaning it up.

    That should fix up the problem - which is the garbage left on the keyboard buffer.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    24
    Code:
    #include<stdio.h>
    int tix=0;
    
    main()
    {
    	do
    	{
    		
    		printf("\nDo you want to reserve another ticket?[1.YES 2.NO]\n");
    		scanf("%d", &tix);
    		int puller;
    		puller+ = getchar();
    
    
    		if (tix == 2)
    		{
    			printf("out of system");
    		}
        }
    	while (tix < 1 || tix > 2);
    }
    Is this what you mean? I tried running it. When I enter "N", it still repeats like crazy.

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    24
    I've managed to redo the logic and here is what I got

    Code:
    #include<stdio.h>
    char tix;
    
    void anotick(void);
    
    main()
    {
    	anotick();
    }
    
    
    void anotick(void)
    {
    	printf("\nDo you want to reserve another ticket?\n[1.YES 2.NO]\n");
    	scanf("%c", &tix);
    
    	switch(tix)
    	{
    		case '1': printf("1");
    		getch();
    
    		case '2': printf("2");
    		getch();
       
    		default : anotick();
    	
    	}
    }
    Problem is, whenever it is wrong, the message "Do you want another ticket?" gets displayed twice!

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    At the end of each case statement, you need a "break;". Switch statements use "fall through" logic, remember.
    Last edited by Adak; 10-13-2009 at 05:05 AM.

  6. #6
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    are u finding the code like this

    Code:
    int tix=0;
    
    main()
    {
    	do
    	{
    		
    		printf("\nDo you want to reserve another ticket?[1.YES 2.NO]\n");
    		scanf("%d", &tix);
    		int puller;
    		puller+ = getchar();
    
    
    		if (tix == 2)
    		{
    			printf("out of system");
    		}
        }
    	while (tix < 1 || tix > 2);
    }

  7. #7
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    sorry dude i pasted the wrong code

    please check this out

    Code:
    #include <stdio.h>
    
    int main() {
      int tix=0;
      do {
           printf("\nDo you want to reserve another ticket?[1.YES 2.NO]\n");
           scanf("%d", &tix);
    
           // if      (tix == 1) { he want to continue; }                                                                                                                               
           // else if (tix == 2) { he dosn't want to continue; }                                                                                                                        
    
      } while (tix != 1 || tix != 2);
    }

  8. #8
    Registered User
    Join Date
    Oct 2009
    Posts
    24
    My code works if I enter numbers. Yours does too. But when I enter an alphabet, it keeps repeating "Do you want another ticket". Tried it with yours. When I enter an alphabet, the same thing happens.

    What I want is to display the message ONCE when an alphabet is entered.

    If you dont get what I mean, execute your program, and enter "n".

  9. #9
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Code:
    File Edit Options Buffers Tools C Help                                                                                                                                              
    #include <stdio.h>
    
    #define WRONG_INPUT 0
    
    int main() {
      int tix=0;
      do {
           printf("\nDo you want to reserve another ticket?[1.YES 2.NO]\n");
           scanf("%d", &tix);
           if (tix == WRONG_INPUT) {
             printf("[Error] Input is invalid\n");
             break;
           }
           // if      (tix == 1) { he want to continue; }                                                                                                                               
           // else if (tix == 2) { he dosn't want to continue; }                                                                                                                        
    
      } while (tix != 1 || tix != 2);
    }

    check this out then

  10. #10
    Registered User
    Join Date
    Oct 2009
    Posts
    24
    Okay. It works. THANKS A MILLION RockyMarrone!

  11. #11
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    U r most Welcome Dude and Enjoy C/C++ Programming

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Too bad your solution is wrong:
    Code:
      } while (tix != 1 || tix != 2);
    What if tix is 1? Well, then it's not 2, so this loop will continue forever if you keep entering 1 or 2.
    Code:
    #include<stdio.h>
    int main( void )
    {
        int x = 0;
        do
        {
            if( x != '\n' )
            {
                printf("enter a char: ");
                fflush(stdout);
            }
            x = getchar();
        }
        while( x != '1' || x != '2' ); /* yay! loop forever! */
    
        return 0;
    }
    This will loop forever, just like your example.
    If 1 or 2 is entered, the program ends.
    Neither one of your solutions will end on 1 or 2. They'll end if scanf fails to store something. But that's not what they said they wanted. Change the OR to an AND.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Poll event loop
    By rogster001 in forum C++ Programming
    Replies: 2
    Last Post: 09-17-2009, 04:28 AM
  2. need help with a loop
    By Darkw1sh in forum C Programming
    Replies: 19
    Last Post: 09-13-2009, 09:46 PM
  3. funny-looking while loop
    By Aisthesis in forum C++ Programming
    Replies: 3
    Last Post: 08-30-2009, 11:54 PM
  4. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 08:14 AM

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