How can I "export" a variable declared within an if out of the if scope?

Hello. I'm trying to code a program that would prompt the user for whether it should assume a default resolution for the window or if it should accept width and height as input by the user, in case it wasn't specified through argv[]. The first problem is what to do if the user says they want the program to receive the resolution as input. I solved that lazily with

1
2
3
4
5
6
7
8
9
10
11
12
13
14
    else
    {
        char chs;
        std::cout << "No window width and height passed. Shall I assume 640x640? [Y/n]";
        std::cin >> chs;
        if(chs == 'n' || chs == 'N')
        {
            askForResolution = true;
        }
    }
    if(askForResolution)
    {
        // Here is where I'm stuck.
    }


Thing is, the resolution should be a const short and if I declare both of them inside the if statement they won't be of use for anything outside of it. I was wondering if I could "export" this to main()'s scope. Thank you all.
You can't, you'd have to declare the variable(s) outside the if block(s).

Scoping issues can be a PITA if not properly accounted for.
Thank you, Furry Guy.
You can put the code into a function which returns say a std::pair. You can then call this function as initialisation for a const std:pair variable. This would keep the variable const when defined whilst allowing the required functionality.
Topic archived. No new replies allowed.