how to do this?

how do i make a c++ file that will print the entered number like a number string on the screen?
Example:
I entered 123,
And it has shown
1
2
3
One simple way is like this:

1
2
3
4
5
6
7
8
9
10
11
12
#include <string>
#include <iostream>

int main() {
	std::string n;

	std::cout << "Enter number: ";
	std::cin >> n;

	for (const auto& c : n)
		std::cout << c << '\n';
}



Enter number: 123
1
2
3

Another recent thread on the topic: https://cplusplus.com/forum/beginner/284003/
Another way to do this, keeping the entered integer value as a number (a bit more complicated than seeplus' example):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>

int main()
{
   std::cout << "Enter a number: ";
   int num { };
   std::cin >> num;

   // std::to_string requires C++11 or later
   // https://en.cppreference.com/w/cpp/string/basic_string/to_string
   std::string num_str { std::to_string(num) };

   for (const auto& itr : num_str)
   {
      std::cout << itr << '\n';
   }
}
Topic archived. No new replies allowed.