There is a main.cpp file and a search.cpp file. I cannot alter anything in the m
ID: 3592161 • Letter: T
Question
There is a main.cpp file and a search.cpp file. I cannot alter anything in the main.cpp code so i need the code for the search.cpp in C++14. thank you.
/*main.cpp*/
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
//
// function declarations:
//
int GetFileStats(string filename, string& firstKey, string& firstValue);
bool SearchForKey(string filename, string key, string& value, int& rank);
//
// main:
//
int main()
{
string filename;
cin >> filename;
cout << "**Starting**" << endl;
cout << "File: '" << filename << "'" << endl;
cout << endl;
//
// First, let's get some statistics about the data file:
//
int N, rank;
string key, value;
bool found;
N = GetFileStats(filename, key, value);
cout << "# of entries: " << N << endl;
cout << "Top-ranked: (" << key << ", " << value << ")" << endl;
cout << endl;
//
// Now let's input keys from the keyboard and look for them in the file:
//
cin >> key;
while (key != "#")
{
found = SearchForKey(filename, key, value, rank);
if (found == true)
{
cout << ">>Found key: " << key << endl;
cout << " Value= " << value << endl;
cout << " Rank= " << rank << endl;
}
else // not found:
{
cout << ">>Not found: " << key << endl;
}
cin >> key;
}
cout << endl;
cout << "**End**" << endl;
return 0;
}
/*search.cpp*/
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
//
// GetFileStats
//
// Returns the # of (key, value) pairs in the given file. Also returns the first (key, value)
// pair via the parameters firstKey and firstValue.
//
// NOTE: it is assumed the file contains at least one (key, value) pair, in this format:
//
// value key
// value key
// value key
// # #
//
// where value and key are one word strings (or numbers that will be input as strings).
// If the file cannot be opened, the program will be exited.
//
int GetFileStats(string filename, string& firstKey, string& firstValue)
{
//
// Initialize return value / parameters:
//
firstKey = "?";
firstValue = "?";
int N = 0;
//
// TODO: open and read the (key, value) pairs from the file. The goal is
// to simply count how many pairs there are and return this value N. Also,
// we need to return the first (key, value) pair via the firstKey and
// firstValue parameters; just assign to the parameters to "return" the
// values. Don't forget to close the file when you're done.
//
return N;
}
Explanation / Answer
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
//
// function declarations:
//
int GetFileStats(string filename, string& firstKey, string& firstValue);
bool SearchForKey(string filename, string key, string& value, int& rank);
//
// main:
//
int main()
{
string filename;
cin >> filename;
cout << "**Starting**" << endl;
cout << "File: '" << filename << "'" << endl;
cout << endl;
//
// First, let's get some statistics about the data file:
//
int N, rank;
string key, value;
bool found;
N = GetFileStats(filename, key, value);
cout << "# of entries: " << N << endl;
cout << "Top-ranked: (" << key << ", " << value << ")" << endl;
cout << endl;
//
// Now let's input keys from the keyboard and look for them in the file:
//
cin >> key;
while (key != "#")
{
found = SearchForKey(filename, key, value, rank);
if (found == true)
{
cout << ">>Found key: " << key << endl;
cout << " Value= " << value << endl;
cout << " Rank= " << rank << endl;
}
else // not found:
{
cout << ">>Not found: " << key << endl;
}
cin >> key;
}
cout << endl;
cout << "**End**" << endl;
return 0;
}
/*search.cpp*/
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
//
// GetFileStats
//
// Returns the # of (key, value) pairs in the given file. Also returns the first (key, value)
// pair via the parameters firstKey and firstValue.
//
// NOTE: it is assumed the file contains at least one (key, value) pair, in this format:
//
// value key
// value key
// value key
// # #
//
// where value and key are one word strings (or numbers that will be input as strings).
// If the file cannot be opened, the program will be exited.
//
int GetFileStats(string filename, string& firstKey, string& firstValue)
{
//
// Initialize return value / parameters:
//
firstKey = "?";
firstValue = "?";
int N = 0;
//
// TODO: open and read the (key, value) pairs from the file. The goal is
// to simply count how many pairs there are and return this value N. Also,
// we need to return the first (key, value) pair via the firstKey and
// firstValue parameters; just assign to the parameters to "return" the
// values. Don't forget to close the file when you're done.
//
return N;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.