-1

I am trying to input n integers into the vector. Following is the code I have used to do so. First, I ask for the value n, then input all those n integers.

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin >> n;
    vector<int> input(n);
    for (int i = 0; i < n; i++)
    {
        cin >> input[i];
    }
    return 0;
}

I don't know why the program is terminating without taking any input, I can't see any flaws with this code, and it seems to work fine for others but not for me.


This is what the terminal looks like after executing this program. This is what the terminal looks like after executing this program.

Input Example:
5
5 3 1 5 2


When I try to compile the program in the windows terminal, I get this error error

run error

  • [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Filburt Jun 13 '22 at 08:44
  • 3
    You need to add `#include ` and `#include `. – Nick is tired Jun 13 '22 at 08:44
  • @NickstandswithUkraine Thanks but includes every standard library, I still tried doing what you said, but it's making no difference. – Ichchha Gupta Jun 13 '22 at 08:49
  • 2
    IIRC VSCode is not setup by default to take input from the terminal. There's some setting somewhere that you have to change. Try running your program from outside of VSCode and see what happens. – john Jun 13 '22 at 08:51
  • Except for coding style, there's nothing technically wrong with your program. Did you already have it build an executable and run that in a normal terminal (cmd/powershell on windows, bash in linux/apple)? Also, add output to see what it is doing. – stefaanv Jun 13 '22 at 08:52
  • 2
    *" includes every standard library"* - Yuck, I'd suggest getting out of the habit of using it then. But if that's the case, there's nothing obviously wrong (with the code itself): https://i.stack.imgur.com/YqMnC.png – Nick is tired Jun 13 '22 at 08:52
  • 1
    *includes every standard library*, that's one reason not to use it. Another is that it isn't standard C++, another is that it annoys people in this forum. [see here](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – john Jun 13 '22 at 08:53
  • Try [Visual Studio Code: Take Input From User](https://stackoverflow.com/questions/36964949/visual-studio-code-take-input-from-user) – Nick is tired Jun 13 '22 at 08:55
  • BTW: https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question/285557#285557 – Marek R Jun 13 '22 at 09:31

3 Answers3

1

When there is some data on the input stream, that is not parseable as number, n is set to 0.

This may be caused, when your terminal sends line-breaks to the input stream, or is not setup at all, to forward terminal input (like john mentioned).

Either run it from a different terminal or add a workaround, to only accept numbers greater than 0:

int n = 0;
while (n == 0) {
  cin >> n;
}
// ...
jdoubleu
  • 31
  • 4
  • This loop is wrong solution and doesn't explain crash. Demo: https://godbolt.org/z/5eq8fGo6d – Marek R Jun 13 '22 at 09:03
  • @Marek The program does not crash. That is not the issue, as far as I understood. Using the std::cin as condition in the while-loop does have a different meaning. It would still allow users to input 0. If that's the desired behaviour, yes your approach is probably better. – jdoubleu Jun 13 '22 at 09:13
  • It is a crash - note question was updated. – Marek R Jun 13 '22 at 09:22
  • Didn't see the updated question, sorry. The crash looks more like an issue with `std::cin` not being defined. It's probably because gcc doesn't ship with a stdlib on Windows (see https://stackoverflow.com/a/72600463/7201175). I would also suggest to use MSVC (cl.exe) instead of gcc on Windows, because you could also run the program then. – jdoubleu Jun 13 '22 at 09:31
0

Edit after question update:

The screenshot of error indicates that problem is not your code, but software installed on your machine. This error was raised since standard library was loaded, but function specificity for compiler you used was not found.

One explanation is you have more then one compiler installed, other is you copied some wrong dlls to directory with your application. It is also possible you provided some strange compiler flags.

So please inspect what has been installed on your machine and what is in current directory of your application (are there some dlls)?

Screenshot of command line (please do not do that) indicates that you are building project in wrong way. Use of gcc do not link C++ standard library into a code. You should use g++ then standard library will be linked to your program implicitly.

This is strange that your program failed to build (console screenshot) and you run it somehow which lead to runtime error (error message box image).

Old answer

Can be still valid.

Please provide example of input.

  • If you provided negative value then exception can be thrown terminating program without any output.
  • other explanation you have entered invalid value and std::cin is in error state. In such case std::cin doesn't do anything until error is handled.

Here is demo of proper input, negative size or invalid none numeric value.

Marek R
  • 32,568
  • 6
  • 55
  • 140
  • Thanks, but I am not even getting chance to input anything, it terminates automatically. I have used the VScode terminal to take input hundreds of times without any problem, it seems to work perfectly in the case of arrays. Also, I am interested in positive values only. Thanks again. – Ichchha Gupta Jun 13 '22 at 09:26
-1

You are wrong in declaring and assigning the value. Remember that you are using a vector and not an array of any type.

According to your setting the correct syntax would be:

#include <bits/stdc++.h>

using namespace std;

int main() {
    int numbers;

    vector<int> input;

    cout<<"How many numbers? ";
    cin>>numbers;

    int temp; /* temporary value used in vet.push_back() */

    for (int i = 0; i < numbers; i++) {
        cout<<"Vector input: ";
        cin>>temp;
        input.push_back(temp);
    }

    cout<<"Size of vector: "<<input.size()<<"\n";

    // for output
    for (int i = 0; i < input.size(); i++)
        cout<<input[i]<<" ";

    return 0;
}

Indeed, a vector is dynamic. You don't need to give it a pre-defined dimension. It is declared with a default size and every time it reaches the maximum size, it double. It is comfortable to use, but it is not efficient due to the fragmentation and unused areas it requires.

On programs like yours, however you can use dynamic array.

Last thing, if you are using vectors take into account the #include <vector> library

  • Although I can't say anything about why this has been downvoted, I'm guessing that the use of `#include ` is a major contributing factor. – Adrian Mole Jun 13 '22 at 13:59
  • literally the last line I wrote: "Last thing, if you are using vectors take into account the #include library" – Giuseppe Speranza Jun 14 '22 at 20:43