0

I am currently working on a C++ project in visual studio 2019 which is about star patterns. there is 6 patterns to be printed and user must select them by entering the patterns number. I noticed that my professor's program automatically closes when user press 7. How would I code that? I only know using return 0 gets me "Press Any Key to Continue". but in my professors program which is in .exe format the console immediately closes after pressing 7 without showing "Press any key to continue.."

this is my code..

int main()
{

    while (choice != 7) {

        cout << "Enter your choice\n";
        cout << "1.Rectangle 1\n"
             << "2.Rectangle 2\n"
             << "3.Rectangle 3\n"
             << "4.Rectangle 4\n"
             << "5.Rhomb(only odd)\n"
             << "6.circle \n"
             << "7.close Application\n\n";
        cin >> choice;

        cout << "Enter the dimension\n";
        cin >> dimension;

        switch (choice) {

        case 1:
            Rectangle_1();
            break;
        case 2:
            Rectangle_2();
            break;
        case 3:
            Rectangle_3();
            break;
        case 4:
            Rectangle_4();
            break;
        case 5:
            Rectangle_5();
            break;
        case 6:
            Rectangle_6();
            break;

        default:
            break;
        }

        system("cls");
    }
}
AbdelAziz AbdelLatef
  • 3,650
  • 6
  • 24
  • 52
AMIR PEDRAM
  • 9
  • 1
  • 3
  • You need to ask for a number then input it using cin and check if it is 7 in all inside a loop. – drescherjm Dec 03 '20 at 14:09
  • Did you cover input and output? Did you cover conditionals (`if`/`else`)? You might consider reviewing those topics. – crashmstr Dec 03 '20 at 14:12
  • 1
    How do you start your program? How do you start your professor's program? – king_nak Dec 03 '20 at 14:13
  • 1
    The Visual Studio IDE adds the prompt that you see in your program for you by a setting. Also if you test from a cmd.exe window instead of the ide you won't see this prompt. With that said for the feature you want you need to write the code I suggested in my first comment. – drescherjm Dec 03 '20 at 14:18
  • 1
    You should make `choice` a local variable in the `int main()` function instead of a global variable. Remember you need to initialize it to a value as a local variable. Other than that it should already do what you want. Run it from cmd.exe to see. – drescherjm Dec 03 '20 at 14:22
  • 1
    Related if not a duplicate: [https://stackoverflow.com/questions/17897736/avoiding-press-any-key-to-continue-when-running-console-application-from-visua](https://stackoverflow.com/questions/17897736/avoiding-press-any-key-to-continue-when-running-console-application-from-visua) – drescherjm Dec 03 '20 at 14:27
  • 2
    Automatically closing the console was the default behaviour of previous versions of Visual Studio (and I believe it's an option you can select in current versions). The behaviour was likely changed because of the volume of "My program didn't run!" "My program flickered and crashed!" and "Dude, where's my output?" questions and comments that had been racked up by MSDN over the years. Plus it helps get rid of all of the `system("pause");` BS tacked onto the end of otherwise good programs. – user4581301 Dec 03 '20 at 14:28

0 Answers0