1

Im trying do-while condition and there's another while inside of it, im having a problem in "do you want to continue?", its skipping it.. Is there anything wrong to my codes?

do {
    printf("\nEnter the start value:");
    scanf("%d", &start_value);
    printf("\nEnter the end value:");
    scanf("%d", &end value);
    printf("\nEnter the interval value:");
    scanf("%d", &interval_value);
    while (start_value <= end_value) {
        printf("%d ", start_value);
        start_value = start_value + interval_value;
    }
    printf("\nDo you want to continue?");
    scanf("%c", &answer);
} while (answer != 'N' || answer != 'n');
Holow
  • 31
  • 1
  • 7

1 Answers1

3

Your conditional statement

while(answer != 'N' || answer != 'n');

is always true. I suggest

while(answer != 'N' && answer != 'n');

(in addition to the first comment above from @SouravGhosh, which cleans off the newline left in the input buffer)

Weather Vane
  • 33,872
  • 7
  • 36
  • 56
  • 1
    I would suggest also `if (scanf(" %c", &answer) != 1) break;` – chqrlie Aug 19 '16 at 17:15
  • My main problem is that, my "do you want to continue?" has been skipped, any suggestion what is my error? – Holow Aug 20 '16 at 01:53
  • @Holow the solution is in the first comment right at the top, which I mentioned in my answer. If that is unclear, [this answer](http://stackoverflow.com/questions/39043617/something-is-wrong-with-the-scanfs-and-or-ifs-in-my-program-coffee-shop/39043835#39043835) suggests it too. Also, please have a good read of the man page for the `scanf` function. – Weather Vane Aug 20 '16 at 16:58