change descending order to ascending.

How can I change the order of 'Name' to start changing from bottom to top, or ascending order.

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
#include <iostream>
#include <string>
using namespace std;
int main(){
    constexpr size_t ARRAY_SZ = 5 ;
    string arr[ARRAY_SZ] ;
    do{
    // print_array
    for ( int input_index=0; input_index < ARRAY_SZ; ++input_index ){
        cout<< "----------------------------------------------\n";
        cout<< "Product # \t \t Name" << endl;
    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";
    }
    // get_array_elements
    cout<< "----------------------------------------------\n";
        cout << "Enter Product name: " ;
        cin >> arr[input_index];
}
    } while(1);
    return 0;
}
Hello seghrein2300,
Include the "<algorithm> header file and use "std::sort" after you have entered everything you need into the array.
Andy
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
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

constexpr size_t ARRAY_SZ {5};

void display(string arr[ARRAY_SZ])
{
	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";
	}
}

int main()
{
	string arr[ARRAY_SZ];

	for (int input_index = 0; input_index < ARRAY_SZ; ++input_index) {
		cout << "----------------------------------------------\n";
		cout << "Product # \t \t Name\n";
		display(arr);
		cout << "----------------------------------------------\n";

		cout << "Enter Product name: ";
		cin >> arr[input_index];
	}

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

my desired output is

-------------------------------
Product #                Name
0                       -NONE-
1                       -NONE-
2                       -NONE-
3                       -NONE-
4                       SHOES
-------------------------------
Enter product name: PANTS
-------------------------------
Product #                Name
0                       -NONE-
1                       -NONE-
2                       -NONE-
3                       PANTS
4                       SHOES
-------------------------------
Enter product name: HAT


ps; sorry i just refreshed site and saw your replies, i will be trying them out.
Last edited on
Hello seghrein2300,
If I understand your desired output correctly you are trying to sort each input after you enter it. This will not work because as you enter into the array you start at element (0) zero and work up. Sorting the array before all the entries are complete would mean that you will overwrite elements 3, 4 and 5 and your final output will not be what you want. Your do/while loop does not work because it is an endless loop with no way out. Look at what seeplus has done to eliminate the do/while loop. This is better. His code is a good start, but still could be etweeked a bit. The "display" function is a good idea, but I do not know if you are ready for that yet. With a little work you could take the contents of the function and put that in "main". Putting the sort inside the for loop I get this:

----------------------------------------------
Product #                Name
0                       -NONE-
1                       -NONE-
2                       -NONE-
3                       -NONE-
4                       -NONE-
----------------------------------------------
Enter Product name: shoes
----------------------------------------------
Product #                Name
0                       -NONE-
1                       -NONE-
2                       -NONE-
3                       -NONE-
4                       shoes
----------------------------------------------
Enter Product name: pants
----------------------------------------------
Product #                Name
0                       -NONE-
1                       -NONE-
2                       -NONE-
3                       pants
4                       shoes
----------------------------------------------
Enter Product name: glove
----------------------------------------------
Product #                Name
0                       -NONE-
1                       -NONE-
2                       glove
3                       pants
4                       shoes
----------------------------------------------
Enter Product name: tie
----------------------------------------------
Product #                Name
0                       -NONE-
1                       -NONE-
2                       glove
3                       shoes
4                       tie
----------------------------------------------
Enter Product name: hat
0                       -NONE-
1                       -NONE-
2                       glove
3                       hat
4                       shoes

// This would be the sorted output.
0                       -NONE-
1                       -NONE-
2                       glove
3                       hat
4                       shoes


Notice after the 3rd entry you are overwriting the last variables in the array until the for loop ends. The better choice here is to fill the array then sort it.

Andy
my desired output is


What about:

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

constexpr size_t ARRAY_SZ {5};

void display(const string arr[ARRAY_SZ])
{
	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";
	}
}

int main()
{
	string arr[ARRAY_SZ];

	for (int input_index = 0; input_index < ARRAY_SZ; ++input_index) {
		cout << "----------------------------------------------\n";
		cout << "Product # \t \t Name\n";

		sort(arr, arr + ARRAY_SZ);
		display(arr);
		cout << "----------------------------------------------\n";

		cout << "Enter Product name: ";
		cin >> arr[0];
	}

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

Last edited on
Hey guys, sorry my internet cut out. I have these in me. But after filling the "0,1,2,3,4" If I try to enter another one instead of replacing what is in 4. it changes what is in 3.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
using namespace std;
int main(){
    constexpr size_t ARRAY_SZ = 5 ;
    string arr[ARRAY_SZ] ;
    do{
    // print_array
    for (int idx = ARRAY_SZ - 1; idx >= 0; --idx){
        cout<< "----------------------------------------------\n";
        cout<< "Product # \t \t Name" << endl;
    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";
    }
    // get_array_elements
    cout<< "----------------------------------------------\n";
        cout << "Enter Product name: " ;
        cin >> arr[idx];
}
    } while(1);
    return 0;
}

