Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need to write a C++ program that can read a team roster from a .csv file and c

ID: 3733176 • Letter: I

Question

I need to write a C++ program that can read a team roster from a .csv file and count how many players are NOT from a certain state.

This is what I have but it doesnt work. There is a second part that asks to count all the E's in the file but that part I have working.

#include <iomanip>

#include <iostream>

#include <fstream>

#include <cstdlib>

#include <string>

using namespace std;

players()

{

char count[256]={0};

string line;

char next, ch;

int A,L;

int counter=0;

ifstream inFile;

inFile.open("roster.csv");

if (inFile.fail())

{

cout<<" The file was not successfully opened"<<endl;

exit(1);

}

while (!inFile.eof())//while loop to count letters from opened file

{

inFile.get(next);//grabs lines from file to count

if (ch!="A")counter++;

if (ch!="L")counter++;

}

}

int main()

{

int counter=0;

int letterCount[26]={0};//initializes alphabet to be counted

string filename;//declares string

char next , ch;//declares character names

int e=0;// character to be counted

ifstream inFile;//allows files to be read

inFile.open("roster.csv");//opens comma delimited file

if (inFile.fail())//enables default fail message

{

cout<<" The file was not successfully opened"<<endl;//fail message

exit(1);//tells program to quit on fail

}

while (!inFile.eof())//while loop to count letters from opened file

{

inFile.get(next);//grabs lines from file to count

if (next=='e')e++;//counts the e's in the lines

}

cout<<"E appears "<<e<<" Times"<<endl;//sends to output screen

players();

cout<<"There are "<<counter<<" players from outside Alabama"<<endl;

return 0;

}

Explanation / Answer

/*
I solved your problem in your way in which you are solving.

while (!inFile.eof()){
inFile.get(next);
}

Reads a character at time if your file is comma separated then it will also read comma ,
it will read like character or comma alternatively

so to check if next contains char or comma(,) you can use if-else conditions

like if(next>='A' && next<='Z') means next contains a upper case alphabet

so using above if condition you can avoid character , space, comma, new line etc.

so to counter frequency of each letter you take help of ASCII values

ASCII values of A-Z is 65-92 and ASCII values of a-z is 97-122

To find ASCII value of any character you can type cast that character to int

ex- int a=(int)'A' =65
int b=(int)'B'=66

So if you want to counter frequency of each character in array of size 26

the you need to take ascii value to the range by subtracting appropriate number
for upper case letter you can subtract 65 so
int index=(int)'A'-65=65-65=0

and you can increase count at that index
like count[index] +=1


Sample file content(roster.csv:

A,A,B,C,C,B,G,C,D,S,F,D
V,V,F,W,S,A,F,T,H,Y,J,K
P,Y,T,Q,W,E,A,S,D,F,Z,X
S,C,X,R,T,G,V,D,S,G,H,D
E,R,E,W,E,D,V,F,G,S,R,F

Sample Output:
E appears 4 Times
There are 56 players from outside Alabama

*/

//Please copy answer from below line to the end of answer to your .cpp file

#include <iomanip>

#include <iostream>

#include <fstream>

#include <cstdlib>

#include <string>

using namespace std;

int players()

{

char count[26]={0};

string line;

char next, ch;

int A,L;

int counter=0;

ifstream inFile;

inFile.open("roster.csv");

if (inFile.fail())

{

cout<<" The file was not successfully opened"<<endl;

exit(1);

}

while (!inFile.eof())//while loop to count letters from opened file

{

inFile.get(next);//grabs lines from file to count

//getting ascii value of character
int ascii=(int)next;

if(ascii>=65 && ascii<=90)
{
counter++;

int index=ascii-65;
//Incrementing index at ascii index
count[index] +=1;
}


}

//finding count at index of A and L
int a=(int)'A'-65;
int l=(int)'L'-65;

//Exclude count of A and L from total count of all letters from A to Z
counter=counter -(count[a]+count[l]);

//returning count to main function
return counter;

}
int main()

{


int letterCount[26]={0};//initializes alphabet to be counted

string filename;//declares string

char next , ch;//declares character names

int e=0;// character to be counted

ifstream inFile;//allows files to be read

inFile.open("roster.csv");//opens comma delimited file

if (inFile.fail())//enables default fail message

{

cout<<" The file was not successfully opened"<<endl;//fail message

exit(1);//tells program to quit on fail

}

while (!inFile.eof())//while loop to count frequency of all letters from opened file

{

inFile.get(next);//grabs lines from file to count

//finding ascii value of char next
int ascii=(int)next;

//checking if ascii value of character A-Z
//ASCII value of A=65 and Z=90
if(ascii>=65 && ascii<=90)
{
//Create array index from ascii value by subtracting 65
int index=ascii-65;

//Increment index count at index
letterCount[index] =letterCount[index]+1;
}


}
//closing file connection
inFile.close();

//finding index of E using ascii value
e=(int)'E' -65;


//Fetching total count of E from index 4 of letterCount array
cout<<"E appears "<<letterCount[e]<<" Times"<<endl;//sends to output screen

//Calling players function to counter player from out sise alabama

cout<<"There are "<<players()<<" players from outside Alabama"<<endl;

return 0;

}