Switch statement-invalid conersion from char to int

hello everyone,
I'm not sure whats going on with my switch statement. I have a enumerated type value of 0,1,2,or 3 that I am passing into my function. I want to convert that int value to a string, but I am getting an error message on (name=): invalid conversion from'const-char' to int. I thought I could convert an int to a string. Line 27,28 on the first section of code and line 11 on the second pertains to the switch statement. Any help would be appreciated. Thank you.

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
	while(!exitLoop)
	{
		choice = mainMenu();				//returns the verified input from main menu
		choice = menuValidation(choice);
		
		if (choice == 'D' || choice == 'd')					//if choice is 'D' exit the loop														
			exitLoop = true;
		cout << endl;
		choice = static_cast<int>(choice);					//change status of choice from char to int
		if(choice == '1')										
		{
			displayOccupiedOffices(total, buildingArray);
			total =calculateCurrentStat(buildingArray, numLawyer, numParalegal, numAssistant);
		//	cout << "choice 1= " << choice;
		}
		else if(choice == '2')
		{
			displayOccupiedOffices(total, buildingArray);
			total =calculateCurrentStat(buildingArray, numLawyer, numParalegal, numAssistant);
			//cout << "choice 2= " << choice;
		}
		else
		{
			displayEmptyOffices(buildingArray);					//displays all empty offices
			enumType = optionThree(flrNum, officeLetter);		//displays option 3 questions (floor number, column letter, and occupant type)
			officeNum = convertLetterToNumber(officeLetter);	//converts the office letter to a numeric value
			buildingArray[flrNum][officeNum] = enumType;		//adds the occupant type to the 2D array (enumerated type)
			//occupantName = displayOccupantType(enumType);		//occupantName becomes the string value of the enumerated type name
			newOccupancyMessage(occupantName, flrNum, officeLetter, buildingArray); 			//displays new occupant message
			total =calculateCurrentStat(buildingArray, numLawyer, numParalegal, numAssistant);	//re-calculates all current status in the array
			//cout << "choice 3= " << choice;
		}
	}

1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
	//variables	
	officeBuilding buildingArray;	//2D array
	int officeCount = 0;			//total count for occupied offices in the binary file
	bool results;					//readFile return
	bool exitLoop = false;			//exits the main loop if true
	char choice;					//return value for user input from menu options
	int numLawyer = 0, numParalegal = 0, numAssistant = 0; 	//individual count for Lawyer, paralegal, and assisant
	int flrNum, 					//
		enumType,					//return value from function optionThree
		officeNum,
		total;

 
enum occupantType {EMPTY, LAWYER, PARALEGAL, ASSISTANT};

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  //************************************************************************************
string displayOccupantType(int name) 
{
	//variables
	
	switch (name)
	{
		case 0:
			name = "Empty";
			break;
		case 1:
			name = "Lawyer";
			break;
		case 2:
			name = "Paralegal";
			break;
		case 3:
			name = "Assistant";
	}
	return name;
}
Last edited on
name is an int but name = "Empty"; tries to assign a string to it. C++ won't allow that. In C++, a variable's type never changes. That's okay, you just need a different variable. Since it's the return value of a function. I'll use "result" which is what I always use for this purpose:
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
string displayOccupantType(int name) 
{
	//variables
	string result;
	
	switch (name)
	{
		case 0:
			result = "Empty";
			break;
		case 1:
			result = "Lawyer";
			break;
		case 2:
			result = "Paralegal";
			break;
		case 3:
			result = "Assistant";
			break;
		default:
			result = "UNKNOWN";
			break;
	}
	return result;
}

I added a default case that returns "UNKNOWN." This is handy for debugging.
closed account (E0p9LyTq)
You can also change the parameter type to your enum type. That way you can use the enum labels in your case statements. It also will prevent you from passing a value not in your enum:

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
#include <iostream>
#include <string>

enum occupantType {EMPTY, LAWYER, PARALEGAL, ASSISTANT};

std::string displayOccupantType(occupantType name);

int main()
{
   std::cout << displayOccupantType(EMPTY) << "\n";
   std::cout << displayOccupantType(LAWYER) << "\n";
   std::cout << displayOccupantType(PARALEGAL) << "\n";
   std::cout << displayOccupantType(ASSISTANT) << "\n";

   // now can't pass any value other than what is defined in the enum
   // std::cout << displayOccupantType(125) << "\n";
   
   return 0;
}

std::string displayOccupantType(occupantType name)
{
   //variables

   switch (name)
   {
      case EMPTY:
         return "Empty";

      case LAWYER:
         return "Lawyer";

      case PARALEGAL:
         return "Paralegal";

      case ASSISTANT:
         return "Assistant";

      default:
         return "";
   }
}
Last edited on
Thank you for your input. Thanks for pointing me in the right direction. Your help is greatly appreciated.
Topic archived. No new replies allowed.