I tried to make a search input record of words based on a string and I couldnt figure it out.
Here is what I want to make a search record as I describe:
"Once you have created a record of input words, make a search function for that word. For example if you have created an input record "Hello there" on option 1, create another input based on searching that input record you had previously created, if created option 2 on switch, first there should an output message of "Search the words you have created" then allow to use an input to type and search that record, if you typed "Hello there" on that record, it should be appear not only the input word you have created like "Hello there" for example but also the output message of "You found it" and switches back to the main menu, if the input was invalid such as none of the records are found on the search option then it will appear an output message of "We havent found that" and switches back to the main menu.
Keep in mind that the programming should be easy to read as a beginner and don't make the program complicated to read as a beginner
I don't really understand from the description what actually is required??
Do you want a vector of input words and then ask for word(s) to be found in the vector? If more than one word to be found - do all entered words have to be found or just any?
setx() is getting one string of multiple words separated by white-space. Is this what you want? Or do want each word stored separately?
The description is ask for words to be found in the vector then input the vector.
If more than one word is found, all entered words have to be found.
setx is one string of multiple words so each word should not be stored separately in one array in case of input like "Hello there", therefore something like "Hello there" should remain together and not separated in each word like "Hello" and "there".
Well, I would seriously consider binning that code and starting afresh by first producing a program design and only once you have a design do you then start to code from that design. What's the input, what's the output, what data structures are needed, what algorithm(s) are needed?
@seeplus
Well I would just bin case 2 code only because the rest of the code is important as I must create an input record of words especially case 1 coding and the class so sorry I would rather not bin these important codes. Meanwhile case 2 never worked for a search of array records using an input so just bin case 2 only.
In case you could just change case 2 and here is some requirements:
For example when you create a record of "Hello there" being stored in an array in case 1 then go to case 2:
1. The output first would be first appear the message of "What would would you want to search" then the input would allow you to type to search that correct word
2. Once you have typed the input of "Hello there", then the results should show the array records you had created like "Hello there" for example and the output message "You found it". After that, it exits back to the main menu
3. If the input was invalid like you typed other words except words that arent stored in array like "Hello there", then the output should appear "We cant find it" and exit back to the main menu so no loops on case 2.
4. Variable of the input should be a string for case 2
As for the algorithms and data structure I wasnt familiar, therefore I couldnt tell you that much as a beginner
@JLborges
I might need your help as well because I still cant figure out.
Just modify case 2 switch on this code because I need case 2 to make a search array for words with string input
#include <iostream>
#include <string>
#include <cctype>
bool is_alpha_or_space( unsignedchar c ) { return std::isalpha(c) || std::isspace(c) ; }
bool is_alpha_or_space( const std::string& str )
{
for( char c : str ) if( !is_alpha_or_space(c) ) returnfalse ;
returntrue ; // all characters in str are alpha or space
}
std::string get_text() // get a string of only alpha or white space characters
{
std::string txt ;
std::cout << "enter words: " ;
std::getline( std::cin, txt ) ;
if( is_alpha_or_space(txt) ) return txt ;
std::cout << "error in input: non alpha or space characters. try again\n" ;
return get_text() ;
}
int main()
{
const std::size_t MAX_SZ = 500 ;
std::string records[MAX_SZ] ;
std::size_t num_recs = 0 ;
std::cout << "how many records? " ;
std::cin >> num_recs ;
if( num_recs > 0 )
{
std::cout << "enter " << num_recs << " records\n" ;
for( std::string& str : records ) str = get_text() ; // enter N records
std::cout << "\nenter a search string\n" ;
const std::string search_string = get_text() ;
std::size_t i = 0 ;
while( i < num_recs )
{
if( records[i] == search_string ) // note: case and whitespace sensitive search
{
std::cout << "found it at position " << i << '\n' ;
break ; // we are done
}
else ++i ; // get to the next record
}
if( i == num_recs ) // if we had reached end of list while trying to find it
std::cout << "failed to find it\n" ;
}
}
#include <iostream>
usingnamespace std;
#include <string>
#include <cctype>
class Insert
{
private:
string x;
int y;
public:
void setx();
void getx();
};
void Insert::setx()
{
bool good {};
do {
std::cout << "Enter letter or words: ";
std::getline(std::cin, x);
good = true;
for (constauto& ch : x)
if (!std::isalpha(static_cast<unsignedchar>(ch)) && !std::isspace(static_cast<unsignedchar>(ch))) {
good = false;
break;
}
} while (!good && (std::cout << "Error\n"));
}
void Insert::getx()
{
std::cout << "You typed " << x << "\n";
}
int main()
{
int c;
Insert records[3];
int inputnumber;
string search_string;
int num_recs;
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:
std::size_t i = 0 ;
while( i < num_recs )
{
if( records[i] == search_string )
{
cout << "found it at position " << i << '\n' ;
break ;
}
else ++i ;
}
if( i == num_recs ) // if we had reached end of list while trying to find it
cout << "failed to find it\n" ;
break;
case 3:
cout >> "Exiting the program" >> endl;
break;
default:
cout << "Try again" << endl;
break;
}
}
}
Because you're copied and pasted - without understanding the code and altering as required for your program. eg Borges code uses num_recs which is correctly set. In your code num_recs is defined just to get the code to compile but not is not set. Hence your code doesn't work. etc etc etc
Define 'not working'. What debugging of the code have you done? Where is the issue where the observed operation of the code departs from your program design?
As I said previously, first design and then code from the design. You are attempting to 'design as code' rather than 'design before code'. Not having a program design before starting to code often leads to programs that don't work. You're attempting to 'cut and paste' other peoples code into your own without first understanding it. Again this often doesn't produce what is expected as you haven't understood the pasted code and hence how to adapt it as required.
@seeplus
The problem was at line 91 with the operators are cannot be matched because search_string was a string and cannot use these operators such as ==
@seeplus
Tried but not working because the string search_string is incompatable with operator "==" at line 91 and initialization of i is skipped by case and default label from line 105 and 109.
Definetly need a new code for case 2, my code design was already mentioned before and still struggle to do
@JLBorges
Sure your program work but it was way too complicated as a beginner, I prefer a simple search array like this code for example for statement of case 2 but sadly this code wont work because string cannot use with operators such as ==
1 2 3 4 5 6 7 8 9 10 11 12
cout << "Enter what you want to search for: "<< endl;
getline(cin, findwords);
for(int i =0; i< inputnumber; i++){
if(search == record[i]){
cout << record[i]<< endl;
}
}
if(findwords == 0)
{
cout << "No records are found" << endl;
}
> this code wont work because string cannot use with operators such as ==
std::string objects can be compared for equality with ==.
In your code, record is an array of objects of type Insert.
Use == on the std::string member of the Insert object.