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

This is my code below. I have new requirements that when the code is ran using t

ID: 3878495 • Letter: T

Question

This is my code below. I have new requirements that when the code is ran using the sample.txt file it should output with the following ouptut supplied below.

But with my code I am having issue fixing one thing. When a word not in the file, my code simply just prompts the user for the next word

#include <iostream>

#include <fstream>
#include <cctype>
#include <string>
#include <string.h>

using namespace std;

void search(string arr[10000], int count, string lookupstr) { //initialze paramters for search
  
string found[count];
int wordCount = 0; //initialize starting count of text file

for(int x = 0; x < count; x++) {
string str = arr[x];
bool matchFound = false;


if(str.length() == lookupstr.length()) { //check length of character found

int f = 0;
while(f < (signed) lookupstr.length()){
  
if(lookupstr[f] == str[f]){ //check characters for matches
matchFound = true;
}
else if(lookupstr[f] == '?'){ //check for special character '?'
matchFound = true;
}
else {
matchFound = false; //no char found set match to false
break;
}
  
f++; //increase count of length for matches
}}

bool matchMade = false;


for(int pos = 0; pos < wordCount; pos++) { //initiliaztion for character placement to search for matches
if(str.compare(found[pos]) == 0) { //if match position is made for loop

matchMade = true;
}}

if(!matchMade) {
if(matchFound) {

int foundAmount = 0; //initialize variable for found character count

for(int z = 0; z < count; z++) { //loop to check for similar characters and total them
if(str.compare(arr[z]) == 0) {
foundAmount++;
}}

cout << "The word " << str << " appears " << foundAmount << " times in the document" << endl;} //output satement of found characters

found[wordCount++] = str;
}}

return;
}

int countAmount(string arr[10000], int amt) { //initialize the unique words in file
string uniquearr[amt];
int cntr = 0;
bool checkUnique = true;

  
for(int x = 0; x < amt; x++) {

checkUnique = true;


for(int f = 0; f < amt; f++) {
if(arr[x].compare(uniquearr[f]) == 0) {
checkUnique = false;
}}


if(checkUnique) {
uniquearr[cntr++] = arr[x]; //if unique found and matches, total with if statement
}}

return cntr;}


string lowerCase(string str) {
int x;
for(x = 0; x < (signed) str.length(); x++) { //for statement to initilaize all characters to lower case
str[x] = tolower(str[x]); //use built in function 'tolower'
}

return str;}

int main(int argc, char** argv) { //initiliaztion of main statement
ifstream infile; //initilization of opening given text file
infile.open(argv[1]); //using arg[1] to set text file when prompted
  
if(infile.fail()) return 0; //takes care of text file not found

string arr[10000];
string str;
int count = 0;
int distinctFound = 0;

while(infile >> str) { //searches through file till no characters left

bool caseCheck = false;


for(int pos = 0; pos < (signed) str.length(); pos++) {
  
if(isalpha(str[pos])) {
  
}
else {

arr[count++] = lowerCase(str.substr(0, pos));


caseCheck = true;


if(str[pos+1] != '') {
  
string split;
for(int loc = pos+1; loc < (signed) str.length(); loc++) {
split += str[loc];
}
arr[count++] = lowerCase(split);
}}}

if(!caseCheck) {
arr[count] = lowerCase(str);
count++;
}}

  
distinctFound = countAmount(arr, count);

cout << "The number of words found in the file was " << count << endl; //required ouptut for project
cout << "The number of unique words found in the file was " << distinctFound << endl;
string lookupstr = ""; //initilization of prometed word to search
cout << "Please enter a word: "; //required prompt to search file
cin >> lookupstr;

while(lookupstr.compare("END") != 0) {
search(arr, count, lowerCase(lookupstr));

cout << "Please enter a word: "; //promt to continue searching for words until user ends the program
cin >> lookupstr;
}

  
infile.close(); //closing of file being read

return 0;
}

The number of words found in the file was 64 The number of unique words found in the file was 52 Please enter a word: of The word of appears 2 times in the document Please enter a word: compiler The word compiler appears O times in the document Please enter a word: is The word is appears 2 times in the document Please enter a word: or The word or appears 2 times in the document Please enter a word: a?? The word and appears 4 times in the document Please enter a word: i? The word is appears 2 times in the document The word in appears 1 time in the document Please enter a word: END

Explanation / Answer

void search(string arr[10000], int count, string lookupstr) { //initialze paramters for search
  
string found[count];
int wordCount = 0; //initialize starting count of text file
int display_flag = 0;

for(int x = 0; x < count; x++) {
string str = arr[x];
bool matchFound = false;


if(str.length() == lookupstr.length()) { //check length of character found

int f = 0;
while(f < (signed) lookupstr.length()){
  
if(lookupstr[f] == str[f]){ //check characters for matches
matchFound = true;
}
else if(lookupstr[f] == '?'){ //check for special character '?'
matchFound = true;
}
else {
matchFound = false; //no char found set match to false
break;
}
  
f++; //increase count of length for matches
}
}

bool matchMade = false;


for(int pos = 0; pos < wordCount; pos++) { //initiliaztion for character placement to search for matches
if(str.compare(found[pos]) == 0) { //if match position is made for loop

matchMade = true;
}
}

if(!matchMade) {
if(matchFound ) {
display_flag = 1;
int foundAmount = 0; //initialize variable for found character count
  
for(int z = 0; z < count; z++) { //loop to check for similar characters and total them
if(str.compare(arr[z]) == 0) {
foundAmount++;
}
}
cout << "The word " << str << " appears " << foundAmount << " times in the document" << endl;


  
} //output satement of found characters

found[wordCount++] = str;
}
}
if(!display_flag){
cout << "The word " << lookupstr << " appears " << 0 << " times in the document" << endl;  
}

return;
}

// modified the function void search(string arr[10000], int count, string lookupstr)

//just added a new variable display_flag with condition. This should work week to get zero occurence

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote