-1

I'm a teenager trying to learn C++. I used to use Batch because all I wanted to program at that time was simple CYOA games. In batch, there's a system that divides codes like so:

@echo off
[Pretend there's some elaborate code here]
...
:menu
echo Would you like to go again?
echo 1. Yes
echo 2. No
set /P choice1="Choice: "
set choice=
if '%choice%'=='1' goto :menu
if '%choice%'=='2' exit

and then 'goto :menu' would take you back to the start of that section and redo the code again. I'm aware of loops, but is there a function that works just like this? If it helps, here's my (very simple) code.

#include <iostream>
using namespace std;

int main()
{
    int num1;
    int num2;
    int product;
    int sum;
    string response;
    cout << "Enter two numbers separated by commas.\n";
    cin >> num1, num2;
    product = num1 * num2;
    sum = num1 + num2;
    
    if (product>sum){
        cout << "The product is larger than the sum.";
    }
    else{
    cout << "The sum is larger than the product.";
    }
    cout << "Would you like to do this again? Y/N\n";
    cin >> response
    if (response==Y)
    

    return 0;
}
  • 4
    `:menu` isn't really a function in `.BAT` files, is it? Isn't it a label? Anyway, you can do the same in C++ - but you should very rarely do that since there are better (more easy to follow) ways to control the program flow. – Ted Lyngmo Sep 22 '20 at 23:12
  • It is a label, my bad. I forgot the name :/ – 555hooligan Sep 22 '20 at 23:18
  • 2
    1. Your way of asking for choice is "wrong". Use [`choice`](https://ss64.com/nt/choice.html) instead. 2. Don't learn batch, learn powershell instead which is cleaner and has no legacy issues – phuclv Sep 22 '20 at 23:22
  • Alright, I'll keep that in mind – 555hooligan Sep 22 '20 at 23:31
  • Hint: `std::toupper` and `std::transform(response.begin(), response.end(), response.begin(), toupper)` These are methods to convert the response to upper case so you only need to make one comparison. BTW, you need to compare to string literals, e.g. `response == "Y"` – Thomas Matthews Sep 22 '20 at 23:41
  • For menu selection and processing, the are a plethora of methods. Search the internet for "c++ menu selection". Some use `switch` statements, others use table driven with function pointers. – Thomas Matthews Sep 22 '20 at 23:43
  • C++ has `goto`, too, but using it is frowned upon. The idiomatic way to repeat until getting a "n" response is by enclosing everything in a `while (true)` loop and using `break` (or `return`) to leave the loop when the user says to stop. – Pascal Getreuer Sep 23 '20 at 04:42

1 Answers1

1

Yes, C++ has goto. What you are doing in the batch file is essentially a loop and you should use a while loop in the equivalent C++.

goto programming easily leads to code that is difficult to understand or to modify. Instead, you should use structured programming with if statements, for and while loops, and functions. They are the bread and butter of C++ and many other programming languages.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • Thank you so much! – 555hooligan Sep 22 '20 at 23:15
  • 2
    sometimes `goto` will make a cleaner code, but most of the time it'll make the code less readable. [Is it ever advantageous to use 'goto' in a language that supports loops and functions? If so, why?](https://stackoverflow.com/q/24451/995714), [What is wrong with using goto?](https://stackoverflow.com/q/3517726/995714), [GOTO still considered harmful?](https://stackoverflow.com/q/46586/995714) – phuclv Sep 22 '20 at 23:30
  • 3
    `goto` is that screwdriver in your toolbox with the really weirdly-shaped head. It should only come out of the box when you have the really weirdly-shaped screw. And even the it may be easier to find a way to use a regular screw. In the real world of programming, it's often harder to get a `goto` through a code review, even when it's used perfectly and you can prove it, than it is to deal with the clunkier alternative. For one thing, someone will try to replace the `goto` and totally things up sooner or later. – user4581301 Sep 23 '20 at 00:20