How to get cin user input in c ++ 11 std :: array

How can I get input from user in std::array?

This is what I have, but it will not compile.

std::array<char, 10> myArray{"hello"} ;
std::cout << "Enter your name: ";
std::cin >> myArray;

If more than 10 characters are entered, trim and ignore them. I will also need to clear the cin buffer to subsequently enable another input.

+4
source share
6 answers

For your current example, you should use std::stringinstead std::array<char, 10>. However, if you still want to read the array, you can do it like this:

#include <iostream>
#include <array>

int main() {
    std::array<int, 10> arr;
    for(int temp, i = 0; i < arr.size() && std::cin >> temp; ++i) {
        arr[i] = temp;
    }

    for(auto&& i : arr) {
        std::cout << i << ' ';
    }
}

Conclusion:

$ ./a.out
10 11 12 13 14 15 16 17 18 19
10 11 12 13 14 15 16 17 18 19

- , operator[]. std::cin >> temp , . , , .

, :

#include <iostream>
#include <array>

template<typename T, size_t N>
std::istream& input_array(std::istream& in, std::array<T, N>& arr) {
    unsigned i = 0u;
    for(T temp; i < arr.size() && in >> temp; ++i) {
        arr[i] = std::move(temp);
    }
    return in;
}

int main() {
    std::array<int, 10> arr;
    input_array(std::cin, arr);
}

operator>> std, undefined.

, :

template<typename T, size_t N>
std::istream& input_array(std::istream& in, std::array<T, N>& arr) {
    for(unsigned i = 0u; i < arr.size() && in >> arr[i]; ++i) { 
        // empty body
    }
    return in;
}
+4

:

std::string name;
std::cout << "Enter your name: ";
std::getline(std::cin, name);

Live demo

+1

<iostream> <limits>.

std::cin.get(myArray.data(), myArray.size());
// get() will actually read up to myArray.size() - 1 chars
// and add a null character at the end ('\0')

if (std::cin.fail()) {
    // could not read any characters or EOF occurred
} else {
    // drop rest of line that was read to buffer
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

    // number of characters read: std::cin.gcount()

    // ... use myArray ...
}

:

http://en.cppreference.com/w/cpp/io/basic_istream/get

http://en.cppreference.com/w/cpp/io/basic_ios/fail

http://en.cppreference.com/w/cpp/io/basic_istream/ignore

http://en.cppreference.com/w/cpp/io/basic_istream/gcount

+1

, 10 ( , 10) . std::cin.

std::array<char, 10> myArray{"hello"} ;

std::string s;
std::getline( std::cin, s );
if ( !std::cin )
     throw.....   // input was closed before a line was entered

std::size_t len = std::min( myArray.size(), s.size() );
std::copy(s.begin(), s.begin() + len, myArray.begin() );

, myArray. , ( len myArray.size() - 1).

<array>, <iostream>, <algorithm>

0

std::cin char (char*). .data().

std::array<char, 50> arr{"Hello, "};
size_t hello_length = strlen(arr.data());

std::cout << "Enter your name: ";
std::cin >> std::setw(10) >> (arr.data() + hello_length);

std::cout << arr.data() << std::endl;
0

++ - .
std::array, .

std::cin >> myArray.begin();
0
source

All Articles