adding some features to my code.

I want to add features to my code that I don't know how to construct or logic it out.

My desired output

STRINGS
Apple
Grapes
Lemon
Mango
Peach

Enter value to search: Peach
What do you want to do [E]dit or [D]elete?: D

STRINGS
Apple
Grapes
Lemon
Mango
-x-

Enter value to search: Mango
What do you want to do [E]dit or [D]elete?: E
Replace it with: Juice

STRINGS
Apple
Grapes
Lemon
Juice
-x-


MY CODE
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
#include <iostream>
using namespace std;

    string x[5] = {"Apple", "Grapes", "Lemon", "Mango", "Peach"};
    void displayValues(){
        cout<< "Item name(s)\n";
            for(int i=0; i < 5; i++){
            cout << "[" << i << "]" << " = "  << x[i] << "\n";
        }
    }
        void searchAndReplace() {
            string cUserInput, cUserChangeto = " ", choice;
            cout << "Enter string to search: ";
            cin >> cUserInput;
            cout << "Enter string to replace: ";
            cin >> cUserChangeto;
            for(int i = 0; i < 5; i++){
                if(x[i]== cUserInput){
                    x[i] = cUserChangeto;
                cout << "String was replaced." << endl;
                displayValues();
                    break;
                }
            }
        }
    int main(){
    displayValues();
    while(true){
    searchAndReplace();
    }
    return 0;
    }
Last edited on
on my recent post, @seeplus made this possible with all the features I showed on the post which is this https://imgur.com/a/pKMjZWf. but now I am tasked to only use loops,functions,arrays,if/else.

if anyone would ask why I create one again cause <algorithm> is not allowed on my new task. but on my recent task yes I used @seeplus's work.

this is @seeplus's work on my recent post
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
#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>
using namespace std;

constexpr size_t ARRAY_SZ {5};

void display(const string arr[ARRAY_SZ])
{
	cout << "----------------------------------------------\n";
	cout << "Product # \t \t Name\n";

	for (size_t i = 0; i < ARRAY_SZ; ++i) {
		cout << i;

		if (arr[i].empty())
			cout << "\t\t\t-NONE-\n";
		else
			cout << "\t\t\t" << arr[i] << "\n";
	}
	cout << "----------------------------------------------\n";
}

int main()
{
	string arr[ARRAY_SZ] {"Apple", "Grapes", "Lemon", "Mango", "Peach"};

	while (true) {
		std::string name;

		sort(arr, arr + ARRAY_SZ);
		display(arr);

		std::cout << "Enter item name to search (CR to terminate): ";
		std::getline(std::cin, name);

		if (name.empty())
			break;

		if (const auto fnd = std::find(arr, arr + ARRAY_SZ, name); fnd == arr + ARRAY_SZ)
			std::cout << "Error! Item does not exist\n";
		else {
			char oper {};

			std::cout << "What do you want to do?\n [E]dit [D]elete: ";
			std::cin >> oper;
			std::cin.ignore(1000, '\n');

			switch (oper = (char)std::tolower(oper)) {
				case 'e':
				{
					std::string newnam;

					std::cout << "Enter new name: ";
					std::getline(std::cin, newnam);

					if (std::find(arr, arr + ARRAY_SZ, newnam) != arr + ARRAY_SZ)
						std::cout << "Error! Item already exists\n";
					else
						*fnd = newnam;
				}
				break;

				case 'd':
					fnd->clear();
					break;

				default:
					std::cout << "Invalid operation\n";
					break;
			}
		}
	}
}
Last edited on
Just replace std::find() with your own, and don't sort (or if you want to sort you'll have to implement your own).

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
#include <iostream>
#include <string>
//#include <algorithm>
#include <cctype>
using namespace std;

const string* find(const string* first, const string* last, const string& val)
{
	for (; first != last; ++first)
		if (*first == val)
			return first;

	return last;
}

constexpr size_t ARRAY_SZ {5};

