Here is the problem I'm working on
Your task here is to write the definition of a function called getElectoralVotes . The function receives a two-character string (like "NY") and returns the number of electoral votes the indicated state has.
The function accomplishes this by doing the following:
open up the ev.txt file
read the state string and votes integer in each line in that file
when it finds a state in the file that matches its parameter (the state abbreviation) it returns the integer from that line in the file
if it does not find a match it returns 0
finally, the function should close the file when it is finished using it
So, if the ev.txt file contained:
NJ 8
NM 3
NY 32
then
getElectoralVotes("NJ") would return the value 8
getElectoralVotes("NY") would return the value 32, and
getElectoralVotes("ON") would return the value 0
Here is what I have so far: (Not looking for answers just direction)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
int getElectoralVotes(string state){
ifstream evFile("ev.txt");
string Fstate;
int votes;
evFile>>Fstate;
evFile>>votes;
cin>>state;
while (!evFile.fail()) {
if(Fstate==state){
return votes;
evFile>>Fstate;
}
else if(!(Fstate==state)){
return 0;
evFile>>Fstate;
}
evFile.close();
}
}
|
ERROR Message:
Remarks:
⇒ Fails on AK returning 0 instead of 3
⇒ Your code had an error during execution