Hi, I don't know if anyone can help but I'm trying to figure out this switch case problem for my read file function. This is just the read file portion. I'm reading in a text file (bottom), and at this point I'm trying to determine electoral votes based on who had the most popular votes. Any help would be much appreciated.

Example. If the state of "DO" recorded 1180 for Popular v. 1 and 1100 for Popular v.2, then 15 electoral votes goes to winner of Popular v. I would have to then place those votes under the appropriate column.

//* ~~~~~~~~~~~~~~~~~~~READ FILE FUNCTION~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ *//

void ReadFile(ifstream& infile, char& stCode1, char& stCode2, int& popVote1, int& popVote2, int& count1, int& count2)
{

//int	state, Alabama;
int		elect_votes = 0;


	infile >> stCode1 >> stCode2 >> popVote1 >> popVote2;
	//cout << stCode1 << stCode2 << popVote1 << popVote2 << endl;
	
	cout << stCode1 << stCode2;
	cout << "                  ";
	cout << popVote1;
	cout << "           ";
	cout << popVote2 << endl;
[B]
char state_name[2];				// switch expression of type 'char [2]' is illegal, integral expression required


state_name[0] = stCode1;
state_name[1] = stCode2;

switch(state_name)
	{
	case 1: ("AB");
		elect_votes = 20;
	//	break;

	case 2: ("DO");
		elect_votes = 15;
	//	break;
	}

[/B]
/*	switch(stCode1 && stCode2)		//this works sort of
	{
		case 1: ('D' && 'O');
		elect_votes = 15;
		cout << elect_votes;
		//cout << "Alabama";
		//break;

		case 2: ('M' && 'A');
		elect_votes = 13;
		cout << elect_votes;
		//break;

		/*case 3: ('O' && 'R');
		elect_votes = 9;
		cout << elect_votes;

		case 4: ('N' && 'S');
		elect_votes = 7;
		cout << elect_votes; 
	}	*/



	
	count1 = count1 + popVote1;
	count2 = count2 + popVote2;

	if (count1 > count2)
	{
	count1 = count1 + elect_votes;
	//cout << count1;
	}
	else
	{
	count2 = count2 + elect_votes;
	//cout << count2;
	}
	
	if (stCode1 == 'A')
		cout << "       Alabama\n";
	
	if (stCode1 == 'R')
		cout << "       Rhode Island\n";
	
	
		

	
}

input file

DO 1180 1100
MA 700 643
OR 700 1000
NS 445 678
IO 620 614
MT 2325 2325
ND 232 444
WH 899 2122
NP 100 800
AB 1001 950
WP 78 75
OY 22 30
MM 115 613
PE 113 222
NW 21 88
RA 7 15
OK 200 200
DW 200 500

This is what it should look like in the end...


State State Pop Vote Pop Vote Elect Vote Elect Vote
Code Name for #1 for #2 for #1 for #2
----- ---------- ------- ------- ---------- ----------

DO Dollarado 1180 1100 15 0

MA Mashpotata 700 643 13 0

OR Oreganon 700 1000 0 9

NS New Shoesia 445 678 0 7

IO Iowyou 620 614 12 0

MT Mittengan 2325 2325 10 10

ND New Dealia 232 444 0 8

WH Whinoming 899 2122 0 4

NP North Podunk 100 800 0 4

AB Alabamba 1001 950 10 0

WP West Podunk 78 75 12 0

OY Old York 22 30 0 8

MM Minnymousa 115 613 0 5

PE Penciltucky 113 222 0 7

NW No Way 21 88 0 2

RA Rhode Atlas 7 15 0 1

OK Okeedokee 200 200 5 5

DW Dellawho 200 500 0 5

----- ----------- ------- ------- ---------- ----------

Totals 8958 12419 77 75

char state_name[2];				// switch expression of type 'char [2]' is illegal, integral expression required


state_name[0] = stCode1;
state_name[1] = stCode2;

[B]switch(state_name)[/B]
	{
	case 1: ("AB");
		elect_votes = 20;
	//	break;

	case 2: ("DO");
		elect_votes = 15;
	//	break;
	}

you can't put a character string or array in a switch, but you could put a index of a array or character string in a switch.

This article has been dead for over six months. Start a new discussion instead.