void display(const string arr[ARRAY_SZ])
{
	cout << "----------------------------------------------\n";
	cout << "Product # \t \t Name\n";

	for (size_t i = 0; i < ARRAY_SZ; ++i) {
		cout << i;

		if (arr[i].empty())
			cout << "\t\t\t-NONE-\n";
		else
			cout << "\t\t\t" << arr[i] << "\n";
	}
	cout << "----------------------------------------------\n";
}

int main()
{
	string arr[ARRAY_SZ] {"Apple", "Grapes", "Lemon", "Mango", "Peach"};

	while (true) {
		string name;

		//sort(arr, arr + ARRAY_SZ);
		display(arr);

		cout << "Enter item name to search (CR to terminate): ";
		getline(cin, name);

		if (name.empty())
			break;

		if (const auto fnd = find(arr, arr + ARRAY_SZ, name); fnd == arr + ARRAY_SZ)
			cout << "Error! Item does not exist\n";
		else {
			char oper {};

			cout << "What do you want to do?\n [E]dit [D]elete: ";
			cin >> oper;
			cin.ignore(1000, '\n');

			switch (oper = (char)tolower(oper)) {
				case 'e':
				{
					string newnam;

					cout << "Enter new name: ";
					getline(cin, newnam);

					if (find(arr, arr + ARRAY_SZ, newnam) != arr + ARRAY_SZ)
						cout << "Error! Item already exists\n";
					else
						*fnd = newnam;
				}
				break;

				case 'd':
					fnd->clear();
					break;

				default:
					cout << "Invalid operation\n";
					break;
			}
		}
	}
}

Hey, seeplus. I don't really seem to get what you mean it's complicated to me. Am I gonna change the std::find() with basic ones?
Yep. Apart from commenting out sort() (which needs <algorithm>), the existing main() code uses the new find() function as a replacement for std::find().
Wait, I tried changing the codes you recently posted for almost an hour and realizing that is it already a constructed code as is? I might be trippin' right now. but I tried the code I am getting an error in line 70(?) if I'm not mistaken and I don't know what causes it.
Last edited on
The above code compiles and runs OK with VS2019.

L70 is *fnd = newnam;

The type of fnd is const auto which becomes std::string *const (ie const pointer to std::string) which is as required to allow *fnd to be changed.

What compiler are you using and what's the actual message?

Assuming this is an issue const, then try:

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
#include <iostream>
#include <string>
//#include <algorithm>
#include <cctype>
using namespace std;

string* find(string* first, string* last, string& val)
{
	for (; first != last; ++first)
		if (*first == val)
			return first;

	return last;
}

constexpr size_t ARRAY_SZ {5};

void display(const string arr[ARRAY_SZ])
{
	cout << "----------------------------------------------\n";
	cout << "Product # \t \t Name\n";

	for (size_t i = 0; i < ARRAY_SZ; ++i) {
		cout << i;

		if (arr[i].empty())
			cout << "\t\t\t-NONE-\n";
		else
			cout << "\t\t\t" << arr[i] << "\n";
	}
	cout << "----------------------------------------------\n";
}

int main()
{
	string arr[ARRAY_SZ] {"Apple", "Grapes", "Lemon", "Mango", "Peach"};

	while (true) {
		string name;

		//sort(arr, arr + ARRAY_SZ);
		display(arr);

		cout << "Enter item name to search (CR to terminate): ";
		getline(cin, name);

		if (name.empty())
			break;

		if (auto fnd = find(arr, arr + ARRAY_SZ, name); fnd == arr + ARRAY_SZ)
			cout << "Error! Item does not exist\n";
		else {
			char oper {};

			cout << "What do you want to do?\n [E]dit [D]elete: ";
			cin >> oper;
			cin.ignore(1000, '\n');

			switch (oper = (char)tolower(oper)) {
				case 'e':
				{
					string newnam;

					cout << "Enter new name: ";
					getline(cin, newnam);

					if (find(arr, arr + ARRAY_SZ, newnam) != arr + ARRAY_SZ)
						cout << "Error! Item already exists\n";
					else
						*fnd = newnam;
				}
				break;

				case 'd':
					fnd->clear();
					break;

				default:
					cout << "Invalid operation\n";
					break;
			}
		}
	}
}


