converting string to int using a switch statement

Hello everyone,
I am having a hard time figuring out why I can't convert a string to an int using a switch statement.

Here is my code:
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
  //***************************************************************************
int studentNum(string level)
{
	int name;
	switch (level)
	{
	case  "Freshman":
		name = 0;
		break;
	case "Sophomore":
		name = 1;
		break;
	case  "Junior":
		name = 2;
		break;
	case  "Senior":
		name = 3;
	}

	return name;
}
//***************************************************************************
void newSorted(ofstream& outCreditData, string idNum[], string classLevel[], int& cnt)
{ 
	int i;
	int temp;
	
 	for(int i = 0; i < cnt; i++)
	 {
	 	temp = studentNum(classLevel); 
	 	outCreditData << idNum[i] << " "  << classLevel[i] << endl; 
	  } 

 	outCreditData.close(); 

	
	return;
}


classLevel[] contains strings like "Senior, sophomore, Junior, etc.
I basically, want to convert this string value to an int, so I can write the int value to my text file. Thank you very much.
switch statements can only be used with integral types (int, char, long..). So strings don't work. Rewriting studentNum using if-else chains is the only way, or using an std::map but that might be overkill for the situation.

But besides the switch statement,
 
temp = studentNum(classLevel); 

classLevel is an array of std::strings, but your studentNum function expects a string. Perhaps you meant to pass classLevel[i]?
Last edited on
Thank you for clearing that up
Topic archived. No new replies allowed.