Can someone post an example please?

This is a discussion on Can someone post an example please? within the C Programming forums, part of the General Programming Boards category; I am using a switch statement for 'Y' or 'N' and what I'm trying to do is have my program ...

  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,682
    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
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,657
    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, 09:16 AM
  3. Post your Best Text Adventure
    By Joe100 in forum Game Programming
    Replies: 3
    Last Post: 08-15-2003, 06:47 PM
  4. Auto POST
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 06-07-2003, 11:42 AM

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