Thread: Can someone post an example please?

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    3

    Can someone post an example please?

    I am using a switch statement for 'Y' or 'N' and what I'm trying to do is have my program start over from the beginning after the user selects 'Y' he/she wants to try again. I tried looking at menu programs but I just get confused. I'm new to this and really slow. An example that I could follow would be greatly appreciated or a youtube video that shows one.



    Thanks in advance.

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Try this...

    Code:
    #include <stdio.h>
    
    void flush( void )
    {
         int ch;
         while((ch=getchar()) != EOF && ch != '\n' );
    }
    int main()
    {
        int ch=0;
        
        do
        {
            /* inside your code here */
            printf(" Y/N\n?");
            ch = getchar(); 
            flush();
        } while( ch == 'y' || ch == 'Y' );
        
        return 0;
    }
    I've been really good to post this code. Which i dont normally do.

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Code:
    int main(void)
    {
        char choice;
        int dummy;
        do
        {
            // Your program
    
            scanf("%c", &choice);
            while ((c = getchar()) != '\n') ;
        } while(choice == 'Y' || choice == 'y');
    
        return 0;
    }
    should do it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Post your games here...
    By Hammer in forum Game Programming
    Replies: 132
    Last Post: 02-28-2013, 09:29 AM
  2. Can't edit an old post?
    By Lesshardtofind in forum General Discussions
    Replies: 5
    Last Post: 09-19-2010, 08:16 AM
  3. Post your Best Text Adventure
    By Joe100 in forum Game Programming
    Replies: 3
    Last Post: 08-15-2003, 05:47 PM
  4. Auto POST
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 06-07-2003, 10:42 AM