1
5
1 2 3 4 5

the first line is how many input will user give. and the second line is the input from the user. basically it's "c >> a >> b >> c;" but it's up to the user how many input they want.

0

4 Answers 4

4

The answer is quite simple. Read an int n indicating the number of items, then declare a std::vector<int> and read in n elements in a loop, pushing each onto the vector. This can be done either with an explicit for loop, or using standard library functions.

E.g.

#include <vector>
#include <iostream>

int main() {
    int n;

    std::cin >> n;
    if (std::cin.fail()) {
        std::cerr << "Input failed." << std::endl;
        return 1;
    }

    std::vector<int> nums(n);

    for (auto& x : nums) {
        std::cin >> x;
        if (std::cin.fail()) {
            std::cerr << "Input failed." << std::endl;
            return 1;
        }
    }
}

This will read n values from standard input even across multiple lines, but if you want to read those values from the next line, you'd want to read that line into a string, feed that string into a std::istringstream, and then extract from that stream.

#include <vector>
#include <iostream>
#include <string>
#include <sstream>

int main() {
    int n;

    std::cin >> n;
    if (std::cin.fail()) {
        std::cerr << "Input failed." << std::endl;
        return 1;
    }

    std::vector<int> nums(n);
    std::string line;
    std::cin.ignore(1024, '\n');
    std::getline(std::cin, line);
    std::istringstream ss { line };

    for (auto& x : nums) {
        ss >> x;
        if (ss.fail()) {
            std::cerr << "Input failed." << std::endl;
            return 1;
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

It's simple to read input and store in std::vector. You can resize the vector to hold n elements by passing n to its constructor. Then you can read into the std::vector like you do for a normal array.

#include <vector>
#include <iostream>

int main()
{
    int n;
    std::cin >> n;
   
    std::vector<int> v(n);
    for (int i = 0; i < n; i++)
        std::cin >> v[i];
    
    for (int i = 0; i < n; i++)
        std::cout << v[i] << std::endl;
}

3 Comments

@ZongruZhan, but how to pass n to std::array? I tried but it threw some error, expecting a const, something like that. Maybe you can post another answer with your solution using std::array.
Sorry, I mean C99 style runtime-sized arrays, which is permitted in g++ compiler. int v[n];
But, it's not standard C++ and not all compilers will support it.
1

I would be inclined to use a std::vector over any other data type.

#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>

int main()
{
  std::vector <int> xs;

  int n;
  std::cin >> n;

  // method 1
  std::copy_n( std::istream_iterator <int> ( std::cin ), n, std::back_inserter( xs ) );

  // method 2
  int x; while (n--) { std::cin >> x; xs.push_back( x ); }

In general, your goal should not be to do things “in one line”, but to do things correctly and succinctly, favoring correctness over terseness.

Comments

-1
#include <iostream>

int main() {
    int n;
    std::cout << "Enter n ";
    std::cin >> n;

    int inputUser[n];
    for (int i = 0; i < n; i++)
        std::cin >> inputUser[i];
        
    for (int i = 0; i < n; i++)
        std::cout << " "<<inputUser[i];
    return 0;
}

2 Comments

int inputUser[n]; is no standard C++ and not all compilers support it. The size has to be known at compile-time. Better use vector.
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.