which also compiles and runs OK with VS2019.
Last edited on
OP, make sure your compiler is set to C++17 standard features otherwise the following will not compile:

if (auto fnd = find(arr, arr + ARRAY_SZ, name); fnd == arr + ARRAY_SZ)

Initialization sections in if() statements were added with C++17
@seeplus, Thank you! I use CodeBlocks, and this is now perfectly working already. But how can I not repeat the "Product # Name" every enter to look more like this? https://imgur.com/a/Mz6s9L5

I've been trying to reposition display(arr);.
Last edited on
How can I stop "Product # Name" from repeating to only appear if a name is changed or deleted?
Last edited on
Just move display() to the required location(s). Consider:

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
#include <iostream>
#include <string>
//#include <algorithm>
#include <cctype>
using namespace std;

string* find(string* first, string* last, string& val)
{
	for (; first != last; ++first)
		if (*first == val)
			return first;

	return last;
}

constexpr size_t ARRAY_SZ {5};

void display(const string arr[ARRAY_SZ])
{
	cout << "----------------------------------------------\n";
	cout << "Product # \t \t Name\n";

	for (size_t i = 0; i < ARRAY_SZ; ++i) {
		cout << i;

		if (arr[i].empty())
			cout << "\t\t\t-NONE-\n";
		else
			cout << "\t\t\t" << arr[i] << "\n";
	}
	cout << "----------------------------------------------\n";
}

int main()
{
	string arr[ARRAY_SZ] {"Apple", "Grapes", "Lemon", "Mango", "Peach"};

	display(arr);

	while (true) {
		string name;

		//sort(arr, arr + ARRAY_SZ);
		//display(arr);

		cout << "Enter item name to search (CR to terminate): ";
		getline(cin, name);

		if (name.empty())
			break;

		if (auto fnd = find(arr, arr + ARRAY_SZ, name); fnd == arr + ARRAY_SZ)
			cout << "Error! Item does not exist\n";
		else {
			char oper {};

			cout << "What do you want to do?\n [E]dit [D]elete: ";
			cin >> oper;
			cin.ignore(1000, '\n');

			switch (oper = (char)tolower(oper)) {
				case 'e':
				{
					string newnam;

					cout << "Enter new name: ";
					getline(cin, newnam);

					if (find(arr, arr + ARRAY_SZ, newnam) != arr + ARRAY_SZ)
						cout << "Error! Item already exists\n";
					else {
						*fnd = newnam;
						display(arr);
					}
				}
				break;

				case 'd':
					fnd->clear();
					display(arr);
					break;

				default:
					cout << "Invalid operation\n";
					break;
			}
		}
	}
}

Thank you so much, seeplus. My one last problem is this one I added some codes in line 66, 67, 68. To make it look like this: https://imgur.com/a/U1GQY35

But my problem is it didn't change Mango to Cake as you can see in the image.

ps; im sorry if i keep asking this typ'a question i don't know what line is changing the lists.
Well I don't know what you're changed as you didn't post your code, but consider:

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
#include <iostream>
#include <string>
//#include <algorithm>
#include <cctype>
using namespace std;

string* find(string* first, string* last, string& val)
{
	for (; first != last; ++first)
		if (*first == val)
			return first;

	return last;
}

constexpr size_t ARRAY_SZ {5};

void display(const string arr[ARRAY_SZ])
{
	cout << "----------------------------------------------\n";
	cout << "Product # \t \t Name\n";

	for (size_t i = 0; i < ARRAY_SZ; ++i) {
		cout << i;

		if (arr[i].empty())
			cout << "\t\t\t-NONE-\n";
		else
			cout << "\t\t\t" << arr[i] << "\n";
	}
	cout << "----------------------------------------------\n";
}

