I need help with a C++ program. The program instructions are as follows: 1. Impl
ID: 3668920 • Letter: I
Question
I need help with a C++ program.
The program instructions are as follows:
1. Implement a class named Age. Class Age has the method ReadFile to read the file input.txt. The values in the file contains in each line: first name, last name, age.
2. Create in the Class Age a method named CatchAge used to extract only the age from each line from the input.txt file.
3. In Class Age, create the method ReckonAge, used to get the sum and the average of all the ages found in the input file.
4. In Class Age, creathe the method Output used to print on the screen and also into the file output.txt each of the ages found in the input.txt and also the total sum of all ages and average age. Then make the greetings: (average <= 50) "YOUNG GROUP", (average >50 and <100) "GOOD AGE GROUP", (average >= 100) "TELL THE SECRET".
So far here is what I have:
#include <iostream>
#include <fstream>
using namespace std;
class Age
{
public:
readFile(string firstName, string lastName, int age)
{
ifstream reader;
reader.open("input.txt");
reader >> firstName >> lastName >> age;
reader.close();
}
catchAge(int age)
{
}
reckonAge(int sum, int average)
{
}
output()
{
ofstream writer;
writer.open("output.txt")
writer <<
int average;
if (average <= 50)
{
cout << "YOUNG GROUP" << endl;
}
else if (average > 50 && average < 100)
{
cout << "GOOD AGE GROUP" << endl;
}
else
{
cout << "TELL THE SECRET" << endl;
}
}
};
int main()
{
return 0;
}
Explanation / Answer
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <cstring>
#include <vector>
#include <cstdlib>
using namespace std;
int sum=0;
float avg=0;
int count=0;
class Age
{
public:
void readFile()
{
ifstream iFile("/home/shekhar/Desktop/chegg/CS/cpp/fileHandling/StudentRecords");
string line;
while(getline(iFile, line))
{
string buff;
stringstream ss(line);
cout<<" ";
count++;
while(ss>>buff)
{
cout<<buff<<' ';
}
}
iFile.close();
}
void catchAge()
{
ifstream iFile("/home/shekhar/Desktop/chegg/CS/cpp/fileHandling/StudentRecords");
string line;
while(getline(iFile, line))
{
string buff;
stringstream ss(line);
vector<string> tokens;
while(ss>>buff)
{
tokens.push_back(buff);
}
cout<<"Age is "<<tokens[2]<<" ";
sum+=atoi(tokens[2].c_str());
}
iFile.close();
}
void reckonAge()
{
cout<<" Sum is "<<sum;
avg=sum/count;
cout<<" Average is"<<avg;
}
void output()
{
if (avg <= 50)
{
cout << " YOUNG GROUP" << endl;
}
else if (avg > 50 && avg < 100)
{
cout << " GOOD AGE GROUP" << endl;
}
else
{
cout << " TELL THE SECRET" << endl;
}
}
};
int main()
{
Age a;
a.readFile();
a.catchAge();
a.reckonAge();
a.output();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.