When you get a fixed income In C, scanf ("%1d", & array) can be entered at once Is there a way to get it without any gaps without using scanf on C++? All I know is to receive it as a string and convert it, but I think it's too much trouble <
c++
I think it would be nice to read one letter at a time and convert it into a number like the code below.
#include <iostream>
int main() {
int inputs[10];
for (int& digit : inputs)
digit = std::cin.get() - '0';
for (int digit : inputs)
std::cout << digit;
return 0;
}
Or you can create a function like this to make it easier to use.
#include <iostream>
template<typename T>
struct Digits
{
typedef T* iterator;
iterator begin, end;
template<typename U, std::size_t size>
Digits(U (&arr)[size]) : begin(arr), end(arr + size) {}
Digits(T* arr, std::size_t size) : begin(arr), end(arr + size) {}
};
template<typename T>
std::istream& operator>>(std::istream& s, Digits<T> v)
{
typedef typename Digits<T>::iterator iterator;
for (iterator itor = v.begin, end = v.end; itor != end; ++itor)
*itor = static_cast<T>(s.get() - '0');
return s;
}
int main() {
int inputs[10];
std::cin >> Digits<int>(inputs);
for (int digit : inputs)
std::cout << digit;
return 0;
}
© 2024 OneMinuteCode. All rights reserved.