Switch Statements; CASES BEGIN WITH SAME LETTER?

Hi! In this program, I am using switch statements in order to execute commands from a made up language. However, I cannot seem to get my program to distinguish DOUBLE from DIV, as you can only use one character to discriminate between cases. I would like to use an If or If/else statement to distinguish DOUBLE from DIV. I cannot use "Hello[1]" or however you would put it. Would I make an empty variable? What goes in the if ( ) ? Help? Thanks so much!

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
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
using namespace std;

int main()
{

	char userInput;
	int userAdd, userAdd1, userAnswer;
	string userCON1, userCON2;

	cout << "Please enter the operation: \n";
	cin >> userInput;
	
	switch (userInput)
	{
	case 'A' :
		cout << "You are using the ADD function.\n";
		cin.ignore(200, '\n');
		cout << "Please enter an integer.\n";
		cin >> userAdd;
		cout << "Thanks. Please enter the second integer.\n";
		cin >> userAdd1;
		userAnswer = userAdd + userAdd1;
		cout << "The answer is: " << userAnswer;
		break;

	case 'C' : 
		cout << "You are using the COMBINE function.\n";
		cin.ignore(200, '\n');
		cout << "Please enter a string.\n";
		getline(cin, userCON1);
		cout << userCON1;
		break;

	case 'D' : 
///?
if ( ) 
    cout >> "You are using the DOUBLE function";
else
cout >> "You are using the DIV function";
		
//?
		
	}





	return 0;
So you can't do this?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<iostream>
#include<string>
using namespace std;

int main() {
    string userInput;
    getline(cin,userInput);
    switch(userInput[0]) {
        case 'D':
        {
            switch(userInput[1]) {  // Or your if () approach
                case 'I':   // DIV
                    break;
                case 'O':   // DOUBLE
            }
        }
    }
    return 0;
}


Plus, by using getline to begin with, you can drop all those cin.ignore lines you have.
That is correct. I cannot use that specifically, but I can put switches in switches or ifs in switches. I just am a little stuck on this part. Thank you for the help thus far. Do you know if there is a way to do that?
Also note that my userInput is a character; this is how it must be! Thanks again
There was recently another thread about using strings in a switch.
http://www.cplusplus.com/forum/general/251347/

For what you are doing, there really are only two good ways:

(1) Use if..else if chains.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  if (userInput == "ADD")
  {
    ...
  }
  else if (userInput == "COMBINE")
  {
    ...
  }
  else if (userInput == "DIVIDE")
  {
    ...
  }
  else if (userInput == "DOUBLE")
  {
    ...
  }


(2) Use a lookup and dispatch. A simple std::map makes this extra easy:

1
2
3
4
void f_add();
void f_combine();
void f_divide();
void f_double();
1
2
3
4
5
6
7
8
  std::map <std::string, void(*)()> dispatch
  {
    { "ADD",     f_add },
    { "COMBINE", f_combine },
    { "DIVIDE",  f_divide },
    { "DOUBLE",  f_double },
  };
  if (dispatch.count( userInput )) dispatch[userInput]();

Hope this helps.
I think that both pieces of advice are very helpful, but I cannot change the format of the current code that I am providing. I must do this inside of a switch statement and preferably with an embedded 'if' statement. Thank you for the advice, though. I will save it for later purposes.
*shrug*
If you really want a complete hands tied behind your back answer, then perhaps.
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
	cout << "Please enter the operation: \n";
	cin >> userInput;
	
	switch (userInput)
	{
	case 'A' :
		cout << "You are using the ADD function.\n";
		cin.ignore(200, '\n');
		cout << "Please enter an integer.\n";
		cin >> userAdd;
		cout << "Thanks. Please enter the second integer.\n";
		cin >> userAdd1;
		userAnswer = userAdd + userAdd1;
		cout << "The answer is: " << userAnswer;
		break;

	case 'C' : 
		cout << "You are using the COMBINE function.\n";
		cin.ignore(200, '\n');
		cout << "Please enter a string.\n";
		getline(cin, userCON1);
		cout << userCON1;
		break;

	case 'D' : 
		cin >> userInput;  // Get another letter
		///?
		if ( userInput == 'O' ) 
			cout >> "You are using the DOUBLE function";
		else
			cout >> "You are using the DIV function";
		
//? 
Hey!

Grabbing another character worked! Thanks so much!
Ah, I take it this is a homework assignment.

Sometimes I wonder what a student is supposed to get from unimaginative assignments poorly fitting to the learning objective(s)...
Quite.

If this were a course in teaching people to run marathons, the initial objective seems to be to create a level playing field by breaking the legs of all the students.
Then teaching them how to crawl, hobble, walk and finally run at some reduced speed.
Topic archived. No new replies allowed.