int main()
{
	string arr[ARRAY_SZ] {"Apple", "Grapes", "Lemon", "Mango", "Peach"};

	display(arr);

	while (true) {
		string name;

		//sort(arr, arr + ARRAY_SZ);
		//display(arr);

		cout << "Enter item name to search (CR to terminate): ";
		getline(cin, name);

		if (name.empty())
			break;

		if (auto fnd = find(arr, arr + ARRAY_SZ, name); fnd == arr + ARRAY_SZ)
			cout << "Error! Item does not exist\n";
		else {
			char oper {};

			cout << "What do you want to do?\n [E]dit [D]elete: ";
			cin >> oper;
			cin.ignore(1000, '\n');

			switch (oper = (char)tolower(oper)) {
				case 'e':
				{
					while (true) {
						string newnam;

						cout << "Enter new name: ";
						getline(cin, newnam);

						if (find(arr, arr + ARRAY_SZ, newnam) != arr + ARRAY_SZ)
							cout << "Error! Item already exists\n";
						else {
							*fnd = newnam;
							display(arr);
							break;
						}
					}
				}
				break;

				case 'd':
					fnd->clear();
					display(arr);
					break;

				default:
					cout << "Invalid operation\n";
					break;
			}
		}
	}
}


I forgot to post the codes it's this.
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
#include <iostream>
#include <string>
//#include <algorithm>
using namespace std;

string* find(string* first, string* last, string& val)
{
	for (; first != last; ++first)
		if (*first == val)
			return first;

	return last;
}

constexpr size_t ARRAY_SZ {5};

void display(const string arr[ARRAY_SZ])
{
	cout << "Product # \tName\n";
	for (size_t i = 0; i < ARRAY_SZ; ++i) {
		cout << "[" << i << "]" << " =";

		if (arr[i].empty())
			cout << "\t\t-x-\n";
		else
			cout << "\t\t" << arr[i] << "\n";
	}
}

int main()
{
	string arr[ARRAY_SZ] {"Apple", "Grapes", "Lemon", "Mango", "Peach"};

	display(arr);

	while (true) {
		string name;

		//sort(arr, arr + ARRAY_SZ);
		//display(arr);

		cout << "Enter item name to search (CR to terminate): ";
		getline(cin, name);

		if (name.empty())
			break;

		if (auto fnd = find(arr, arr + ARRAY_SZ, name); fnd == arr + ARRAY_SZ)
			cout << "Error! Item does not exist\n";
		else {
			char oper {};

			cout << "What do you want to do?\n[E]dit [D]elete: ";
			cin >> oper;
			cin.ignore(1000, '\n');

			switch (oper = (char)tolower(oper)) {
				case 'e':{
					string newnam;
					cout << "Enter new name: ";
					getline(cin, newnam);

					if (find(arr, arr + ARRAY_SZ, newnam) != arr + ARRAY_SZ){
						cout << "Error! Item already exists\n";
                        cout << "Enter new name: ";
                        getline(cin, newnam);
                        display(arr);

					}else {
						*fnd = newnam;
						display(arr);
					}
				}
				break;

				case 'd':
					fnd->clear();
					display(arr);
					break;

				default:
					cout << "Invalid operation\n";
					break;
			}
		}
	}
}
Last edited on
@seeplus, I changed the codes you recently sent because it will be hard for me to explain some codes that were used so I came up with these, but my problem is whenever I try to delete one from the list it doesn't show the display(arr); anymore.
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
#include <iostream>
#include <string>
using namespace std;
string* find(string* first, string* last, string& val){
    for (; first != last; ++first)
        if (*first == val)
        return first;
        return last;
}
    constexpr size_t ARRAY_SZ {5};
    void display(const string arr[ARRAY_SZ]){
	cout << "Item Name(s)\n";
        for (size_t i = 0; i < ARRAY_SZ; ++i){
            cout << "[" << i << "]" << " = ";
                if (arr[i].empty()){
                    cout << "\t\t-x-\n";
            }else{
                cout << "\t" << arr[i] << "\n";
        }
    }
}
int main(){
	string arr[ARRAY_SZ]{"Apple", "Grapes", "Lemon", "Mango", "Peach"};
	display(arr);
        while (true){
            string name;
                cout << "Enter item to search: ";
                getline(cin, name);
                    if (name.empty()){
                    break;
                    }
        if(auto fnd = find(arr, arr + ARRAY_SZ, name); fnd == arr + ARRAY_SZ){
        cout << "Error! Item does not exist\n";
		}else{
        char oper {};
        cout << "What do you want to do?\n [E]dit [D]elete: ";
        cin >> oper;
        cin.ignore(1000, '\n');
        //switch (oper = (char)tolower(oper)){
        if(oper=='e' || oper=='E'){
            //case 'e':{
            while (true){
            string newnam;
            cout << "Enter new name: ";
            getline(cin, newnam);
            if (find(arr, arr + ARRAY_SZ, newnam) != arr + ARRAY_SZ){
            cout << "Error! Item already exists\n";
            }else {
            *fnd = newnam;
            display(arr);
            break;  }
            if(oper=='d' || oper=='D'){
            //case 'd':
            fnd->clear();
            display(arr);
            }else{
            cout << "Invalid operation\n";
            break;
                            }
                        }
                    }
                }
            }
        return 0;
        }
