for this project, i can only manipulate the code below that is provided but i ca
ID: 3592313 • Letter: F
Question
for this project, i can only manipulate the code below that is provided but i cannot change any of the code i can only add. the search.cpp corresponds with the main code given above and i have provided the inputs for the code and the expected outputs for the code. I need to be shown how to manipulate the search.cpp code so i can put the inputs and get the desired outputs. in C++14 please.
/*search.cpp*/(unfinished code)
#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;
}
//
// SearchForKey
//
// Opens the given file, searches for the given key, and if found, returns true; if the
// key is not found then false is returned.
//
// If the key is found, the value will be returned via the "value" parameter; likewise
// the rank will be returned via the "rank" parameter. The rank is simply the line #
// where the (key, value) pair was found: 1 for first line, 2 for second line, etc.
// If the key is not found, value and rank should be ignored.
//
// 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.
//
bool SearchForKey(string filename, string key, string& value, int& rank)
{
//
// initialize return value / parameters:
//
value = "<<not found>>";
rank = 0;
bool found = false; // not found:
//
// TODO: search for the key, and if found, we want to return the associated value.
// But we also need to return the "rank" via the rank parameter; the rank is the
// line number where the (key, value) appears: 1 for first line, 2 for second
// line, and so on. Finally, return true if key was found, false if not.
//
// Don't forget to close the file when you're done.
return found;
}
Explanation / Answer
Implemented two functions as required.
Also in main.cpp added freopen to take input from a file.
/*main.cpp*/
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include "search.cpp"
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:
//
// to read input from a file
freopen("serach_keys.txt","r",stdin);
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.
//
ifstream Sfile(filename.c_str()); // Opening a file
string line,value,key;
int pos,pos2;
if(!Sfile){ cout << "Unable to open file "; exit(1); }
while (getline(Sfile, line)){ //reading data line by line
if(N==0){ //print first line (key,value)
pos = line.find(" ");
firstValue = line.substr(0,pos);
pos2 = line.find(" ",pos+1);
firstKey = line.substr(pos+1,pos2);
}
N++;
}
Sfile.close(); //closing a file
return N; //return count of values
}
//
// SearchForKey
//
// Opens the given file, searches for the given key, and if found, returns true; if the
// key is not found then false is returned.
//
// If the key is found, the value will be returned via the "value" parameter; likewise
// the rank will be returned via the "rank" parameter. The rank is simply the line #
// where the (key, value) pair was found: 1 for first line, 2 for second line, etc.
// If the key is not found, value and rank should be ignored.
//
// 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.
//
bool SearchForKey(string filename, string key, string& value, int& rank)
{
//
// initialize return value / parameters:
//
value = "<<not found>>";
rank = 0;
bool found = false; // not found:
//
// TODO: search for the key, and if found, we want to return the associated value.
// But we also need to return the "rank" via the rank parameter; the rank is the
// line number where the (key, value) appears: 1 for first line, 2 for second
// line, and so on. Finally, return true if key was found, false if not.
//
// Don't forget to close the file when you're done.
//
ifstream Sfile(filename.c_str()); // Opening a file
string line,Pvalue,Pkey;
int pos,pos2;
if(!Sfile){ cout << "Unable to open file "; exit(1); }
while (getline(Sfile, line)){ //reading data from file
pos = line.find(" ");
Pvalue = line.substr(0,pos);
pos2 = line.find(" ",pos+1);
Pkey = line.substr(pos+1,pos2);
rank++;
if(Pkey == key){ // if key matches
found = true;
value = Pvalue;
break;
}
}
Sfile.close(); //closing file
return found;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.