So basically I worked really hard on this code in my CS150 class and now I need to change it to
a class, and I thought OK that\'s simple enough, but I cant seem to figure it out at all! I know I
can just make all of the variables inside the class public, but that\'s not what my professor wants.
I am supposed to implement the private variables. I tried making the \"letter\" and \"letterCount\"
variables private and I created some void functions under the public part, I think I might have
been on the right track with that but I wasn\'t sure how to modify the rest of my code in order to
get it to run. Here\'s the original code, still in the struct format. Any help would be GREATLY
appreciated.
P.S. my professor said \"make the member variables private, and impliment functions to manage
those private variables, such as getter and setter functions.\" so that might help you understand
whats expected. Thanks!
I NEED TO GET RID OF THE STRUCT ALLTOGETHER!
my class should be set up like this:
class
{
private:
letter
letterCount
public:
//functions to controll the private variables such as...
void getletter
void getlettercount
}
I just dont know how to impliment that in my code, and what to write in those functions in the
class. Thanks for your help!
[code]
#include
#include
#include
#include
using namespace std;
//Struct definition
struct letterType
{
char letter;
int letterCount;
};
//Function to open I/O files
void openFile(ifstream& inFile, ofstream& outFile);
//Function to fill the array of structs and keep track of the amount of uppercase/lowercase letters
void count(ifstream& inFile, letterType letterList[], int& totalBig, int& totalSmall);
//Function to print the letters, the occurences, and the percentages
void printResult(ofstream& outFile, letterType letterList[], int totalBig, int totalSmall);
//Function to create an output file that contains programmer info
void Info (ofstream& outputFile);
int main()
{
ofstream fout;
ifstream input; //object to read the text
ofstream output; //object to write the results
int totalCapital = 0; //variable to store the total number of uppercase
int totalLower = 0; //variable to store the total number of lowercase
letterType letterObj[52]; //array of structs of type letterType to hold the information
//Input and process data
Info (fout);
openFile(input, output);
count(input, letterObj, totalCapital, totalLower);
printResult(output, letterObj, totalCapital, totalLower);
//Close files
input.close();
output.close();
return 0;
}
void openFile(ifstream& inFile, ofstream& outFile)
{
string inFileName;
string outFileName;
cout << \"Enter the name of the input file: \";
cin >> inFileName;
inFile.open(inFileName.c_str());
cout << endl;
cout << \"Enter the name of the output file: \";
cin >> outFileName;
outFile.open(outFileName.c_str());
cout << endl;
}
void count(ifstream& inFile, letterType letterList[], int& totalBig, int& totalSmall)
{
char ch;
//Loop to initialize the array of structs; set letterCount to zero
for(.
Fostering Friendships - Enhancing Social Bonds in the Classroom
So basically I worked really hard on this code in my CS150 class and.pdf
1. So basically I worked really hard on this code in my CS150 class and now I need to change it to
a class, and I thought OK that's simple enough, but I cant seem to figure it out at all! I know I
can just make all of the variables inside the class public, but that's not what my professor wants.
I am supposed to implement the private variables. I tried making the "letter" and "letterCount"
variables private and I created some void functions under the public part, I think I might have
been on the right track with that but I wasn't sure how to modify the rest of my code in order to
get it to run. Here's the original code, still in the struct format. Any help would be GREATLY
appreciated.
P.S. my professor said "make the member variables private, and impliment functions to manage
those private variables, such as getter and setter functions." so that might help you understand
whats expected. Thanks!
I NEED TO GET RID OF THE STRUCT ALLTOGETHER!
my class should be set up like this:
class
{
private:
letter
letterCount
public:
//functions to controll the private variables such as...
void getletter
void getlettercount
}
I just dont know how to impliment that in my code, and what to write in those functions in the
class. Thanks for your help!
[code]
#include
#include
#include
#include
using namespace std;
//Struct definition
struct letterType
2. {
char letter;
int letterCount;
};
//Function to open I/O files
void openFile(ifstream& inFile, ofstream& outFile);
//Function to fill the array of structs and keep track of the amount of uppercase/lowercase letters
void count(ifstream& inFile, letterType letterList[], int& totalBig, int& totalSmall);
//Function to print the letters, the occurences, and the percentages
void printResult(ofstream& outFile, letterType letterList[], int totalBig, int totalSmall);
//Function to create an output file that contains programmer info
void Info (ofstream& outputFile);
int main()
{
ofstream fout;
ifstream input; //object to read the text
ofstream output; //object to write the results
int totalCapital = 0; //variable to store the total number of uppercase
int totalLower = 0; //variable to store the total number of lowercase
letterType letterObj[52]; //array of structs of type letterType to hold the information
//Input and process data
Info (fout);
openFile(input, output);
count(input, letterObj, totalCapital, totalLower);
printResult(output, letterObj, totalCapital, totalLower);
//Close files
input.close();
output.close();
return 0;
3. }
void openFile(ifstream& inFile, ofstream& outFile)
{
string inFileName;
string outFileName;
cout << "Enter the name of the input file: ";
cin >> inFileName;
inFile.open(inFileName.c_str());
cout << endl;
cout << "Enter the name of the output file: ";
cin >> outFileName;
outFile.open(outFileName.c_str());
cout << endl;
}
void count(ifstream& inFile, letterType letterList[], int& totalBig, int& totalSmall)
{
char ch;
//Loop to initialize the array of structs; set letterCount to zero
for(int index = 0; index < 26; index++)
{
//This segment sets the uppercase letters
letterList[index].letter = static_cast(65 + index);
letterList[index].letterCount = 0;
//This segment sets the lowercase letters
letterList[index + 26].letter = static_cast(97 + index);
letterList[index + 26].letterCount = 0;
}
//read first character
inFile >> ch;
4. //Keep reading until end of file is reached
while(!inFile.eof())
{
//If uppercase letter or lowercase letter is found, update data
if('A' <= ch && ch <= 'Z')
{
letterList[static_cast(ch) - 65].letterCount++;
totalBig++;
}
else if('a' <= ch && ch <= 'z')
{
letterList[static_cast(ch) - 71].letterCount++;
totalSmall++;
}
//read the next character
inFile >> ch;
} //end while
} //end function
void printResult(ofstream& outFile, letterType letterList[], int totalBig, int totalSmall)
{
outFile << fixed << showpoint << setprecision(2);
outFile << "Letter Occurences Percentage" << endl;
for(int index = 0; index < 52; index++)
{
if(0 <= index && index <= 25)
{
outFile << setw(4) << letterList[index].letter << setw(12) << letterList[index].letterCount << "
";
outFile << setw(15)<< 100 * (letterList[index].letterCount /
(static_cast(totalBig)+static_cast(totalSmall))) << "%" << endl;
5. }
else
{
outFile << setw(4) << letterList[index].letter << setw(12) << letterList[index].letterCount << "
";
outFile << setw(15) << 100 * (letterList[index].letterCount /
(static_cast(totalBig)+static_cast(totalSmall))) << "%" << endl;
}
} //end for
} //end
void Info (ofstream& outputFile)
{
string outputFileName;
cout << "Enter your name: ";
cin >> outputFileName;
outputFileName = outputFileName+"_p4.txt";
outputFile.open(outputFileName.c_str());
outputFile << "Programmer name: Bryce Messer LAB CRN: 10342";
outputFile << " This program is a collection of variable declarations";
outputFile << " and function calls. This program reads a text and outputs";
outputFile << " the letters, together with their counts, as explained below";
outputFile << " in the function printResult.";
outputFile << " The program consists of the following four functions: ";
outputFile << " **Function openFile: Opens the input and output files. You must pass the";
outputFile << " file streams as parameters (by reference, of course). If the file does not";
outputFile << " exist, the program should print an appropriate message and exit. The";
outputFile << " program must ask the user for the names of the input and output files.";
outputFile << " **Function count: counts every occurrence of capital letters A-Z and";
outputFile << " small letters a-z in the text file opened in the function openFile. This";
outputFile << " information must go into an array of structures. The array must be passed";
6. outputFile << " as a parameter, and the file identifier must also be passed as a parameter.";
outputFile << " **Function printResult: Prints the number of capital letters and small";
outputFile << " letters, as well as the percentage of capital letters for every letter A-Z and";
outputFile << " the percentage of small letters for every letter a-z. The percentages";
outputFile << " should look like this: ‘‘25%’’. This information must come from an array";
outputFile << " of structures, and this array must be passed as a parameter.";
outputFile << " **Function Info: Creates an output file named 'yourName_P4.txt'";
outputFile << " that contains all your required programmer info, along with";
outputFile << " TA name,..) a description of the program ( what it does, and how),";
outputFile << " and how you approached solving it. You must include your algorithm that";
outputFile << " you wrote, including the steps and any refinements made.";
outputFile << " For Project 5, create an output file named 'yourName_P5.txt.'";
outputFile << " ";
cout << endl;
}
[/code]
Solution
Here we are using the concept of this pointer of C++.
I have defined four functions
1) getletter()
2)setletter()
3)getlettercount()
4)setlettercount()
I dont know the name of the input file. So not able to run the code.I gave the random file number
of mine and it executed without any trouble
Please check and let me know for any quieries you have.
#include
#include
#include
#include
using namespace std;
7. class letterType
{
private:
char letter;
int letterCount;
public:
//functions to controll the private variables such as...
void setletter(char ch);
void setlettercount(int lcount);
char getletter();
int getlettercount();
};
void letterType::setletter(char ch)
{
this->letter = ch;
}
void letterType::setlettercount(int _letterCount)
{
this->letterCount = _letterCount;
}
char letterType::getletter()
{
return this->letter;
}
int letterType::getlettercount()
{
return this->letterCount;
}
//Function to open I/O files
void openFile(ifstream& inFile, ofstream& outFile);
//Function to fill the array of structs and keep track of the amount of uppercase/lowercase letters
void count(ifstream& inFile, letterType letterList[], int& totalBig, int& totalSmall);
//Function to print the letters, the occurences, and the percentages
void printResult(ofstream& outFile, letterType letterList[], int totalBig, int totalSmall);
//Function to create an output file that contains programmer info
void Info (ofstream& outputFile);
8. int main()
{
ofstream fout;
ifstream input; //object to read the text
ofstream output; //object to write the results
int totalCapital = 0; //variable to store the total number of uppercase
int totalLower = 0; //variable to store the total number of lowercase
letterType letterObj[52]; //array of structs of type letterType to hold the information
//Input and process data
Info (fout);
openFile(input, output);
count(input, letterObj, totalCapital, totalLower);
printResult(output, letterObj, totalCapital, totalLower);
//Close files
input.close();
output.close();
return 0;
}
void openFile(ifstream& inFile, ofstream& outFile)
{
string inFileName;
string outFileName;
cout << "Enter the name of the input file: ";
cin >> inFileName;
inFile.open(inFileName.c_str());
cout << endl;
cout << "Enter the name of the output file: ";
cin >> outFileName;
outFile.open(outFileName.c_str());
cout << endl;
}
void count(ifstream& inFile, letterType letterList[], int& totalBig, int& totalSmall)
{
char ch;
//Loop to initialize the array of structs; set letterCount to zero
for(int index = 0; index < 26; index++)
9. {
//This segment sets the uppercase letters
letterList[index].setletter(static_cast(65 + index));
letterList[index].setlettercount(0);
//This segment sets the lowercase letters
letterList[index + 26].setletter(static_cast(97 + index));
letterList[index + 26].setlettercount(0);
}
//read first character
inFile >> ch;
//Keep reading until end of file is reached
while(!inFile.eof())
{
//If uppercase letter or lowercase letter is found, update data
if('A' <= ch && ch <= 'Z')
{
letterList[static_cast(ch) - 65].setlettercount(letterList[static_cast(ch) - 65].getlettercount() + 1);
totalBig++;
}
else if('a' <= ch && ch <= 'z')
{
letterList[static_cast(ch) - 71].setlettercount(letterList[static_cast(ch) - 71].getlettercount() + 1);
totalSmall++;
}
//read the next character
inFile >> ch;
} //end while
} //end function
void printResult(ofstream& outFile, letterType letterList[], int totalBig, int totalSmall)
{
outFile << fixed << showpoint << setprecision(2);
outFile << "Letter Occurences Percentage" << endl;
for(int index = 0; index < 52; index++)
{
if(0 <= index && index <= 25)
{
10. outFile << setw(4) << letterList[index].getletter() << setw(12) <<
letterList[index].getlettercount() << " ";
outFile << setw(15)<< 100 * (letterList[index].getlettercount() /
(static_cast(totalBig)+static_cast(totalSmall))) << "%" << endl;
}
else
{
outFile << setw(4) << letterList[index].getlettercount() << setw(12) <<
letterList[index].getlettercount() << " ";
outFile << setw(15) << 100 * (letterList[index].getlettercount() /
(static_cast(totalBig)+static_cast(totalSmall))) << "%" << endl;
}
} //end for
} //end
void Info (ofstream& outputFile)
{
string outputFileName;
cout << "Enter your name: ";
cin >> outputFileName;
outputFileName = outputFileName+"_p4.txt";
outputFile.open(outputFileName.c_str());
outputFile << "Programmer name: Bryce Messer LAB CRN: 10342";
outputFile << " This program is a collection of variable declarations";
outputFile << " and function calls. This program reads a text and outputs";
outputFile << " the letters, together with their counts, as explained below";
outputFile << " in the function printResult.";
outputFile << " The program consists of the following four functions: ";
outputFile << " **Function openFile: Opens the input and output files. You must pass the";
outputFile << " file streams as parameters (by reference, of course). If the file does not";
outputFile << " exist, the program should print an appropriate message and exit. The";
outputFile << " program must ask the user for the names of the input and output files.";
outputFile << " **Function count: counts every occurrence of capital letters A-Z and";
outputFile << " small letters a-z in the text file opened in the function openFile. This";
outputFile << " information must go into an array of structures. The array must be passed";
outputFile << " as a parameter, and the file identifier must also be passed as a parameter.";
11. outputFile << " **Function printResult: Prints the number of capital letters and small";
outputFile << " letters, as well as the percentage of capital letters for every letter A-Z and";
outputFile << " the percentage of small letters for every letter a-z. The percentages";
outputFile << " should look like this: ‘‘25%’’. This information must come from an array";
outputFile << " of structures, and this array must be passed as a parameter.";
outputFile << " **Function Info: Creates an output file named 'yourName_P4.txt'";
outputFile << " that contains all your required programmer info, along with";
outputFile << " TA name,..) a description of the program ( what it does, and how),";
outputFile << " and how you approached solving it. You must include your algorithm that";
outputFile << " you wrote, including the steps and any refinements made.";
outputFile << " For Project 5, create an output file named 'yourName_P5.txt.'";
outputFile << " ";
cout << endl;
}
The ‘this’ pointer is passed as a hidden argument to all nonstatic member function calls and is
available as a local variable within the body of all nonstatic functions. ‘this’ pointer is a constant
pointer that holds the memory address of the current object. ‘this’ pointer is not available in
static member functions as static member functions can be called without any object (with class
name).