Make a search record for string of the array

Pages: 12
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

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
  #include <iostream>

using namespace 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 (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;
    Insert records[3];
    int inputnumber;
    string findwords;
    
 
    
    while(c != 3)
    {
          cout << "Main menu \n";
          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:
        cin >> searchname;
        for (int i = 0; i < searchname; i++)
        {
                   if (recordArray[i].getx() == searchname)
                   {
                          recordArray[i].getx();
                          cout >> "You found it" >> endl;
                    }
                    else
                    {
                           cout >> "Try again" >> endl;
                     } 
        }
        break;

        case 3:
        cout  << "Exiting the program" << endl;
        break;

        default:
        cout << "Try again" << endl;
        break;
        }
        
    }
}
Last edited on
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?

Last edited on
@seeplus

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".

Last edited on
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
Last edited on
@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
Last edited on
> Meanwhile case 2 never worked for a search of array records using an input

Case 2 could be something along these lines:

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
#include <iostream>
#include <string>
#include <cctype>

bool is_alpha_or_space( unsigned char 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) ) return false ;
    
    return true ; // 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" ;
    }
}
@JLBorges, so the bold letters are for case 2 for my program right?
@JLBorges

Tried to use your code that you had mention with the bold letters, it didnt work at all
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
#include <iostream>
using namespace 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 (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;
    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;
        }
        
    }
}
Last edited on
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
@seeplus
@JLBorges
OK my mistake but my code was still not working for case 2
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
#include <iostream>
using namespace 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 (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;
    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:
        size_t i = 0 ;
        while( i < inputnumber )
        {
            std::getline(std::cin, search_string);
            if( records[i] == search_string ) 
            {
                cout << "found it at position " << i << '\n' ;
                break ; 
            }

            else ++i ; 
        }

        if( i == inputnumber ) 
            cout << "failed to find it\n" ;
        break;

        case 3:
        cout << "Exiting the program" << endl;
        break;

        default:
        cout << "Try again" << endl;
        break;
        }
        
    }
}
Last edited on
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 ==
Last edited on
One issue is that your .getx() just displays the contents rather than actually returning the data. L91 should be something like:

 
if (record[i].getx() == search_string)


assuming that .getx() is now:

 
std::string getx() const {return x;}

@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
Last edited on
@JLBorges

Your code was working as this is exactly what I wanted but I just want to use that code on case 2 statement only still for the requirement at least.
Last edited on
> Your code was working as this is exactly what I wanted but I just want to use that code on case 2 statement only

To learn programming in C++, you need to try and write code. Blindly copying someone else's code is not going to get you anywhere.

Try writing something like this on your own, using constructs that you have already learnt:

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
#include <iostream>
#include <string>
#include <cctype>

bool is_alpha_or_space( unsigned char 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) ) return false ;

    return true ; // 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: " ;
    while( txt.empty() ) std::getline( std::cin, txt ) ; // skip empty lines
    if( is_alpha_or_space(txt) ) return txt ;
    std::cout << "error in input: non alpha or space characters. try again\n" ;
    return get_text() ; // try again
}

enum choice_t { CREATE, SEARCH, EXIT } ;

choice_t menu()
{
    std::cout << "\nMain menu\n----------\n"
    "1. create records\n"
    "2. search record\n"
    "3. exit\n"
    "Enter choice: " ;

    char c ;
    std::cin >> c ;
    switch(c)
    {
        case '1' : return CREATE ;
        case '2' : return SEARCH ;
        case '3' : return EXIT ;
        default:
        std::cout << "invalid choice. try again\n" ;
        return menu() ; // try again
    }
}

void create( std::string records[], std::size_t num_recs ) // create num_recs records
{
    std::cout << "enter " << num_recs << " records\n" ;
    for( std::size_t i = 0 ; i < num_recs ; ++i ) records[i] = get_text() ; // enter num_recs records
}

// search for search_string and print the result of the search
void search( const std::string records[], std::size_t num_recs, const std::string& search_string )
{
    std::size_t i = 0 ;
    while( i < num_recs )
    {
        if( records[i] == search_string ) // note: case and whitespace sensitive search
        {
            std::cout << "*** found '" << search_string << "' 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 '" << search_string << "'\n" ;
}

int main()
{
    const std::size_t MAX_SZ = 500 ;
    std::string records[MAX_SZ] ;
    std::size_t num_recs = 0 ;

    choice_t choice = EXIT ;
    while( ( choice = menu() ) != EXIT )
    {
        switch(choice)
        {
            case CREATE:
                num_recs = 0 ; // create a fresh set of records
                std::cout << "create how many records? " ;
                std::cin >> num_recs ;
                if( num_recs > MAX_SZ ) num_recs = MAX_SZ ;

                if( num_recs > 0 ) create( records, num_recs ) ;
                break ;

            case SEARCH:
                if( num_recs == 0 )
                {
                    std::cout << "no records. create some records first\n" ;
                    break ;
                }

                std::cout << "\nenter a search string\n" ;
                search( records, num_recs, /* search_string = */ get_text() ) ;

                break ;

            case EXIT: ; // do nothing
        }
    }

    std::cout << "exiting program. bye!\n" ;
}



@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;
     }
Last edited on
> 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.
@JLBorges

However Line 94 and 96 mentioned that the operators cannot match these operands
Here is the code that I worked so far
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
#include <iostream>
using namespace 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 (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;
    Insert records[3];
    int inputnumber;
    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])
                {
                   cout << records[i] << endl;
                }
            }
            if (inputnumber == 0)
            {
                cout << "No records are found" << endl;
            }
            

        case 3:
            cout << "Exiting the program" << endl;
            break;

        default:
            cout << "Try again" << endl;
            break;
        }

    }
}
Last edited on
Pages: 12