SlideShare a Scribd company logo
1 of 11
Download to read offline
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(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;
//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;
}
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";
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;
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);
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++)
{
//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)
{
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.";
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).

More Related Content

Similar to So basically I worked really hard on this code in my CS150 class and.pdf

C-Program Custom Library, Header File, and Implementation FilesI .pdf
C-Program Custom Library, Header File, and Implementation FilesI .pdfC-Program Custom Library, Header File, and Implementation FilesI .pdf
C-Program Custom Library, Header File, and Implementation FilesI .pdf
herminaherman
 
JavaScript and jQuery Fundamentals
JavaScript and jQuery FundamentalsJavaScript and jQuery Fundamentals
JavaScript and jQuery Fundamentals
BG Java EE Course
 
Data structures and algorithms lab1
Data structures and algorithms lab1Data structures and algorithms lab1
Data structures and algorithms lab1
Bianca Teşilă
 
IN C LANGUAGE- I've been trying to finish this program for the last fe.docx
IN C LANGUAGE- I've been trying to finish this program for the last fe.docxIN C LANGUAGE- I've been trying to finish this program for the last fe.docx
IN C LANGUAGE- I've been trying to finish this program for the last fe.docx
GordonpACKellyb
 
Falcon初印象
Falcon初印象Falcon初印象
Falcon初印象
勇浩 赖
 
Php Reusing Code And Writing Functions
Php Reusing Code And Writing FunctionsPhp Reusing Code And Writing Functions
Php Reusing Code And Writing Functions
mussawir20
 
Gift-VT Tools Development Overview
Gift-VT Tools Development OverviewGift-VT Tools Development Overview
Gift-VT Tools Development Overview
stn_tkiller
 
Javascript part1
Javascript part1Javascript part1
Javascript part1
Raghu nath
 
So I am writing a CS code for a project and I keep getting cannot .pdf
So I am writing a CS code for a project and I keep getting cannot .pdfSo I am writing a CS code for a project and I keep getting cannot .pdf
So I am writing a CS code for a project and I keep getting cannot .pdf
ezonesolutions
 
Input output functions
Input output functionsInput output functions
Input output functions
hyderali123
 
#include iostream #include cstring #include vector #i.pdf
 #include iostream #include cstring #include vector #i.pdf #include iostream #include cstring #include vector #i.pdf
#include iostream #include cstring #include vector #i.pdf
anandatalapatra
 
For this homework, you will write a program to create and manipulate.pdf
For this homework, you will write a program to create and manipulate.pdfFor this homework, you will write a program to create and manipulate.pdf
For this homework, you will write a program to create and manipulate.pdf
herminaherman
 

Similar to So basically I worked really hard on this code in my CS150 class and.pdf (20)

C-Program Custom Library, Header File, and Implementation FilesI .pdf
C-Program Custom Library, Header File, and Implementation FilesI .pdfC-Program Custom Library, Header File, and Implementation FilesI .pdf
C-Program Custom Library, Header File, and Implementation FilesI .pdf
 
JavaScript and jQuery Fundamentals
JavaScript and jQuery FundamentalsJavaScript and jQuery Fundamentals
JavaScript and jQuery Fundamentals
 
Esoft Metro Campus - Certificate in c / c++ programming
Esoft Metro Campus - Certificate in c / c++ programmingEsoft Metro Campus - Certificate in c / c++ programming
Esoft Metro Campus - Certificate in c / c++ programming
 
Data structures and algorithms lab1
Data structures and algorithms lab1Data structures and algorithms lab1
Data structures and algorithms lab1
 
IN C LANGUAGE- I've been trying to finish this program for the last fe.docx
IN C LANGUAGE- I've been trying to finish this program for the last fe.docxIN C LANGUAGE- I've been trying to finish this program for the last fe.docx
IN C LANGUAGE- I've been trying to finish this program for the last fe.docx
 
Falcon初印象
Falcon初印象Falcon初印象
Falcon初印象
 
Php Reusing Code And Writing Functions
Php Reusing Code And Writing FunctionsPhp Reusing Code And Writing Functions
Php Reusing Code And Writing Functions
 
SQL -PHP Tutorial
SQL -PHP TutorialSQL -PHP Tutorial
SQL -PHP Tutorial
 
Gift-VT Tools Development Overview
Gift-VT Tools Development OverviewGift-VT Tools Development Overview
Gift-VT Tools Development Overview
 
Javascript part1
Javascript part1Javascript part1
Javascript part1
 
C
CC
C
 
So I am writing a CS code for a project and I keep getting cannot .pdf
So I am writing a CS code for a project and I keep getting cannot .pdfSo I am writing a CS code for a project and I keep getting cannot .pdf
So I am writing a CS code for a project and I keep getting cannot .pdf
 
Unit 5 Foc
Unit 5 FocUnit 5 Foc
Unit 5 Foc
 
Scala 3camp 2011
Scala   3camp 2011Scala   3camp 2011
Scala 3camp 2011
 
Input output functions
Input output functionsInput output functions
Input output functions
 
#include iostream #include cstring #include vector #i.pdf
 #include iostream #include cstring #include vector #i.pdf #include iostream #include cstring #include vector #i.pdf
#include iostream #include cstring #include vector #i.pdf
 
JavaScript
JavaScriptJavaScript
JavaScript
 
For this homework, you will write a program to create and manipulate.pdf
For this homework, you will write a program to create and manipulate.pdfFor this homework, you will write a program to create and manipulate.pdf
For this homework, you will write a program to create and manipulate.pdf
 
C++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdfC++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdf
 
Useful c programs
Useful c programsUseful c programs
Useful c programs
 

More from eyewaregallery

in C++ , Design a linked list class named IntegerList to hold a seri.pdf
in C++ , Design a linked list class named IntegerList to hold a seri.pdfin C++ , Design a linked list class named IntegerList to hold a seri.pdf
in C++ , Design a linked list class named IntegerList to hold a seri.pdf
eyewaregallery
 
I am writing a program that places bolcks on a grid the the sape of .pdf
I am writing a program that places bolcks on a grid the the sape of .pdfI am writing a program that places bolcks on a grid the the sape of .pdf
I am writing a program that places bolcks on a grid the the sape of .pdf
eyewaregallery
 
How do teams bring value to an organizationHow are high performin.pdf
How do teams bring value to an organizationHow are high performin.pdfHow do teams bring value to an organizationHow are high performin.pdf
How do teams bring value to an organizationHow are high performin.pdf
eyewaregallery
 
Gene RegulationFunction of transcription factor domains, i.e. DNA.pdf
Gene RegulationFunction of transcription factor domains, i.e. DNA.pdfGene RegulationFunction of transcription factor domains, i.e. DNA.pdf
Gene RegulationFunction of transcription factor domains, i.e. DNA.pdf
eyewaregallery
 
For each step of DNA replication, predict the outcome ifconcentra.pdf
For each step of DNA replication, predict the outcome ifconcentra.pdfFor each step of DNA replication, predict the outcome ifconcentra.pdf
For each step of DNA replication, predict the outcome ifconcentra.pdf
eyewaregallery
 
Explain the basic relationship between humans and microorganisms.pdf
Explain the basic relationship between humans and microorganisms.pdfExplain the basic relationship between humans and microorganisms.pdf
Explain the basic relationship between humans and microorganisms.pdf
eyewaregallery
 
discuss the rationale for the global harmonization of financial repo.pdf
discuss the rationale for the global harmonization of financial repo.pdfdiscuss the rationale for the global harmonization of financial repo.pdf
discuss the rationale for the global harmonization of financial repo.pdf
eyewaregallery
 
Describe the need to multitask in BBC (behavior-based control) syste.pdf
Describe the need to multitask in BBC (behavior-based control) syste.pdfDescribe the need to multitask in BBC (behavior-based control) syste.pdf
Describe the need to multitask in BBC (behavior-based control) syste.pdf
eyewaregallery
 

More from eyewaregallery (20)

in C++ , Design a linked list class named IntegerList to hold a seri.pdf
in C++ , Design a linked list class named IntegerList to hold a seri.pdfin C++ , Design a linked list class named IntegerList to hold a seri.pdf
in C++ , Design a linked list class named IntegerList to hold a seri.pdf
 
In addition to the fossil record, what other kinds of evidence reinfo.pdf
In addition to the fossil record, what other kinds of evidence reinfo.pdfIn addition to the fossil record, what other kinds of evidence reinfo.pdf
In addition to the fossil record, what other kinds of evidence reinfo.pdf
 
How much 0.521 M NaOH will be needed to raise the pH of 0.161 L of 4.pdf
How much 0.521 M NaOH will be needed to raise the pH of 0.161 L of 4.pdfHow much 0.521 M NaOH will be needed to raise the pH of 0.161 L of 4.pdf
How much 0.521 M NaOH will be needed to raise the pH of 0.161 L of 4.pdf
 
I am writing a program that places bolcks on a grid the the sape of .pdf
I am writing a program that places bolcks on a grid the the sape of .pdfI am writing a program that places bolcks on a grid the the sape of .pdf
I am writing a program that places bolcks on a grid the the sape of .pdf
 
How do teams bring value to an organizationHow are high performin.pdf
How do teams bring value to an organizationHow are high performin.pdfHow do teams bring value to an organizationHow are high performin.pdf
How do teams bring value to an organizationHow are high performin.pdf
 
how can we use empricism to figure out if a fact is trueSolutio.pdf
how can we use empricism to figure out if a fact is trueSolutio.pdfhow can we use empricism to figure out if a fact is trueSolutio.pdf
how can we use empricism to figure out if a fact is trueSolutio.pdf
 
Gene RegulationFunction of transcription factor domains, i.e. DNA.pdf
Gene RegulationFunction of transcription factor domains, i.e. DNA.pdfGene RegulationFunction of transcription factor domains, i.e. DNA.pdf
Gene RegulationFunction of transcription factor domains, i.e. DNA.pdf
 
For each step of DNA replication, predict the outcome ifconcentra.pdf
For each step of DNA replication, predict the outcome ifconcentra.pdfFor each step of DNA replication, predict the outcome ifconcentra.pdf
For each step of DNA replication, predict the outcome ifconcentra.pdf
 
Explain THREE unique characteristics of fungi.SolutionThree un.pdf
Explain THREE unique characteristics of fungi.SolutionThree un.pdfExplain THREE unique characteristics of fungi.SolutionThree un.pdf
Explain THREE unique characteristics of fungi.SolutionThree un.pdf
 
Explain the basic relationship between humans and microorganisms.pdf
Explain the basic relationship between humans and microorganisms.pdfExplain the basic relationship between humans and microorganisms.pdf
Explain the basic relationship between humans and microorganisms.pdf
 
discuss the rationale for the global harmonization of financial repo.pdf
discuss the rationale for the global harmonization of financial repo.pdfdiscuss the rationale for the global harmonization of financial repo.pdf
discuss the rationale for the global harmonization of financial repo.pdf
 
Describe the need to multitask in BBC (behavior-based control) syste.pdf
Describe the need to multitask in BBC (behavior-based control) syste.pdfDescribe the need to multitask in BBC (behavior-based control) syste.pdf
Describe the need to multitask in BBC (behavior-based control) syste.pdf
 
A spinner with 7 equally sized slices is shown below. The dial is spu.pdf
A spinner with 7 equally sized slices is shown below. The dial is spu.pdfA spinner with 7 equally sized slices is shown below. The dial is spu.pdf
A spinner with 7 equally sized slices is shown below. The dial is spu.pdf
 
ble below to calculate the Gross Domestic Product uning the ral Gro.pdf
ble below to calculate the Gross Domestic Product uning the ral Gro.pdfble below to calculate the Gross Domestic Product uning the ral Gro.pdf
ble below to calculate the Gross Domestic Product uning the ral Gro.pdf
 
1.The limbic systemA. matures before the prefrontal cortexB. is.pdf
1.The limbic systemA. matures before the prefrontal cortexB. is.pdf1.The limbic systemA. matures before the prefrontal cortexB. is.pdf
1.The limbic systemA. matures before the prefrontal cortexB. is.pdf
 
Why is leadership a critical factor in implementing change and qua.pdf
Why is leadership a critical factor in implementing change and qua.pdfWhy is leadership a critical factor in implementing change and qua.pdf
Why is leadership a critical factor in implementing change and qua.pdf
 
Why are there bubbles in beer and champagneSolution Bubbles of .pdf
Why are there bubbles in beer and champagneSolution  Bubbles of .pdfWhy are there bubbles in beer and champagneSolution  Bubbles of .pdf
Why are there bubbles in beer and champagneSolution Bubbles of .pdf
 
Which of the following is true of a traditional master budget a. It.pdf
Which of the following is true of a traditional master budget a. It.pdfWhich of the following is true of a traditional master budget a. It.pdf
Which of the following is true of a traditional master budget a. It.pdf
 
Which ethnic groups were a part of the former YugoslaviaCroats, S.pdf
Which ethnic groups were a part of the former YugoslaviaCroats, S.pdfWhich ethnic groups were a part of the former YugoslaviaCroats, S.pdf
Which ethnic groups were a part of the former YugoslaviaCroats, S.pdf
 
What would the components of the development be in an early learning.pdf
What would the components of the development be in an early learning.pdfWhat would the components of the development be in an early learning.pdf
What would the components of the development be in an early learning.pdf
 

Recently uploaded

Recently uploaded (20)

Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
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).