-1

i was working on an educationary project to study C++ where specific labels include code to that specific topic being studied. However, at the end of each code block i want the compiler to ask if the user wants to exit or countine from beginning:

        void main(void)
    {
        beginning:
        printf("goto : \n 1) pointers \n 2) classes \n 3) \n 4) \n 5) \n ");
        scanf_s("%d", &input);
            switch (input)
            {
            case 1:
                goto pointers;
            case 2:
                goto classes;
            case 3:
                case 4:
                    case 5:
            default:
                printf("wrong try again \n"); 
                goto beginning;
            }
    pointers:
    .....
    classes:
    ......
}

the code to check what the user wants:

    do {
        printf("1 for exit and 2 for return menu \n");
        scanf_s("%d", &reserved);
        if (reserved == 2) { goto beginning; }
        else if (reserved == 1) { exit; }
        else { printf("wrong number, put a valid one you little..!"); }
    } while (reserved != 1 && reserved != 2);

i tried to turn it to a function but the problem is labels are defined putside of main function so i want to just make the compiler put my function once it is called and not evaulate it from the beginning.

mohamed
  • 231
  • 1
  • 13
  • 1
    Don't use labels and `goto` instead of loops. That makes *spaghetti code* which is hard to impossible to understand and maintain. – Some programmer dude Feb 04 '22 at 08:43
  • 3
    you have an issue with goto because of globals variables. Hmm.. dont use global variables and don't use goto and the issue won't be there – 463035818_is_not_an_ai Feb 04 '22 at 08:44
  • 1
    And are you really learning C++? Nothing in the shown code snippets are specific for C++, it could all be plain C. C++ doesn't even have `scanf_s` (it exists in C, but the standardized function is different from the Windows MSVC implementation). Also, `exit;` as a statement? That won't do what you probably expect it to do. Enable more compiler warnings when building. And if you really want to learn C++ then invest in [some good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282). – Some programmer dude Feb 04 '22 at 08:44
  • 1
    I'll just put this here: https://dl.acm.org/doi/10.1145/362929.362947 – littleadv Feb 04 '22 at 08:46
  • 1
    Whatever reading material you use to study C++, dispose of it in a responsible manner and get yourself a [reputable book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – n. m. could be an AI Feb 04 '22 at 09:04

1 Answers1

0

The proper way to structure the code would be (pseudocode follows):

Do
  PrintPrompt()
  input = ReadInput()
  switch (input) 
    case 1: DoFirstThing()
    case 2: DoSecondThing()
    ...
    default: PrintError()
While (UserWantsToContinue())

There's really no justification to using goto here (or almost anywhere, see answers to this question). For the canonical thesis go here.

littleadv
  • 20,100
  • 2
  • 36
  • 50
  • I understand your point but what i want to do is study ALL aspects of C. I want to see how i could let the function just work in the place where i want it to work (i know hw to use switch..case, if..elseif..) I am taking this as an example to learn a new aspect of the complier – mohamed Feb 04 '22 at 10:00
  • @mohamed you will be an old man long before you can study ALL aspects of C. You rather focus on stuff that has proven to be useful. `goto` has turned out to be not that useful already in the last millenia ;) – 463035818_is_not_an_ai Feb 04 '22 at 10:05
  • @463035818_is_not_a_number, seems like harsh reality to me but ok i will put that in mind next time i am coding. – mohamed Feb 04 '22 at 10:07
  • @mohamed its not harsh, its a wonderful reality with almost infinite possibilities, though its easy to get lost. `goto` won't lead you to nice code, others have tried it already, you don't have to make the same mistakes again – 463035818_is_not_an_ai Feb 04 '22 at 10:10
  • @463035818_is_not_a_number, i didn't mean the `goto` example . I meant deep tricks in the compiler like actually forcing it to just compile the function in a specifc line (putting the function where i want it to act) – mohamed Feb 04 '22 at 10:12