# Thread: Making an Algorithm repeat itself

1. ## 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

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. 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.

3. 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

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.

4. 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.

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. Ok, then use a scanf() or getch() to scan in the junk character or something.

7. 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

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.

8. Originally Posted by mvd1212
scanf("%c", %choice);
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 );```

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.

9. ## Oops.

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.

10. 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. 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.

12. Problem solved.

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

filker0... I love you.

13. Originally Posted by mvd1212
filker0... I love you.
Ahh... That's awkward... I'm already married. Glad to be of service, though.

14. 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.