-4

At the end of my program I want to ask the user if he wants to exit or not.

PROBLEM: I want to create a loop, that would send the user to beginning (Point A) if he enters n or N, or to the end of my program (Point C) if he enters y or Y, or to the middle of the program (Point B) if he enters anything else.

How do I achieve this?

#include <stdio.h> 
int main()
{
  char exit=n;
  A POINT
  // imagine here some stuff
  // some more stuff
  // just a bit more junk
  // some other C statements
  B POINT
  printf("wana exit? press y/n);
  scanf("%c", &exit);
  C POINT
  return 0;
}
pmr
  • 58,701
  • 10
  • 113
  • 156
BeucaN
  • 3
  • 4
  • 2
    This is not your actual code as it would not even compile. Also, `scanf` is inherently unsafe. – Ed S. Jan 23 '14 at 22:34
  • Yeah im sorry I didnt know I had to type actual code my bad. Also why is everyone telling me its not safe? what should I do ? why isnt it safe? – BeucaN Jan 23 '14 at 22:36
  • @BeucaN Are "they" referring to `goto`? – Njol Jan 23 '14 at 22:38
  • @Njol Pretty sure that theyre refering to scanf, dont know goto function im sorry :/ – BeucaN Jan 23 '14 at 22:40
  • `scanf` is unsafe because it does not perform any sort of buffer size check, i.e., buffer overruns are a serious possibility. To get a single char, just use `getchar`. For strings use `fgets`. – Ed S. Jan 23 '14 at 22:46
  • @EdS. I'm having a hard time figuring out if you're trolling this person or if you missed the point of the question. – paddy Jan 23 '14 at 22:48
  • @paddy: Well, certainly not trolling, but I did mentally skip... that entire second paragraph. My fault. – Ed S. Jan 23 '14 at 23:00
  • Okay even more problems to coveer for me I guess. Overruns scanf buffer whats that mean? getchar? isnt it scanf("%c, &char"); also for string fgets? not %s? whats difference? – BeucaN Jan 23 '14 at 23:03

2 Answers2

0

You can wrap the whole code in main in a do { ... } while (false), and use continue; to return to the top, and break; to exit the loop, e.g.:

main() {
    do {
        // your code
        if (exit == 'n')
            continue;
    } while (false);
}

PS: You could use goto, but that's highly discouraged, so better don't even start to use it.

Njol
  • 3,271
  • 17
  • 32
  • 1
    How about this instead: `do { ... } while ( exit == 'n' );` – Ned Jan 23 '14 at 22:39
  • And as for `goto`, don't go there... Wait for a question about finite state machines, then maybe... – Ned Jan 23 '14 at 22:41
  • if you really want a forever loop, at least make it `for (;;)` or `while(1)` so that it's clear from the top that it's a forever loop – Brandin Jan 23 '14 at 22:43
  • @NedNowotny That would require to define exit outside of the loop, but I would actually use that myself. edit: that's why I used a do while actually but then I changed my mind while writing the answer ^^ – Njol Jan 23 '14 at 22:46
  • @Brandin its not a loop at all: `while (false)`. It's just a block to be able to jump to its start and end. – Njol Jan 23 '14 at 22:46
  • @Njol Hmmm... I did not pay attention to the `while (false)` when I first looked at this -- being C, that should be `FALSE`. So, congratulations, you have indeed used a `goto` without using `goto`. Now, `continue` is just syntactic sugar for a "structured" `goto` -- or, branch with or without the label. However, this code example is about as blatant a `goto` as you can achieve without actually using the keyword... – Ned Jan 23 '14 at 22:55
  • Highly discouraged. Got it. wont use it. But why is it discouraged? – BeucaN Jan 23 '14 at 23:04
  • @BeucaN If you're interested you can read the [wikipedia article on goto](https://en.wikipedia.org/wiki/Goto#Criticism_and_decline). – Njol Jan 24 '14 at 09:34
0

This is pretty basic. What you're really asking is how to control a main loop based on a valid response, and keep asking the question if the response is not valid. One way to do this is using the value as your loop condition.

do {

    // etc etc...


    // Ask question until yes or no answer
    do {
        printf("Wanna exit? Press y/n\n");
        if( 1 != scanf("%c", &exit) ) {
            printf("Input failed.  Exiting.\n");
            exit = 'y';
        }
        exit = tolower(exit);
    } while(exit != 'y' && exit != 'n');

} while(exit != 'y');

Other approaches use loop control constructs such as break and continue. There's already an answer here using those.

paddy
  • 60,864
  • 6
  • 61
  • 103