Char for switch case

I've got a code which allows people to change values, of the machines. It works if they input a, however if they first change an object then accept them, it crashes out
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
cout<< "Lathe:"<<endl;
cout<< " 1) The Average proccesing rate[jobs/hr]: "<<prosRL<<endl;
cout<< " 2) Cost: R"<<costL<<endl;
cout<< " 3) The number of lathes: "<<machL<<endl;

while (fin == true)
{
cout<< "To change a value, please enter the corrisponding number"<<endl;
cout<< "If you would like to keep these values please enter 'A' for Accept"<<endl;
cin>>selct;
switch (selct)
{
    case 1:
    cout<<"Enter new value for the Average proccessing rate in Jobs per hour for the lathe:";
    cin>>prosRL;
break;
//etc
    default:
    fin = false;
}
}

I think its because if they input char the switch case cant handle that??
how do i correct it?
Thank you
Last edited on
What is the type of "selct"?
Its an int
The problem might be because if you enter 'A' you get a random value in selct. Without further code I can't say, but you should fix that anyway.

Rule one of user-input: the user will always hit ENTER after typing a value. Therefore, read std::strings and convert it to the appropriate type (if possible):
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <sstream>

...

// These are the codes for the responses the user can give.
enum {
  CODE_ERROR = -1,
  CODE_FIRST,

  CODE_ACCEPT = CODE_FIRST,  // 0
  CODE_AVERAGE,              // 1
  CODE_COST,                 // 2
  CODE_NUMLATHES,            // 3
  CODE_DONE,                 // 4

  CODE_LAST = CODE_DONE
  };

...

std::string user_input_string;
int         user_input_code;
bool        finished = false;

...

while (!finished)
{
  ...

  // Get the user's input, no matter what it is (or how nonsensical).
  getline( cin, user_input_string );

  // Try to turn it into a number in the range CODE_FIRST..CODE_LAST
  // (this part could go in a separate function)
  if (user_input_string.empty())
    // The user just pressed ENTER, so we're done.
    user_input_code = CODE_DONE;
  else
  {
    stringstream ss( user_input_string );
    ss >> user_input_code;
    if (ss)
      // User successfully entered a WHOLE NUMBER.
      // Validate it.
      if ((user_input_code < 1) or (user_input_code > 3))
        user_input_code = CODE_ERROR;
      else
        user_input_code += CODE_AVERAGE -1;
    else
      // It is not a WHOLE NUMBER. Try for "A" or "a":
      if (user_input_string == "A" or user_input_string == "a")
        user_input_code = CODE_ACCEPT;
      else
        user_input_code = CODE_ERROR;
  }

  // Act upon the user's validated input
  switch (user_input_code)
  {
    case CODE_ACCEPT:
      ...
      break;

    case CODE_AVERAGE:
      ...
      break;

    case CODE_COST:
      ...
      break;

    case CODE_NUMLATHES:
      ...
      break;

    case CODE_DONE:
      finished = true;
      break;

    case CODE_ERROR:
      cout << "That is not one of the things I asked you to enter. Please try again." << endl;
  }
}


The point is that the user can enter anything without crashing your program.

Notice also how I changed the fin variable to be true if you are finished and not the other way around. Since this is how people think the code won't trip you up when you look at it later.

Hope this helps.
Last edited on
Topic archived. No new replies allowed.