Separate String into different Variables

So currently I am trying to separate a string that a user provides to reserve a seat on an airplane, Examples: "2A" "3C". I need to separate that that first number and then the letter into separate variables.

Here is how I obtain the string:

1
2
cout << "Please choose a seat to reserve, with the row number and section seat (Ex: 3A, 2D): " << endl;
cin >> SeatChoice;



My attempt at trying to iterate and pass the materials of that string into separate variables Please help!:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void SeatFunction(string SeatChoice, char& SectionChoice, char& RowChoice) {
	char chr;
	
	for (int i = 0; i < SeatChoice.length(); i++) {


		RowChoice = SeatChoice[i];
		i++;
		SectionChoice = SeatChoice[i];

	}






}
Last edited on
On the assumption that the input will always be one number and one letter,

1
2
3
4
5
void SeatFunction(string SeatChoice, char& SectionChoice, char& RowChoice)
{
  SectionChoice = seatChoice[0];
  RowChoice = seatChoice[1];
}
I thought that too and tried that originally, but it says "Exception thrown: write access violation. _Left was 0xF."
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

void seatFunction( string seatChoice, int &seatRow, char &seatPos )
{
   stringstream( seatChoice ) >> seatRow >> seatPos;
}


int main()
{
   int row;
   char pos;
   seatFunction( "26D", row, pos );
   cout << "Row is " << row << '\n';
   cout << "Position is " << pos << '\n';
}


Row is 26
Position is D
You got it! Thank you so much!
Topic archived. No new replies allowed.