getline to array

How can I use the getline function to get a string of numbers 30 digits long, and store each digit in one spot of the array?
Use std::cin instead. Then you will have to loop through the returned string and assign it to the array.

This won't take more than few lines if you use c++11.
well how do you do that?
bufige wrote:
Use std::cin instead. Then you will have to loop through the returned string and assign it to the array.

This won't take more than few lines if you use c++11.

Your answer becomes more valuable if you give code examples.

jwilt wrote:
How can I use the getline function to get a string of numbers 30 digits long, and store each digit in one spot of the array?

Could you give example input, please? Without example input I am not sure exactly what you're trying to achieve.

If you just want to read a string of digits, you could do:

1
2
3
4
5
6
7
8
#include <iostream>
#include <string>

// ...

std::string arr;

std::getline(std::cin, arr);


However this won't stop users from giving bad input such as "23193djakdADJ3992".

So you'd have to check for bad input, possibly by using the C++11 regex library, and ask for input again if you find that the user messed around.

http://www.cplusplus.com/reference/regex/
I would use cin.getline
http://www.cplusplus.com/reference/istream/istream/getline/?kw=cin.getline

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;

int main()
{
	char myArray[31];

	cout << "Enter up to 30 characters: " << flush;
	cin.getline(myArray, 31);
	
	cout << "You entered: " << flush;
	
	int i = 0;
	while (myArray[i] != '\0')
	{
		cout << myArray[i];
		i++;
	}


	cin.ignore();
	return 0;

}
Topic archived. No new replies allowed.