Thread: Making an Algorithm repeat itself

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    5

    Making an Algorithm repeat itself

    I'm trying to get a C program to repeat itself when the user selects from an option at the end of the algorithm. This algorithm uses functions for most of the main body of it.

    example:

    code starts:

    greeting
    calculation
    answer

    Would you like to find another answer (y or n)?

    code ends:

    I've been trying to use a while loop of "while (choice == 'y')" but when I put it before the actual calculation it simply skips over the while to the end of the program. The problem is I don't know how to get the program to repeat itself when the option is at the end.

    Any help anyone could provide would be appreciated.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You need to flush the input buffer before you prompt for the repeat or not. Or as I like to say, turn off the faucet.


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

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    5
    I don't really understand what that is supposed to accomplish. I'm trying to get the entire program to repeat if the user selects 'y'. Here's what I've been trying to do.

    (not actual code)
    Code:
    while (choice == 'y')
    {
    funct(1) Calculation
    funct(2) Answer
    funct(3) print answer
    
    printf("Would you like to find another answer (y or n)? ");
    scanf("%c", &choice);
    }
    printf("Goodbye.");
    return 0;
    I'm not sure what this flushing of the input buffer does or actually how to do it. I was actually hoping for a simpler way to do it.
    Last edited by mvd1212; 12-02-2005 at 04:58 PM.

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    No... here is an idea. Maybe it's best that you click on the link provided before saying you don't get it.

    You flush the input buffer because when you try to scan your character for the repeat statement at the end, the program will take what's already in the input buffer (usually a newline character). You need to clear the buffer before you prompt for that input otherwise it will appear to have skipped over it.
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    5
    Of course that was the first thing I did. But I don't really understand what that page is saying. I've just started programming in C and my instructor hasn't taught us about flushing the input so I'm pretty sure we aren't supposed to use that method yet.

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Ok, then use a scanf() or getch() to scan in the junk character or something.
    Sent from my iPadŽ

  7. #7
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    Of course, it will help if choice is set to 'y' before you reach the loop. Another way to do this will be to use the do {} while () construct, as in
    Code:
    do
    {
        funct(1) Calculation
        funct(2) Answer
        funct(3) print answer
    
        printf("Would you like to find another answer (y or n)? ");
        scanf("%c", choice);
    } while (choice == 'y');
    printf("Goodbye.");
    return 0;
    Of course, this doesn't fix the fact that scanf() doesn't consume all of the input from the console, but it's actually what you were asking rather than the answer to a question that you'll have once you solve the problem you're having now.
    Insert obnoxious but pithy remark here

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by mvd1212
    scanf("%c", %choice);
    Quote Originally Posted by filker0
    scanf("%c", choice);
    Boy you guys are having a heckuva time with that one today...
    Code:
    char choice;
    ...
    scanf("%c", &choice );


    [edit]
    LMAO. I forgot the 'choice' in my smart ass reply and had to edit it in. But hey, I got the colored &. But in my defense, I have numerous alcoholic beverages as witnesses.
    [/edit]

    Quzah.
    Last edited by quzah; 12-02-2005 at 04:50 PM.
    Hope is the first step on the road to disappointment.

  9. #9
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235

    Oops.

    Quote Originally Posted by quzah
    Boy you guys are having a heckuva time with that one today...
    Code:
    char choice;
    ...
    scanf("%c", &choice );
    Oops... I cut/pasted his code sample, then when I saw the %, I went in and edited it out. Yes, you need the & for a %c. I had considered suggesting that he do a %s, make choice an array, and use
    Code:
    while (choice[0] == 'y');
    Typing as someone who's been coding in C since 1981 and has written more than one printf() implementation, I hang my head in shame at my omission.
    Insert obnoxious but pithy remark here

  10. #10
    Registered User
    Join Date
    Dec 2005
    Posts
    5
    hehe... Yeah I didn't make that same mistake in the code.

    I've tried to set choice = 'y' at the beginning of the code but it doesn't seem to do anything. I keep getting the problem that it'll print the message "Would you like to find another answer (y or n)? " and then it exits the code without waiting for input from the user.

    I'm sorry if I'm making this difficult, I'm pretty new at this and if the option came at the beginning of the code then I could do it easily.

    My instructor has just taught us about arrays and while she did say you could probably use them in this project, don't use them.

    I thought there was some simple method to get the code to loop back that I was missing. Is it more complex then I imagined?

    I'm not allowed to flush the input or use arrays. Not that I would know how to do that anyway.

  11. #11
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    Hmmm... I suppose you could read "choice" in a loop until you got either "y" or "n", as in
    Code:
    choice = '\0';
    while (choice != 'y' && choice != 'n')
    {
        scanf("%c", &choice);
    }
    Eventually, it will find a 'y' or a 'n'. But that, of course, isn't really what you want. You could change your format string to "%c\n", as in
    Code:
    scanf("%c\n", &choice);
    which will discard a trailing newline.

    You may want to check your scanf() calls within the functions... If you're not consuming the newlines at the end of the input there, it explains why you still have them in the input stream when you get to the end of your loop.
    Last edited by filker0; 12-02-2005 at 05:15 PM. Reason: Add an aside
    Insert obnoxious but pithy remark here

  12. #12
    Registered User
    Join Date
    Dec 2005
    Posts
    5
    Problem solved.

    The addition of \n to "scanf("\n%c", &choice)" completely fixed the problem.


    filker0... I love you.

  13. #13
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    Quote Originally Posted by mvd1212
    filker0... I love you.
    Ahh... That's awkward... I'm already married. Glad to be of service, though.
    Insert obnoxious but pithy remark here

  14. #14
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by mvd1212
    Problem solved.

    The addition of \n to "scanf("\n%c", &choice)" completely fixed the problem.


    filker0... I love you.

    Course just adding a getch() or scanf() to junk the newline like I've been saying all the time would have worked too, but whatever. I'm used to being stepped on.
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  2. Database Search Algorithm
    By Krupux in forum C Programming
    Replies: 1
    Last Post: 08-28-2003, 09:57 PM
  3. Help with an algorithm
    By PJYelton in forum C++ Programming
    Replies: 15
    Last Post: 06-11-2003, 10:25 AM
  4. Replies: 2
    Last Post: 01-13-2003, 01:28 PM