Question: Hi, could u explain these functions. let me know why my old function doesn't work and please rewrite the correct function with instances of istream

Hi, could u explain these functions. let me know why my old function doesn't work and please rewrite the correct function with instances of istream and ostream?

void Stats::saveAs(const char* fileName) const

{

if (m_filename == nullptr);

ifstream f(m_filename);

ofstream fout(fileName);

string line;

while (getline(f, line))

{

fout << line;

}

f.close();

}

void Stats::loadNums() {

m_noOfNums = 0;

ifstream file(m_filename);

if (file.good())

{

string line;

getline(file, line);

stringstream ss(line);

double n;

string num;

while (getline(ss, num, ',')) m_noOfNums ++;

m_arr = new double[m_noOfNums];

stringstream ss2(line);

int idx = 0;

while (getline(ss2, num, ','))

{

n = stod(num);

m_arr[idx] = n;

idx++;

}

file.close();

}

}

old function

void Stats::setNoOfNumbers() {

ifstream f(m_filename);

int m_noOfNums= 0;

string num;

if (f.is_open()) {

while (getline(f, num, ',')) ++m_noOfNums;

}

if (m_noOfNums== 0) {

delete[] m_filename;

m_filename = nullptr;

}

}

void Stats::loadNums() {

if (m_filename) {

int i(0);

delete[] m_arr;

m_arr = nullptr;

m_arr = new double[m_noOfNums];

//string temp;

double temp;

ifstream f(m_filename);

while (f >> temp) {//extraction operator

f.ignore();

m_arr[i] = temp;

i++;

}

/*while (getline(f, temp, ','))

{

m_arr[i] = temp;

++i;

}*/

m_noOfNums= i;

}

}

explanation of module

This module should be able to load the numbers and perform the following tasks.

  • AStatsobject should be able to display the numbers in a tabular format on ostream in a specified number of columns, column width and precision.
  • AStatsobject should be able to display the numbers that fall within a specific range and the number of their occurrence.
  • AStatsobject should be able to sort the numbers in an ascending or descending order.
  • Copying aStatsobject should be done safely and doing so it should also copy the data file on the hard disk. The new file name should be the same as the original file with an added"C_"prefix.
  • Assigning aStatsobject to another should be done safely and the content of the target file should also be overwritten by the source file.
  • AStatsobject should also make the numbers available to the user program by indexing like an array.
  • AStatsobject should be able to receive the data file name from the istream and load the data from the file on the hard drive.

my class

class Stats {

double* m_arr;//dma array holding doubles

unsigned m_noOfNums{0};// number of numbers in each file

char* m_filename;//name of csv file

unsigned m_columnWidth;

unsigned m_noOfColumns;

unsigned m_precision;

public:

Stats(unsigned columnWidth = 15, unsigned noOfColumns = 4, unsigned precision = 2);

Stats(const char* filename, unsigned columnWidth = 15, unsigned noOfColumns = 4,

...........

}'

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!