Last edited on
OK, based upon your code. Note when posting, please format your code so that it is easy to read.

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
#include <iostream>
#include <string>
using namespace std;

string* find(string* first, string* last, string& val) {
	for (; first != last; ++first)
		if (*first == val)
			return first;
	return last;
}

constexpr size_t ARRAY_SZ {5};

void display(const string arr[ARRAY_SZ]) {
	cout << "Item Name(s)\n";
	for (size_t i = 0; i < ARRAY_SZ; ++i) {
		cout << "[" << i << "]" << " = ";
		if (arr[i].empty()) {
			cout << "\t\t-x-\n";
		} else {
			cout << "\t" << arr[i] << "\n";
		}
	}
}

int main() {
	string arr[ARRAY_SZ] {"Apple", "Grapes", "Lemon", "Mango", "Peach"};

	display(arr);

	while (true) {
		string name;

		cout << "Enter item to search: ";
		getline(cin, name);

		if (name.empty())
			break;

		if (auto fnd = find(arr, arr + ARRAY_SZ, name); fnd == arr + ARRAY_SZ)
			cout << "Error! Item does not exist\n";
		else {
			char oper {};

			while (true) {
				cout << "What do you want to do?\n [E]dit [D]elete: ";
				cin >> oper;
				cin.ignore(1000, '\n');

				if (oper == 'e' || oper == 'E') {
					while (true) {
						string newnam;

						cout << "Enter new name: ";
						getline(cin, newnam);

						if (find(arr, arr + ARRAY_SZ, newnam) != arr + ARRAY_SZ)
							cout << "Error! Item already exists\n";
						else {
							*fnd = newnam;
							display(arr);
							break;
						}
					}
					break;
				} else
					if (oper == 'd' || oper == 'D') {
						fnd->clear();
						display(arr);
						break;
					} else
						cout << "Invalid operation\n";
			}
		}
	}
}



Item Name(s)
[0] =   Apple
[1] =   Grapes
[2] =   Lemon
[3] =   Mango
[4] =   Peach
Enter item to search: qqq
Error! Item does not exist
Enter item to search: Grapes
What do you want to do?
 [E]dit [D]elete: e
Enter new name: Apple
Error! Item already exists
Enter new name: Apple
Error! Item already exists
Enter new name: qq
Item Name(s)
[0] =   Apple
[1] =   qq
[2] =   Lemon
[3] =   Mango
[4] =   Peach
Enter item to search:

Last edited on
Sorry, my computer needed some repair these past days.

Thank you, @seeplus!
Topic archived. No new replies allowed.