zelzzel
zelzzel

Reputation: 11

How to get multiple input from user in one line c++

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.

Upvotes: 1

Views: 1568

Answers (4)

Chris
Chris

Reputation: 36680

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

Upvotes: 4

Muzammil Malik
Muzammil Malik

Reputation: 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;
}

Upvotes: -1

D&#250;thomhas
D&#250;thomhas

Reputation: 10123

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.

Upvotes: 1

kiner_shah
kiner_shah

Reputation: 4681

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

Upvotes: 1

Related Questions