-1

enter image description hereThe program outputs the number of the leftmost column with only positive numbers. Everything works fine in the Visual Studio Code terminal.

I think double-clicking on the automatically generated .exe file in a separate window should launch a full-fledged program that reads the input, processes it and displays the output (the number of the desired column or a message that there is none). In fact, when running this .exe file, a window appears, I can enter 12 numbers through Enter, but the window disappears after entering the 12th number.

Am I correct in my assumption, and if so, what could be causing the problem? If not, why is this file needed at all?

#include <iostream>
#include <cstdio>
using namespace std;

int main()
{
    int n = 3, m = 4;
    int matrix[n][m];
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            cin >> matrix[i][j];
        }
    };
    int col1[4];
    int col2[4];
    int col3[4];
    int col4[4];
    //создание 4 массивов-столбцов (creating 4 column arrays)
    for (int l = 0; l < n; l++)
    {
        for (int k = 0; k < m; k++)
        {
            if ((k % 4) == 0)
            {
                col1[l] = matrix[l][k];
            }
            else if (((k - 1) % 4) == 0)
            {
                col2[l] = matrix[l][k];
            }
            else if (((k - 2) % 4) == 0)
            {
                col3[l] = matrix[l][k];
            }
            else if (((k - 3) % 4) == 0)
            {
                col4[l] = matrix[l][k];
            }
        };
    };
    //поиск крайнего левого столбца только положительных чисел (finding the leftmost column of only positive numbers)
    int c = 0, s = 0;
    for (int g = 0; g < m; g++)
    {
        if (g == 0)
        {
            for (int d = 0; d < n; d++)
            {
                if (col1[d] <= 0)
                {
                    c++;
                }
            };
            if (c == 0)
            {
                s++;
                cout << "col 1";
                break;
            };
            c = 0;
        }
        else if (g == 1)
        {
            for (int d = 0; d < n; d++)
            {
                if (col2[d] <= 0)
                {
                    c++;
                }
            };
            if (c == 0)
            {
                s++;
                cout << "col 2";
                break;
            };
            c = 0;
        }
        else if (g == 2)
        {
            for (int d = 0; d < n; d++)
            {
                if (col3[d] <= 0)
                {
                    c++;
                }
            };
            if (c == 0)
            {
                s++;
                cout << "col 3";
                break;
            };
            c = 0;
        }
        else if (g == 3)
        {
            for (int d = 0; d < n; d++)
            {
                if (col4[d] <= 0)
                {
                    c++;
                }
            };
            if (c == 0)
            {
                s++;
                cout << "col 4";
                break;
            };
            c = 0;
        }
    };
    if (s == 0)
    {
        cout << "No positive columns";
    };
    return 0;
}

enter image description here

  • why don't you refactor your `finding the leftmost column of only positive numbers` piece of code, why do you have 4 `col` variables – rioV8 Jun 29 '22 at 17:34
  • You don't "enter an .exe file", you run it. In your case you want to run it from the command prompt (CMD.EXE). If you run it by double-clicking the .exe in the file explorer, it will open a window for your I/O, but it will close that window at program end. – L. Scott Johnson Jun 29 '22 at 17:35
  • 1
    On a side note: `int matrix[n][m];` is not standard C++, since `n` and `m` are not compile-time constants. Make `n` and `m` be `const`, or use `std::vector` instead. See [Why aren't variable-length arrays part of the C++ standard?](https://stackoverflow.com/questions/1887097/) – Remy Lebeau Jun 29 '22 at 21:12

1 Answers1

2

If you are using Microsoft Windows, then the console window will disappear as soon as your program ends. If you don't want this to happen, then you can

  1. run your program from the Windows command prompt cmd.exe instead of double-clicking it, or
  2. add something to the end of your program that prevents it from closing immediately, such as the following code statement:
std::system( "pause" );

Note that you will have to #include <cstdlib> in order to use std::system.

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
  • The code in question is C++, so use `std::cout` instead of `printf()`. And you can use `std::cin.get()` instead of `system("pause")` – Remy Lebeau Jun 29 '22 at 18:07
  • @RemyLebeau: Yes, you are correct that I should be using C++ streams instead of C streams. I have edited my answer accordingly. Regarding using `std::cin.get()`, the problem is that this will not pause the program if there already is an unread character on the input stream, which is often the case when using `std::cin.operator >>`. – Andreas Wenzel Jun 29 '22 at 18:19
  • `system("pause")` is not standard and not guaranteed to work on all platforms. And if the code is not reading all of the user's input before pausing, well some might consider that to be a bug in the code. – Remy Lebeau Jun 29 '22 at 18:35
  • @RemyLebeau OP indicates that code is reading all of the user's input. 3*4 = 12. – L. Scott Johnson Jun 29 '22 at 19:32
  • 1
    @L.ScottJohnson: The newline character at the end of the last line of input will not be consumed, so it will be left on the input stream. – Andreas Wenzel Jun 29 '22 at 19:52
  • Easily remedied with `cin.ignore()` after reading the numbers. – Remy Lebeau Jun 29 '22 at 21:13
  • @RemyLebeau: I agree with you that `system("pause");` should not be used in portable code. However, my answer is a specific to the Microsoft Windows platform, and `system("pause");` works on that platform irrespective of the current state of the input stream. Therefore, I believe it is the best solution in this case. – Andreas Wenzel Jun 29 '22 at 21:28
  • @AndreasWenzel, I did everything according to your instructions (both points), but it did not help: the program does not end after entering the data, it just closes the window immediately after entering the twelfth required number. – VladymyrTsyb Jul 02 '22 at 20:25
  • @VladymyrTsyb: Are you using Microsoft Windows? Or are you using some other operating system? – Andreas Wenzel Jul 02 '22 at 20:26
  • @AndreasWenzel, Windows 10 Home. – VladymyrTsyb Jul 02 '22 at 21:01
  • @VladymyrTsyb: I find it hard to believe that option #1 (running your program in `cmd.exe`) will cause the console window to close. Therefore, I believe that you are not following my instructions correctly. Can you post a screenshot of your console window running `cmd.exe` and typing the name of your program that you intend to run? Please make a screenshot window immediately before pressing the ENTER key that causes the console window to close. – Andreas Wenzel Jul 02 '22 at 21:23
  • @AndreasWenzel, please see the last two photos in the feed. The next press of Enter closes the program window (the cmd window does not close): https://www.facebook.com/profile.php?id=100010981128950. – VladymyrTsyb Jul 02 '22 at 21:39
  • @VladymyrTsyb: When I click on "Photos" or "See all photos", it asks me to log in with my Facebook account, but I do not have one. – Andreas Wenzel Jul 02 '22 at 21:50
  • @VladymyrTsyb: In your previous comment, you wrote: "The next press of Enter closes the program window (the cmd window does not close)" Are you saying that there are two console windows? That sounds strange. I would expect the program to be executed in the same console window as `cmd.exe`, when you run the program from there. That is at least how console programs behave that I write in Visual Studio. Maybe Visual Studio Code is different, because it uses a different compiler than Visual Studio. Note that Visual Studio Code is not the same product as Visual Studio, despite the similar name. – Andreas Wenzel Jul 02 '22 at 21:50
  • @VladymyrTsyb: I believe it would be best if you edited your question and added the images to the question, in order to demonstrate the issue. Please don't change the question in such as way that it invalidates my answer, though. It would probably be best to add the image(s) to the bottom of the question without overwriting the original question. – Andreas Wenzel Jul 02 '22 at 21:54
  • @AndreasWenzel, I added a link to the image below the code. – VladymyrTsyb Jul 02 '22 at 22:01
  • @VladymyrTsyb: Yes, I can see it now. That is strange, it seems that you do indeed have two console windows. Unfortunately, I cannot see the contents of the bottom window, because it is covered by the top window. Can you also show me the contents of the bottom window (as text or as an image)? – Andreas Wenzel Jul 02 '22 at 22:06
  • @VladymyrTsyb: Do you know what command-line options Visual Studio Code is configured to use for compiling your program? Is my assumption correct that you have configured Visual Studio Code to use the compiler named gcc? Or are you using some other compiler? – Andreas Wenzel Jul 02 '22 at 22:07
  • @VladymyrTsyb: Unfortunately, I am not familiar with Visual Studio Code (I use Visual Studio instead). However, the answers to [this question](https://stackoverflow.com/q/58352944/12149471) may contain some relevant information. There appear to be many things that you can configure in Visual Studio Code. – Andreas Wenzel Jul 02 '22 at 22:10
  • @VladymyrTsyb: If you have configured Visual Studio Code to use the MINGW compiler (which is based on the gcc compiler), it would be interesting to know whether you have configured Visual Studio Code to compile with the `-mconsole` or the `-mwindows` command-line option. [This answer](https://stackoverflow.com/a/13102199/12149471) explains the relevance of these two command-line options. – Andreas Wenzel Jul 02 '22 at 22:24
  • @AndreasWenzel, The second image is at the very beginning of the question. – VladymyrTsyb Jul 02 '22 at 22:24
  • @AndreasWenzel, gcc. MinGW. – VladymyrTsyb Jul 02 '22 at 22:25
  • @VladymyrTsyb: Why are you using the `start` command? By using that command, you are explicitly asking that the program is executed in a new window. That is not what you want. Instead of `start left_column.exe`, you should simply write `left_column`. – Andreas Wenzel Jul 02 '22 at 22:29
  • @VladymyrTsyb: I still do not understand why `std::system( "pause" );` does not work, though. As far as I can tell, it should have worked, even when using the `start` command. Did you put `std::system( "pause" );` immediately before the line `return 0;` in your function `main`? – Andreas Wenzel Jul 03 '22 at 22:35
  • @AndreasWenzel, yes exactly. In this case, the program window closes after entering the twelfth number. I tried between std::system("pause"); and return 0; write int ab; cin >> ab; cout << ab;, but this block of code is not executed. I am creating a c++ based computer game. I would like to be able to run the .exe file by double-clicking, but this is not working yet. – VladymyrTsyb Jul 04 '22 at 06:14
  • @VladymyrTsyb: The statement `std::system("pause");` should be at the end of the program, immediately before the `return 0;` statement. You should not write anything in-between those statements. – Andreas Wenzel Jul 05 '22 at 05:44