How can I store both Numbers and Letters in a Variable

Basically I want to be able to ask the user to enter a grid reference which is two letters followed by 6 numbers eg SX 345787 and then put that into an array. Is this possible?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
std::string line;

std::getline(line, cin); // Gets everything up to a \n (enter). 
// line now contains "SX 345787"

std::stringstream iss(line); // Let's put this in a stream.

struct Grid_reference // Let's decide how we will store/access this data
{
  std::string LetterPart;
  int NumberPart;
} grid_reference;

iss >> grid_reference.LetterPart; // Let's extract the data from the stream and put it in the structure.
iss >> grid_reference.NumberPart; // And we can continue with the numbers. 
Last edited on
Topic archived. No new replies allowed.