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

I need help with this function its in c++ 11 This is the header file #pragma onc

ID: 3744153 • Letter: I

Question

I need help with this function its in c++ 11

This is the header file

#pragma once

#include <algorithm>
#include <array>
#include <fstream>
#include <functional>
#include <iostream>
#include <sstream>
#include <string>
using std::array;
using std::function;
using std::ifstream;
using std::istringstream;
using std::ostream;
using std::ostringstream;
using std::sort;
using std::string;
using std::stringstream;

enum class RamenSort {BRAND, VARIETY, STYLE, COUNTRY, STARS};

struct RamenRating {
unsigned int reviewNumber;
string brand;
string variety;
string style;
string country;
float stars;

// Default contructor
// Allows creation of "dummy" RamenRatings.
RamenRating () {
reviewNumber = 0;
brand ="Ramenator Brand";
variety = "Ramen";
;
country = "None";
stars = 0.0;
}

// Overloaded operator==
// Allows comparison of two RamenRatings
bool operator== (const RamenRating& r) const {
return (
reviewNumber == r.reviewNumber &&
brand == r.brand &&
variety == r.variety &&
style == r.style &&
country == r.country &&
stars == r.stars
);
}

// Overloaded operator<<
// Allows convenient stream output of RamenRatings
friend ostream& operator<< (ostream& outs, const RamenRating& r) {
outs << "#" << r.reviewNumber << ':'
<< r.brand << ':'
<< r.variety << ':'
<< r.style << ':'
<< r.country << ':'
<< r.stars;
return outs;
}
};

// RamenRating comparison functions -- headers
bool compareBrands (RamenRating l, RamenRating r);
bool compareVarieties (RamenRating l, RamenRating r);
bool compareStyles (RamenRating l, RamenRating r);
bool compareCountries (RamenRating l, RamenRating r);
bool compareStars (RamenRating l, RamenRating r);

// Ramenator class header
class Ramenator {
public:

/*
* Filter the array using the function f. Returned string
* will contain the RamenRatings from array for which function for
* which function f returned true, separated by newlines.
* @ param a function that takes a RamenRating and returns bool
* @param limit if 0, all matches will be returned; if not 0, filter
* will stop at limit matches
* @return a string containing RamenRatings from array for which
* function f returned true, separated by newlines
*/
This is the function --> string filter (function<bool (RamenRating)>& f, unsigned int limit=0);

private:

array<RamenRating, 2500> ratings;

};

Explanation / Answer


Given below is the completed code for the question.
Please do rate the answer if it was helpful. Thank you


#pragma once
#include <algorithm>
#include <array>
#include <fstream>
#include <functional>
#include <iostream>
#include <sstream>
#include <string>
using std::array;
using std::function;
using std::ifstream;
using std::istringstream;
using std::ostream;
using std::ostringstream;
using std::sort;
using std::string;
using std::stringstream;
enum class RamenSort {BRAND, VARIETY, STYLE, COUNTRY, STARS};
struct RamenRating {
unsigned int reviewNumber;
string brand;
string variety;
string style;
string country;
float stars;
// Default contructor
// Allows creation of "dummy" RamenRatings.
RamenRating () {
reviewNumber = 0;
brand ="Ramenator Brand";
variety = "Ramen";
;
country = "None";
stars = 0.0;
}
// Overloaded operator==
// Allows comparison of two RamenRatings
bool operator== (const RamenRating& r) const {
return (
reviewNumber == r.reviewNumber &&
brand == r.brand &&
variety == r.variety &&
style == r.style &&
country == r.country &&
stars == r.stars
);
}
// Overloaded operator<<
// Allows convenient stream output of RamenRatings
friend ostream& operator<< (ostream& outs, const RamenRating& r) {
outs << "#" << r.reviewNumber << ':'
<< r.brand << ':'
<< r.variety << ':'
<< r.style << ':'
<< r.country << ':'
<< r.stars;
return outs;
}
};
// RamenRating comparison functions -- headers
bool compareBrands (RamenRating l, RamenRating r);
bool compareVarieties (RamenRating l, RamenRating r);
bool compareStyles (RamenRating l, RamenRating r);
bool compareCountries (RamenRating l, RamenRating r);
bool compareStars (RamenRating l, RamenRating r);
// Ramenator class header
class Ramenator {
public:
/*
* Filter the array using the function f. Returned string
* will contain the RamenRatings from array for which function for
* which function f returned true, separated by newlines.
* @ param a function that takes a RamenRating and returns bool
* @param limit if 0, all matches will be returned; if not 0, filter
* will stop at limit matches
* @return a string containing RamenRatings from array for which
* function f returned true, separated by newlines
*/
string filter (function<bool (RamenRating)>& f, unsigned int limit=0){
stringstream ss;
for(int i = 0; i < ratings.size(); i++){
if(f(ratings[i])) //call the function using hte ratings[i] and it it returned true, output it to string stream
ss << ratings[i] << std::endl;
}
return ss.str();
}
private:
array<RamenRating, 2500> ratings;
};