Switch statements + strings

do string statements work with strings? i seem to have problems with that...

Nope. Switch only works with integer types.
ok that helps X) thnx!
Though you can do stuff to switch on a string fairly simply. Here's the relevant part of an old post of mine:
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <algorithm>
#include <cctype>
#include <functional>
#include <iostream>
#include <string>
using namespace std;

const char* months[] =
  { 
  "jan", "feb", "mar", "apr", "may", "jun",
  "jul", "aug", "sep", "oct", "nov", "dec"
  };

int main()
  {
  string user_input;
  cout << "Please type the name of your favorite month> " << flush;
  getline( cin, user_input );

  // We're only interested in the first three characters
  user_input += "---";  // (but make sure there actually are three...) 
  user_input.erase( 3 );

  // Convert user's input to lowercase
  transform(
    user_input.begin(),
    user_input.end(),
    user_input.begin(),
    ptr_fun <int, int> ( tolower )
    );

  // Find the index of the matching month
  int month = find(
                months,
                months +12,
                user_input
                )
            - months;

  if (month < 12)
    cout << "The month number is " << (month +1) << ".\n";
  else
    cout << "That was not a month name.\n";

  return 0;
  }
http://www.cplusplus.com/forum/general/11460/page1.html#msg54095

Hope this helps.

[edit]
Oh, so to finish it, just switch on the value calculated using the std::find() algorithm.
Last edited on
Topic archived. No new replies allowed.