Home > database >  C program terminates without even taking the input for vector
C program terminates without even taking the input for vector

Time:06-13

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

CodePudding user response:

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;
}
// ...

CodePudding user response:

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.

CodePudding user response:

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

  • Related