Make a search record for string of the array
This should compile cleanly.
Caveat: I've not tested it; not even run it once. Testing it and fixing errors (if any) is your job.
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
|
#include <iostream>
using namespace std;
#include <string>
#include <cctype>
class Insert
{
private:
string x;
// int y; // *** unused member: commented out ***
public:
void setx();
void getx(); // *** ideally, make this const-correct
const std::string& str() const { return x ; } // *** added ***
};
void Insert::setx()
{
bool good{};
do {
std::cout << "Enter letter or words: ";
std::getline(std::cin, x);
good = true;
for (const auto& ch : x)
if (!std::isalpha(static_cast<unsigned char>(ch)) && !std::isspace(static_cast<unsigned char>(ch))) {
good = false;
break;
}
} while (!good && (std::cout << "Error\n"));
}
void Insert::getx()
{
std::cout << "You typed " << x << "\n";
}
int main()
{
// int c;
int c = 1 ; // *** modified: avoid uninitialised use ***
Insert records[3];
// int inputnumber;
int inputnumber = 0 ; // *** modified: avoid a potential uninitialised use ***
string findwords;
while (c != 3)
{
cout << "Main menu";
cout << "1. create record of any words \n";
cout << "2. search record of any words \n";
cout << "3. exit\n";
cin >> c;
switch (c)
{
case 1:
cout << "Enter number of words and numbers ";
cin >> inputnumber;
cin.ignore();
if (inputnumber > 3)
{
cout << "Value has reached to over 3" << endl;
}
else
{
for (int i = 0; i < inputnumber; i++)
{
cout << "total" << i + 1 << " : " << endl;
records[i].setx();
}
cout << "\nTotal stuff you have inputed : " << endl;
for (int i = 0; i < inputnumber; i++)
{
records[i].getx();
}
}
break;
case 2:
cout << "Enter what do you want to search for" << endl;
getline(cin, findwords);
cin.ignore();
for (int i = 0; i < inputnumber; i++)
{
// if (findwords == records[i])
if (findwords == records[i].str() ) // *** modified ***
{
// cout << records[i] << endl;
std::cout << records[i].str() << '\n' ; // *** modified ***
}
}
if (inputnumber == 0)
{
cout << "No records are found" << endl;
}
break ; // *** added: avoid inadvertent fall through to next case label ***
case 3:
cout << "Exiting the program" << endl;
break;
default:
cout << "Try again" << endl;
break;
}
}
}
|
Last edited on
Topic archived. No new replies allowed.