convert a string to a char

Alright well my program only seems o work when i have a switch statement in place but it won take a string type as an answer so how would i convert a string to a char

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
  int main()
{
	cout <<" Hello please enter current month "<< endl;
	getline(cin,Month);
	create();
	
	
	cout << "Do you have anything new to log ? yes(Y), no(N), exit(E) ";
	getline(cin,ans1);
	ans1 = (ans1[0]);
	switch (ans1)
	{
	case 'Y': 
	getline(cin,universal);
	SetNew();
	Review();
		break;
	case 'N': noDevelopment();
		break;
	case 'E': return 0;
		break;
	default :
		break;
	}
	system("pause");
}
If you want just the first character, just use ans1[0] (assuming, of course, you know the string is not empty).
e.g.:
switch(ans[0]) {

Just FYI, you cannot actually switch on full strings. You have to use if/else chains for that if you want it.
yeah i found that out the hard way , but it would seem when i did a if/else chain it would combined both the review() and the nodevelopment() together but it didn't with a switch so i just needed that char to help me out there, so thank yo for the help I can't believe i didn't see it myself .
The string class has a c_str() method that returns the equivalent of a pointer-based character string. If I remember correctly, we can use c-style strings in switch statements.

Joe - C++ tutor
Topic archived. No new replies allowed.