OUTPUT AS

-------------------------------
Product #         Name
0                       PANTS
1                       SHIRT
2                       HAT
3                       SOCKS
4                       SHOES
-------------------------------
Enter product name: MONEY
-------------------------------
Product #          Name
0                       PANTS
1                       SHIRT
2                       HAT
3                       MONEY        <-------- THIS IS WHERE IT REPLACES INSTEAD OF
4                       SHOES         <---------- HERE.
-------------------------------
Enter product name:


@seeplus, yes exactly like that but is there a way to loop it?
Last edited on
@seeplus, yes exactly like that but is there a way to loop it?


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

constexpr size_t ARRAY_SZ {5};

void display(const string arr[ARRAY_SZ])
{
	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";
	}
}

int main()
{
	string arr[ARRAY_SZ];
	string line;

	do {
		cout << "----------------------------------------------\n";
		cout << "Product # \t \t Name\n";

		display(arr);
		cout << "----------------------------------------------\n";

		cout << "Enter Product name (CR to terminate): ";
		getline(cin, line);

		if (!line.empty() && line > arr[0]) {
			arr[0] = line;
			sort(arr, arr + ARRAY_SZ);
		}
	} while (!line.empty());
}


This will display ascending from the bottom and only add new items if they are greater than what is displayed.
Thank you, @seeplus.
I'll try to work for now with the code that I recently added, I'm just trying to fix the overwriting in the # 3 instead of #4 if STRINGS is full, I still haven't figured out a good solution yet.
What do you want to happen if arr is full? In my code above a new item is only added if it would be displayed. but ... ???
If arr is full, I want to overwrite what is written on Product # 4, like.

-------------------------------
Product #         Name
0                       PANTS
1                       SHIRT
2                       HAT
3                       SOCKS
4                       SHOES
-------------------------------
Enter product name: APPLE
-------------------------------
Product #         Name
0                       SHIRT
1                       HAT
2                       SOCKS
3                       SHOES
4                       APPLE
-------------------------------
Enter product name: 
Last edited on
But then, what about sort order?

The original sort order is

HAT
PANTS
SHIRT
SHOES
SOCKS

So adding APPLE maintaining sort order gives

HAT
PANTS
SHIRT
SHOES
SOCKS

as APPLE becomes before these 5 and hence isn't added.

Or do you want the new item to be always added and the previous lowest one is replaced giving

APPLE
PANTS
SHIRT
SHOES
SOCKS

where HAT is replaced by APPLE

???

Yeah, I want the new item to be always added and the previous lowest one is the one that is gonna be replaced.
b4:

HAT
PANTS
SHIRT
SHOES
SOCKS

after

PANTS
SHIRT
SHOES
SOCKS
APPLE

where HAT will disappear as the flow goes up(?)
OK. But APPLE would be top, not bottom in the sort order.

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

constexpr size_t ARRAY_SZ {5};

void display(const string arr[ARRAY_SZ])
{
	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";
	}
}

int main()
{
	string arr[ARRAY_SZ];
	string line;

	do {
		cout << "----------------------------------------------\n";
		cout << "Product # \t \t Name\n";

		display(arr);
		cout << "----------------------------------------------\n";

		cout << "Enter Product name (CR to terminate): ";
		getline(cin, line);

		if (!line.empty()) {
			arr[0] = line;
			sort(arr, arr + ARRAY_SZ);
		}
	} while (!line.empty());
}

@seeplus, Thank you! before I mark this solved what can codes can I use to make a program like this? https://imgur.com/a/pKMjZWf

Is it a combination of functions, loops, if/else, arrays?
Yes, could be. Also std::find(). Something like ??

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>
#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];
	string line;

	do {
		display(arr);

		cout << "Enter Product name (CR to terminate): ";
		getline(cin, line);

		if (!line.empty()) {
			arr[0] = line;
			sort(arr, arr + ARRAY_SZ);
		}
	} while (!line.empty());

	do {
		std::string name;

		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;
			}

			sort(arr, arr + ARRAY_SZ);
			display(arr);
		}
	} while (true);
}


Last edited on
Thank you so much, seeplus. That's a very big help. I will be studying the codes you just posted to get me a better explanation.

I will just change the first top program because it's not a connected program.
I will change it to like, the items/products is displayed already. fruits[5] = {Apple, Grapes, Lemon, Mango, Peach}; if that is possible? like:

Item name(s)
[0] = Apple
[1] = Grapes
[2] = Lemon
[3] = Mango
[4] = Peach
Enter item to search: 


Again, thank you so much.
Last edited on
Note case sensitive:

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;
			}
		}
	}
}

Thank you, seeplus. I'm done with the said program.
Topic archived. No new replies allowed.