I wrote a program for my C++ class (it finished a few weeks ago) and my instructor said a comment I didn't really understand (he didn't elaborate on the comment). He said that it seems to be written in Java or seems Java-like.
I'm not going to post whole thing here (too long) but he did say the comment on a particular piece of code, wherein we were to do the ff.: (Paraphrased)
Given a list of names, allow the user to enter a search term. Return all strings which have that search term as a substring. Write your own code to determine if a string is a substring of another.
Here's what I came up with:
vector<string> Directory::searchByName(string nameArg) {
vector<string> matches;
for (string r : records) {
int pos = 0;
int last = r.length();
bool isFound = false;
do {
int x = pos, a;
for (a = 0; a < nameArg.length() && x < last; a++, x++) {
if (toupper(nameArg[a]) != toupper(r[x])) break;
}
if (a == nameArg.length()) {
matches.push_back(r);
isFound = true;
break;
}
int temp_pos = pos;
pos = r.find(toupper(nameArg[0]), temp_pos + 1);
if (pos == string::npos) {
pos = r.find(tolower(nameArg[0]), temp_pos + 1);
}
} while ( !isFound && pos != string::npos && pos < last);
}
return matches;
}
The code runs (and could be optimized, I admit) but I just don't understand that comment. Did I do something wrong or unconventional in terms of C++ in my syntax? If so, what is it and how can I improve it?
Somebody asked me to include code that can be compiled. We were also practicing the three file format thing so I'm not sure how to do that here.
Full code:
main.cpp
#include "Directory.h"
#include <iostream>
using namespace std;
int main() {
Directory dir;
string searchTerm;
// dir.lazyInit();
// dir.saveToFile("dat.dat");
// dir.displayAllRecords();
dir.loadDirFromFile("dat.dat");
cout << "This program searches for records that " << endl
<< "matches a full name or a partial name." << endl
<< "Please enter a name as prompted." << endl << endl;
cout << " Search? ";
getline(cin, searchTerm);
cout << endl;
vector<string> results = dir.searchByName(searchTerm);
if (results.size() == 0) {
cout << "We're sorry. There are no matches." << endl;
} else {
cout << "The following records are found: " << endl;
for (string r : results) {
cout << " " << r << endl;
}
}
return 0;
}
Directory.h
#ifndef HOMEWORK_CH12_QN11_DIRECTORY_H
#define HOMEWORK_CH12_QN11_DIRECTORY_H
#include <vector>
#include <string>
class Directory {
private:
std::vector<std::string> records;
public:
void lazyInit();
void displayAllRecords();
std::vector<std::string> searchByName(std::string);
void saveToFile(std::string);
void loadDirFromFile(std::string);
};
Directory.cpp
#include "Directory.h"
#include <iostream>
#include <fstream>
using namespace std;
void Directory::lazyInit() {
records = {
"Hoshikawa Tanaka, 678-1223",
"Joe Looney, 586-0097",
"Geri Palmer, 223-8787",
"Lynn Lopez, 887-1212",
"Holly Gaddis, 223-8878",
"Sam Wiggins, 486-0998",
"Bob Kain, 586-8712",
"Tim Haynes, 586-7676",
"Warren Gaddis, 223-9037",
"Jean James, 678-4939",
"Ron Palmer, 486-2783"
};
}
void Directory::displayAllRecords() {
for (string s : records) {
cout << s << endl;
}
}
vector<string> Directory::searchByName(string nameArg) {
vector<string> matches;
for (string r : records) {
int pos = 0;
//int last = r.find(',');
//if (last == string::npos) last = r.length();
int last = r.length();
bool isFound = false;
do {
int x = pos, a;
for (a = 0; a < nameArg.length() && x < last; a++, x++) {
if (toupper(nameArg[a]) != toupper(r[x])) break;
}
if (a == nameArg.length()) {
matches.push_back(r);
isFound = true;
break;
}
int temp_pos = pos;
pos = r.find(toupper(nameArg[0]), temp_pos + 1);
if (pos == string::npos) {
pos = r.find(tolower(nameArg[0]), temp_pos + 1);
}
} while ( !isFound && pos != string::npos && pos < last);
}
return matches;
}
void Directory::saveToFile(string filename) {
fstream file(filename, ios::out | ios::binary);
if (!file) return;
for (string r : records) {
int l = r.length();
file.write(reinterpret_cast<char *>(&l), sizeof(l));
file.write(r.data(), l);
}
file.close();
}
void Directory::loadDirFromFile(string filename) {
const int BUFFER_SIZE = 256;
static char buffer[256];
fstream file(filename, ios::in | ios::binary);
if (!file) return;
while(file.good() && file.peek() != EOF) {
int l = 0;
file.read(reinterpret_cast<char *>(&l), sizeof(l));
file.read(buffer, l);
buffer[l] = '\0';
records.push_back(static_cast<string>(buffer));
}
file.close();
}