-1
#include <bits/stdc++.h>

using namespace std;

struct Cow
{
    int p, s, t;
};

int main()
{
    int n, b;
    cin >> n >> b;

    Cow cows[n];

    for (int i = 0; i < n; i++)
    {
        cin >> cows[i].p >> cows[i].s;
        cows[i].t = cows[i].p + cows[i].s;
    }

    int temp = b;
    int best = 0;

    for (int i = 0; i < n; i++)
    {
        temp = b;
        int counter = 0;
        for (int j = 0; j < n; j++)
        {
            if (j == i)
            {
                temp = temp - (cows[j].p / 2 + cows[j].s);
            }
            else
            {
                temp = temp - cows[j].t;
            }
            if (temp < 0)
            {
                break;
            }
            else
            {
                counter++;
            }
            if (counter > best)
            {
                best = counter;
            }
        }

        cout << best;

        return 0;
    }

In the above code, when I run it, it opens up a window as usual, and let's me enter the input (shown below) fine as well.

5 24
4 2
2 0
8 1
6 3
12 5

But when I press enter, it just loads for a couple of seconds, then closes the C++ window. Any help?

Thanks in advance!

anastaciu
  • 23,467
  • 7
  • 28
  • 53
Mike Smith
  • 527
  • 2
  • 6
  • 20
  • Presumably when you open it, it opens a new window? You're not running it from the command line? Them the reason it disappears is because the program is done, and therefore the window closes. – ChrisMM Apr 21 '20 at 23:43
  • I am using the Codelite IDE. It opens it in another window (ending: \cmd.exe) Two things that are wrong about this: first, it doesn't run the whole program, because It prints the answer in the end. Second, when the program ends, I have to press a key to end the program and close the window, it doesn't do so on its own. – Mike Smith Apr 21 '20 at 23:46
  • you don't read the newline from the input so Codelite IDE might count this as entering a key. Try putting `getchar();` just before returning from main. – Isaac Clancy Apr 21 '20 at 23:57
  • 1
    *"it doesn't run the whole program, because It prints the answer in the end"* -- huh? Doesn't printing the answer in the end mean that the whole program **did** run? – JaMiT Apr 22 '20 at 00:03
  • You should verify that `n` is a sane value. – drescherjm Apr 22 '20 at 00:05
  • 2
    A close brace was dropped somewhere. – user4581301 Apr 22 '20 at 00:06
  • 1
    Where is `counter` defined? – user4581301 Apr 22 '20 at 00:08
  • The best way to make a program stop and wait when running from an IDE is usually to place a breakpoint near the end of the program. `return 0;` looks like a good spot. – user4581301 Apr 22 '20 at 00:10

3 Answers3

2

It's a common problem when you run your program from the IDE, as the program ends the window automatically closes, to prevent this you can do as follows:

//...
cout << best;

cin.get(); //get the newline character present in the buffer from previous cin
cin.get(); //wait for the user to hit enter before closing the window (if needed)

return 0;
//...

You are also missing a bracket, since your program runs I assume you just missed it when you posted the code, you should include it exactly like in your code for a more complete diagnostic.


Side notes:


1. Variable Length Arrays (cows[n]) are not part of C++ standard, though some compilers allow it, you shouldn't use it. Try std::vector.

Why aren't variable-length arrays part of the C++ standard?


2. Don't use #include <bits/stdc++.h>, it's a bad practice, moreover in this case where you only need the <iostream> library.

Why should I not #include <bits/stdc++.h>?


3. using namespace std; is also not good. Use std:: scope or individual usings, for instance: using std::cout;

Why is "using namespace std;" considered bad practice?


The combination of 2 and 3 can sometimes have nasty effects.

Consider the following example:

#include <bits/stdc++.h>

using namespace std;

int size = 0;

int main()
{
    cout << size;
}

This code works fine with pre C++17 compilers, but since C++17 introduced std::size, you will have an ambiguous reference to size and your program will fail to compile.

Credits for the example to @user4581301.

anastaciu
  • 23,467
  • 7
  • 28
  • 53
  • 1
    And here's a little example that demonstrates just how easily combining `#include ` and `using namespace std;` can ruin a program: https://godbolt.org/z/DxEczb – user4581301 Apr 22 '20 at 00:16
  • @user4581301, simple but effective, do you mind if I add it to the answer? This needs to be featured. – anastaciu Apr 22 '20 at 00:19
  • Feel free, but I think it works better as an addendum. The truly nasty part is this works just fine right up until the code is compiled with a C++17 compiler. – user4581301 Apr 22 '20 at 00:22
  • @user4581301, since you are the author, let me know if everything is up to your standards. – anastaciu Apr 22 '20 at 00:37
1

In the code you pasted, the "return 0" instruction is placed in the for "i" loop.

If it is the same in your file, it will automatically end on the first run of this loop, with only one "cout".

Cptn-Tomatoe
  • 116
  • 5
  • Then there would be a `}` missing below. – drescherjm Apr 22 '20 at 00:07
  • there should also be somewhere a declaration of counter, and the cows[] cannot be declared by n. I ran the code through the IDE and declared arraysize and counter, only problem remaining was that misplaced return 0 for me – Cptn-Tomatoe Apr 22 '20 at 00:13
1

In some quick debugging, here are a few issues I've found with your code.

Line 14:

Cow cows[n];

According to most C++ compilers, arrays are static in memory, what this means is you have to make an array of constant size. An array cannot be declared of variable length.

Even if the compiler you use supports this feature, it's bad practice. Look instead into std::vector, like other users have said.

Line 26:

counter = 0;

You have to declare counter's type before you initialize a value to it.

Something like:

int counter = 0;

Line 47:

return 0;

This is the reason the window closes prematurely. This command effectively ends a main function, and it is currently being run inside of a for loop.
In the first iteration of the for loop, the program will end and your loop will end prematurely.

You should move the return statement out of the for loop, then add an ending closing curly brace '}' to close off your main function.

  • The `#include ` not stopping compilation dead suggests the asker is using g++ to compile their program. g++ allows Variable Length Arrays. Personally I think it's a bad idea, but this compiler allows `Cow cows[n];` – user4581301 Apr 22 '20 at 00:12
  • Ah! Thank you for the insight, I'm not familiar with g++ compiler. – Casey Whitener Apr 22 '20 at 00:17