1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
#include <chrono>
#include <thread>
#include <limits>
using namespace std; // <--- Not the best idea to use.
const int MAX = 30;
int search(string[], int, string);
void fixstring(string&);
void get_data(string[], int&);
void bubblesort(string[], int&);
int main()
{
int n{}; // <--- Good practice to initialize all your variables.
int count{};
string names[MAX];
int place{};
int finished = 0; // Changed to make the while loop work. Also not a good idea to set this up to end
// the while loop before it starts.
string target = ""; // <--- This is OK, but no need to initialize a string becuse it is already empty.
get_data(names, n);
count = n; // <--- Defined "count", but never gave it a value before it is used in thee next line.
bubblesort(names, count);
// Added this to print the sorted names. Keep or not it is up to you, but helps with testing.
for (size_t lc = 0; lc < count; lc++)
{
std::cout << "\n " << names[lc];
}
while (finished != -9)
{
cout << "\n\n Enter name to find \n"; // Changed.
cout << " Q to quit: "; // <--- Changed. Removed the "\n"
std::cin >> target;
if (target == "Q" || target == "q") // <--- Added ||
finished = -9;
else
{
place = search(names, n, target);
cout << "\n " << target << " "; // <--- Changed. Added space.
if (place >= 0)
cout << "found at position " << place;
else
cout << "not found";
cout << endl;
}
}
// Could use something here to pause before the return.
// At least I need it.
std::cin.get(); // <--- One possibioity.
return 0;
}
void get_data(string names[], int& n)
{
ifstream fin;
std::string fileName{ "TextFile.txt" }; //<--- Added the variable and constant for file name for testing. Remove for run.
string fnames; // <--- Can keep this or use "fileName" if you want.
int namecount{};
// Moved these lines up to here for when needed.
// cout<< "enter name of input file" << endl;
//cin >> fnames;
//fin.open(fnames.c_str());
fin.open(fileName); //<--- Added.
// Always good practice and programming to make sure the input file is open.
if (!fin.is_open()) //<--- Added.
{
std::cout << "\n File " << fileName << " did not open" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(3)); // Requires header files "chrono" and "thread"
exit(1);
}
n = 0;
while (fin>>names[n]) // <--- Changed. This is where the read should go to.
{
fixstring(names[n]);
n++;
namecount++;
//cin >> fnames;
}
cout << "Number of Names in the file " << namecount << endl;
// Clears the input buffer after a "fin >>" to something. Or can be used with "cin >>".
fin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires heder file <limits>.
}
// These next threee functions work.
void fixstring(string& word)
{
int num = word.size();
int lenght = word.size();
word[0] = toupper(word[0]);
for (int i = 1; i<num; i++)
word[i] = tolower(word[i]);
}
int search(string names[], int n, string target)
{
for (int i = 0; i<n; i++)
if (names[i] == target)
return i;
return -1;
}
void bubblesort(string names[], int& count)
{
string temp; //place holder when values are interchanged
for (int i = 0; i < count - 1; i++)
for (int j = 0; j < count - (i + 1); j++)
if (names[j] > names[j + 1]) // <--- Changed to >.
{
temp = names[j];
names[j] = names[j + 1];
names[j + 1] = temp;
//cout << names[j] << endl; // <--- Confusing but OK for testing.
}
cout << "Organized names by ABC order " << endl;
}
|