URGENT HELP NEEDED!

Hi, thank you for the help in advance. I have this project in C++ for AP Computer Science (I know that this isn't generally part of the curriculum, but our teacher aligned it with a college.) The description for the f=project is as follows: "This program should work the same as Program 1, except that for each item, the user will also enter a quantity. Make sure this is guided, and easy for the user. After the user has finished entering items and quantities, the program should "merge" all duplicate items and quantities into a single alphabetized list with quantities." I've had trouble sorting and adding the quantities for some time now. I have attempted to use the "sort" function, however that only sorts the items not the quantities and that's where I get stuck. I thought of using structures, however I have absolutely no idea how to do this and am completely lost on how to actually sort and add the list. Any help would be greatly appreciated. Thanks.


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
123
124
//Fun with Arrays
//Date Modified: 7 Jan, 2017

#include <iostream>
#include <iomanip>
#include <string>
#include <stdio.h>
#include <algorithm>
#include <fstream>
using namespace std;

int main (void) {
	
	//Constant integer that sets the base width for between the item and 
        the quantity

	const int indent = 30;

	//Declares Max number of items that one can enter

	const int array_Size = 256;

	//Defines the Array

	string items[array_Size];

	//Variable for the Quantites

	int quantities[array_Size];

	//Counter for the Number of Items entered

	int count = 0;

	//Counter for the added number of quantities

	int j = 1;
	
	int i = 1;
	
	int deletedItems = 0;
	
	//Text that tells the user what to do.
	cout << "This is a simple program in which you can enter items to 
        create a shopping list." << endl;
	cout << "Please enter the items in which you wish to purchase (When 
        done entering items type 'STOP' to compile your items)" << endl << 
        endl;
	
	//do-while loop that allows the user to enter items into the shopping 
        list.

	do {
		cout << "Enter item " << count+1 << ": ";
		getline(cin, items[count]);
		cout << endl;
		
		if (items[count] != "stop" && items[count] != "STOP") 
		{
			cout << "Enter a Quantity: ";
			cin >> quantities[count];	
		}
		cout << endl;
		cin.ignore();
		++count;
	} while (items[count-1] != "stop" && items[count-1] != "STOP" && count 
        < array_Size);
	
	//eliminates the word "STOP" or "stop" from the final list

	--count;
	
	//Sorts the Items using the sort function included with "#include 
        <algorithm>"	

	sort(items, items + count);
	sort(quantities, quantities + count);
	std::unique(items, items + count);
	
	//Function to eliminate duplicates. 
	for (int i = 1; i < count; ++i) {
		if (items[count] == items[count - 1]) {
			for (int j = i; j < (count - 1); ++j ) {
				items[j] == items[j-1];		
			}
	//	--count;
		}
	--count;
	} 
	++count; 

	while ( i < count-1) {
		if (items[i-1] == items[j-1]) {
			++deletedItems;
			quantities[j-1] = quantities[j-1] + quantities[j];
			items[j-1] = items[i-1];
		}
		else {
			items[j] = items[i-1];
			quantities[j] = quantities[i];
			++j;
		}
	++i;
	}
	
	--count;
	
	//Print sorted list
	cout << endl << endl << "Shopping List: " << endl << endl;
	cout << setfill(' ');
	cout << "Items: " << setw(indent + 7) << "Quantities: " << endl;
	for (int i = 0; i < count; ++i) {	
		cout << i+1 << " " << items[i] << setw(indent-
                (items[i].length()%10)) << ' ';
		cout << quantities[i] << endl;
	}
	
	
	cout << endl << "# of Items Entered: " << count << endl << endl;
	
	system ("pause");
	return 0;
}
You can use a structure to create the shopping cart. This structure will contain the name and the quantity of the item. You can then sort this structure by name by using a bubble sort function. That's your first option.

Your second option is to create parallel arrays and sort those two. Sorting is going to be using bubble sort as well.
Thank you so much fiji885. In regards to the bubble sort function, how exactly do I make that work though? I thought it only worked with integers. In regards to the parallel arrays, I tried doing that and I wasn't able to sort the items with their corresponding quantities. Again, I greatly appreciate the help.
My problem is when I sort the items, it doesn't sort the quantities with it. How do I link the two together? I read that I needed to do that using structures, however I am unable to get that to work. Please advise. Thanks again.
If you want to use parallel arrays, you'll have to write your own sort code so that when you move an item, you also move the corresponding quantity.

If you want to use an existing sort function, you will have to use a single array in which each item is a struct containing the item and the quantity, and provide the function the sort code will use to know what order to put them in.


In regards to the bubble sort function, how exactly do I make that work though? I thought it only worked with integers.

This show a massive misunderstanding of what sorting is. Any sort algorithm can be used to sort any kind of object, as long as it's possible to look at two objects and know which of them should go ahead of the other (or if they're the same). C++ comes with the knowledge already of how to do this with simple objects; int, double, char, and so on. If you create a new kind of object (like a struct) that you want to sort, then you must also provide the function to be used to decide which of any two of those objects goes ahead of the other.
Last edited on
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
#include <iostream>
#include <string>
#include <map>

// merge information for common items
// return the logical size of the array after merging
// std::size_t: http://en.cppreference.com/w/cpp/types/size_t
std::size_t merge( std::string items[], unsigned int quantities[], std::size_t sz )
{
    // define a map which maps unique items to merged quantities
    // https://www.cprogramming.com/tutorial/stl/stlmap.html
    // http://en.cppreference.com/w/cpp/container/map
    std::map< std::string, unsigned int > iems_to_quantities_map ;

    // for each pair item - quantity in the original arrays
    for( std::size_t i = 0 ; i < sz ; ++i )
    {
        // add the quantity to the entry in the map if the item is present
        // otherwise create a new entry with quantity as zero, and then add this quantity
        iems_to_quantities_map[ items[i] ] += quantities[i] ;
    }

    // copy the merged entries back into the two arrays
    std::size_t pos = 0 ; // starting at position zero
    // range-based loop: http://www.stroustrup.com/C++11FAQ.html#for
    for( const auto& pair : iems_to_quantities_map ) // for each item-quantiy pair in the map
    {
        items[pos] = pair.first ; // unique key: item
        quantities[pos] = pair.second ; // mapped value: merged quantity
        ++pos ;
    }

    return pos ; // size after merging
}

int main()
{
    const std::size_t orig_sz = 10 ;
    std::string items[orig_sz]       { "abc", "def", "ghi", "def", "ghi", "def", "abc", "jkl", "abc", "jkl" } ;
    unsigned int quantities[orig_sz] {  92,    25,    37,    52,    64,    88,    21,    50,    37,    64 } ;

    std::cout << "before merge:\n------------\n" ;
    for( std::size_t i = 0 ; i < orig_sz ; ++i ) std::cout << items[i] << " : " << quantities[i] << '\n' ;

    std::cout << "\nafter merge:\n------------\n" ;
    // auto: http://www.stroustrup.com/C++11FAQ.html#auto
    const auto new_sz = merge( items, quantities, orig_sz ) ;
    for( std::size_t i = 0 ; i < new_sz ; ++i ) std::cout << items[i] << " : " << quantities[i] << '\n' ;
}

http://coliru.stacked-crooked.com/a/56b53fce0f9fe03f
I appreciate all of your guys' help. Repeater, I greatly appreciate the clarification and I apologize for the ignorance. If I use the method of parallel arrays, how would I be able to have them be linked when I write the code. Aren't they two separate arrays, thus they wont be able be linked unless put in a structure. Or I presume that the code that JLBorges included does indeed sort two parallel arrays. Apologies if my ignorance trumps me again.

On a separate note, if doing this in a structure, how would one be able to input both items and quantities into the structure and then sort that as a whole, with the items and structures corresponding. Or is this not possible. I read something about using operator overloading, however my knowledge on that is minimal even after reading several articles on it. Please advise.

Thank you all again for your continued help. I greatly appreciate it.
Also a Side Note:

JLBorges, It does indeed state that I am unable to use any "pre-made" merge functions. Would the function that you have as "merge" constitute as that, or am I having a misunderstanding. Please Advise. Thanks so much again.
> I am unable to use any "pre-made" merge functions.
> Would the function that you have as "merge" constitute as that,

Though I called the function 'merge', it really is just a consolidation operation. There is no real merge involved; it does not use any kind of 'merge' function. Perhaps, a better name for the function would have been either 'consolidate' or 'coalesce'.

It however does use the standard library container std::map<> (an associative array) to associate item names with corresponding quantities. However, to the archetypal "career teacher", the intelligent use of a standard library facility would be like a red rag to a bull.

Here is a (less efficient) version which does not use any standard library facility other than the ones which were already in use. (Note that here too, there no real 'merge' operation.):

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

// find the string what in array items.
// if found, return the position at which it is found
// if not found, return sz (an invalid position)
std::size_t find( const std::string items[], std::size_t sz, const std::string& what )
{
    for( std::size_t i = 0 ; i < sz ; ++i ) if( items[i] == what ) return i ;
    return sz ;
}

int main()
{
    const std::size_t orig_sz = 10 ;
    std::string items[orig_sz]       { "abc", "def", "ghi", "def", "ghi", "def", "abc", "jkl", "abc", "jkl" } ;
    unsigned int quantities[orig_sz] {  92,    25,    37,    52,    64,    88,    21,    50,    37,    64 } ;

    std::cout << "original:\n------------\n" ;
    for( std::size_t i = 0 ; i < orig_sz ; ++i ) std::cout << items[i] << " : " << quantities[i] << '\n' ;

    // merge
    // createtwo auxiliary arrays to hold the results of the merge
    std::string merged_items[orig_sz] ; // initially, all empty strings
    unsigned int merged_quantities[orig_sz] {0} ; // initially, all zeroes
    std::size_t merged_pos = 0 ; // next position into the merged arrays (initially zero)

    for( std::size_t i = 0 ; i < orig_sz ; ++i )
    {
        // check if items[i] is already in the merged array
        const std::size_t found_at = find( merged_items, merged_pos, items[i] ) ;
        if( found_at < merged_pos ) // if it was found
        {
            // add the quantity to the corresponding position in merged_quantities
            merged_quantities[found_at] += quantities[i] ;
        }
        else // not found
        {
            // append item and quantity to the respective merged arrays
            merged_items[merged_pos] = items[i] ;
            merged_quantities[merged_pos] = quantities[i] ;
            ++merged_pos ; // and move to the next position in he merged arrays
        }
    }

    // at this point, merged_pos holds the logical size of the merged array
    const std::size_t merged_sz = merged_pos ; // this is just for elucidation

    // print out the merged_sz values in the merged arrays
    std::cout << "\nmerged:\n------------\n" ;
    for( std::size_t i = 0 ; i < merged_sz ; ++i )
        std::cout << merged_items[i] << " : " << merged_quantities[i] << '\n' ;
}

http://coliru.stacked-crooked.com/a/acc230296d3694af
Ok, so I answered my own question above ^^ (the "Side Note") and I modified the program to be able to ask the user for an item and a quantity corresponding to one another. After I did that however, I found out that the merged list will not print any item that comes alphabetically after "stop." I have attempted to find multiple ways of solving this, however am coming up short. Please advise on how I may be able to fix this problem.

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

using namespace std;

// merge information for common items
// return the logical size of the array after merging
// size_t: http://en.cppreference.com/w/cpp/types/size_t
size_t merge( string items[], unsigned int quantities[], size_t sz )
{
    // define a map which maps unique items to merged quantities
    // https://www.cprogramming.com/tutorial/stl/stlmap.html
    // http://en.cppreference.com/w/cpp/container/map
    map< string, unsigned int > iems_to_quantities_map ;

    // for each pair item - quantity in the original arrays
    for( size_t i = 0 ; i < sz ; ++i )
    {
        // add the quantity to the entry in the map if the item is present
        // otherwise create a new entry with quantity as zero, and then add this quantity
        iems_to_quantities_map[ items[i] ] += quantities[i] ;
    }

    // copy the merged entries back into the two arrays
    size_t pos = 0 ; // starting at position zero
    // range-based loop: http://www.stroustrup.com/C++11FAQ.html#for
    for( const auto& pair : iems_to_quantities_map ) // for each item-quantiy pair in the map
    {
        items[pos] = pair.first ; // unique key: item
        quantities[pos] = pair.second ; // mapped value: merged quantity
        ++pos;
    }

    return pos ; // size after merging
}

int main()
{
		
	int count = 0;
    const size_t orig_sz = 256 ;
    unsigned int quantities[orig_sz];
	string items[orig_sz];
    do {    
	cout << "Enter an item " << count + 1 << " : ";
	getline(cin,items[count]) ;
	cout << endl;
	if (items[count] != "stop" && items[count] != "STOP") {
		cout << "Enter a Quantity: ";
		cin >> quantities[count];	
	}
	cout << endl << endl;
	cin.ignore();
	++count;
    } while (items[count-1] != "stop" && items[count-1] != "STOP" && count < orig_sz);

	
    cout << "before merge:\n------------\n" ;
    for( size_t i = 0 ; i < count-1 ; ++i ) cout << items[i] << " : " << quantities[i] << '\n' ;
    --count;

    cout << "\nafter merge:\n------------\n" ;
    // auto: http://www.stroustrup.com/C++11FAQ.html#auto
    const auto new_sz = merge( items, quantities, orig_sz );
    for( size_t i = 1 ; i < count; ++i ) cout << items[i] << " : " << quantities[i] << '\n' << 
    endl;

	system("pause");
	return 0;
}

Last edited on
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
#include <iostream>
#include <string>
#include <map>
#include <cctype>

using namespace std;

// merge information for common items
// return the logical size of the array after merging
// size_t: http://en.cppreference.com/w/cpp/types/size_t
size_t merge( string items[], unsigned int quantities[], size_t sz )
{
    // define a map which maps unique items to merged quantities
    // https://www.cprogramming.com/tutorial/stl/stlmap.html
    // http://en.cppreference.com/w/cpp/container/map
    map< string, unsigned int > iems_to_quantities_map ;

    // for each pair item - quantity in the original arrays
    for( size_t i = 0 ; i < sz ; ++i )
    {
        // add the quantity to the entry in the map if the item is present
        // otherwise create a new entry with quantity as zero, and then add this quantity
        iems_to_quantities_map[ items[i] ] += quantities[i] ;
    }

    // copy the merged entries back into the two arrays
    size_t pos = 0 ; // starting at position zero
    // range-based loop: http://www.stroustrup.com/C++11FAQ.html#for
    for( const auto& pair : iems_to_quantities_map ) // for each item-quantiy pair in the map
    {
        items[pos] = pair.first ; // unique key: item
        quantities[pos] = pair.second ; // mapped value: merged quantity
        ++pos;
    }

    return pos ; // size after merging
}

// get item, quantity from user input
// return false if stop was entered to signal end of input
bool get_item( std::string& item, unsigned int& qty, int slno )
{
    std::cout << "Enter item #" << slno << " : ";
    std::getline( std::cin, item ) ;

    if( item == "STOP" || item == "stop" ) return false ;

    std::cout << "Enter Quantity: " ;
    std::cin >> qty ;
    std::cin.ignore( 1000, '\n' ) ;
    return true ;
}

int main()
{
	unsigned int count = 0;
    const size_t max_sz = 256 ;
    unsigned int quantities[max_sz];
	string items[max_sz];

    // for each item, quantity read, increment the count of items
    while( count < max_sz && get_item( items[count], quantities[count], count+1 ) ) ++count ;

    cout << "before merge:\n------------\n" ;
    for( size_t i = 0 ; i < count ; ++i ) cout << items[i] << " : " << quantities[i] << '\n' ;

    cout << "\nafter merge:\n------------\n" ;
    // auto: http://www.stroustrup.com/C++11FAQ.html#auto
    // *** count is the logical size of the array before merge
    const auto new_sz = merge( items, quantities, count ); // ****

    // *** and new_size is the logical size of the array after merge
    // start with position 0, upto position new_sz-1
    for( size_t i = 0 ; i < new_sz; ++i ) cout << items[i] << " : " << quantities[i] << '\n' ;

	// system("pause");
	return 0;
}
Last edited on
I read something about using operator overloading


Operator overloading is a fancy way of saying writing your own functions named + or - or some other such. Don't worry about it.
Thank you Repeater and JLBorges for all of your help. You have made this part of the assignment bearable and easier to do than what I was attempting to do previously. Many thanks again for bearing with my ignorance. Thank you.
According to the next part in the problem, I have to have the user input two separate list and then merge them together and output it into one text file. The full description is as follows:

"This program will allow the user to enter two separate shopping lists into TEXT FILES. The program MUST input each text file into a separate list/array/set of arrays, and then display both lists separately on the screen. After taking the lists in, the program should merge and sort both lists. Then it should output the final list BOTH to the screen AND another TEXT FILE."

Please help.

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
#include "funWithArraysShoppingListWithQuantitiesThatOutputsToTextFiles2FUNCTIONS.h"

using namespace std;

// merge information for common items
// return the logical size of the array after merging
// size_t: http://en.cppreference.com/w/cpp/types/size_t
size_t merge( string items[], unsigned int quantities[], size_t sz )
{
    // define a map which maps unique items to merged quantities
    // https://www.cprogramming.com/tutorial/stl/stlmap.html
    // http://en.cppreference.com/w/cpp/container/map
    map< string, unsigned int > iems_to_quantities_map ;

    // for each pair item - quantity in the original arrays
    for( size_t i = 0 ; i < sz ; ++i )
    {
        // add the quantity to the entry in the map if the item is present
        // otherwise create a new entry with quantity as zero, and then add this quantity
        iems_to_quantities_map[ items[i] ] += quantities[i] ;
    }

    // copy the merged entries back into the two arrays
    size_t pos = 0 ; // starting at position zero
    // range-based loop: http://www.stroustrup.com/C++11FAQ.html#for
    for( const auto& pair : iems_to_quantities_map ) // for each item-quantiy pair in the map
    {
        items[pos] = pair.first ; // unique key: item
        quantities[pos] = pair.second ; // mapped value: merged quantity
        ++pos;
    }

    return pos ; // size after merging
}

// get item, quantity from user input
// return false if stop was entered to signal end of input
bool get_item( string& item, unsigned int& qty, int slno )
{
    cout << "Enter item #" << slno << " : ";
    getline( cin, item ) ;

    if( item == "STOP" || item == "stop" ) return false ;

    cout << "Enter Quantity: " ;
    cin >> qty ;
    cin.ignore( 1000, '\n' ) ;
    return true ;
}

int main()
{
	ofstream myfile;
	myfile.open("shoppingList.txt");
	
	unsigned int count = 0;
    const size_t array_Size = 256 ;
    unsigned int quantities[array_Size];
	string items[array_Size];

    // for each item, quantity read, increment the count of items
    while( count < array_Size && get_item( items[count], quantities[count], count+1 ) ) ++count ;

    cout << "before merge shopping List:\n------------\n" ;
    for( size_t i = 0 ; i < count ; ++i ) cout << items[i] << " : " << quantities[i] << '\n' ;

    myfile << "\nafter merge Shopping List:\n------------\n" ;
    // auto: http://www.stroustrup.com/C++11FAQ.html#auto
    // *** count is the logical size of the array before merge
    const auto new_sz = merge( items, quantities, count ); // ****

    // *** and new_size is the logical size of the array after merge
    // start with position 0, upto position new_sz-1
    for( size_t i = 0 ; i < new_sz; ++i ) myfile << items[i] << " : " << quantities[i] << '\n' ;

	// system("pause");
	return 0;
}
Ok, so I figured out how to do multiple parallel arrays, however I am having a hard time sorting properly according to the specifications that I am supposed to follow(as seen ^^^). Please advise. When items are inputted, it doesn't properly display the second array as a standalone array. Also, is there a way to be able to have the user input as many arrays as they want and then display them and sort them like I need for this part of the assignment? This is an extra credit part of the assignment. It offers some heavy extra credit which I need.
Thank you so much again. Please help.

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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <iostream>
#include <string>
#include <map>
#include <cctype>
#include <iomanip>
#include <fstream>
using namespace std;

// merge information for common items
// return the logical size of the array after merging
// size_t: http://en.cppreference.com/w/cpp/types/size_t
size_t merge( string items[], unsigned int quantities[], size_t sz )
{
    // define a map which maps unique items to merged quantities
    // https://www.cprogramming.com/tutorial/stl/stlmap.html
    // http://en.cppreference.com/w/cpp/container/map
    map< string, unsigned int > iems_to_quantities_map ;

    // for each pair item - quantity in the original arrays
    for( size_t i = 0 ; i < sz ; ++i )
    {
        // add the quantity to the entry in the map if the item is present
        // otherwise create a new entry with quantity as zero, and then add this quantity
        iems_to_quantities_map[ items[i] ] += quantities[i] ;
    }

    // copy the merged entries back into the two arrays
    size_t pos = 0 ; // starting at position zero
    // range-based loop: http://www.stroustrup.com/C++11FAQ.html#for
    for( const auto& pair : iems_to_quantities_map ) // for each item-quantiy pair in the map
    {
        items[pos] = pair.first ; // unique key: item
        quantities[pos] = pair.second ; // mapped value: merged quantity
        ++pos;
    }

    return pos ; // size after merging
}

size_t merge2( string items[], unsigned int quantities[], size_t sz )
{
    // define a map which maps unique items to merged quantities
    // https://www.cprogramming.com/tutorial/stl/stlmap.html
    // http://en.cppreference.com/w/cpp/container/map
    map< string, unsigned int > iems_to_quantities_map ;

    // for each pair item - quantity in the original arrays
    for( size_t i = 0 ; i < sz ; ++i )
    {
        // add the quantity to the entry in the map if the item is present
        // otherwise create a new entry with quantity as zero, and then add this quantity
        iems_to_quantities_map[ items[i] ] += quantities[i] ;
    }

    // copy the merged entries back into the two arrays
    size_t pos = 0 ; // starting at position zero
    // range-based loop: http://www.stroustrup.com/C++11FAQ.html#for
    for( const auto& pair : iems_to_quantities_map ) // for each item-quantiy pair in the map
    {
        items[pos] = pair.first ; // unique key: item
        quantities[pos] = pair.second ; // mapped value: merged quantity
        ++pos;
    }

    return pos ; // size after merging
}

// get item, quantity from user input
// return false if stop was entered to signal end of input

bool get_item( string& item, unsigned int& qty, int slno )
{
    cout << "(List 1) Enter item #" << slno << " : ";
    getline( cin, item ) ;

    if( item == "STOP" || item == "stop" ) return false ;

    cout << "Enter Quantity: " ;
    cin >> qty ;
    cin.ignore( 1000, '\n' ) ;
    return true ;
}

bool get_item2( string& item, unsigned int& qty, int slno2)
{
    cout << endl << "(List 2) Enter item #" << (slno2+1) << " : ";
    getline( cin, item ) ;

    if( item == "STOP" || item == "stop" ) return false ;

    cout << "Enter Quantity: " ;
    cin >> qty ;
    cin.ignore( 1000, '\n' ) ;
    cout << endl;
    return true ;
}

int main()
{
	ofstream myfile;
	myfile.open("shoppingList.txt");
	
	unsigned int count = 0;
    const size_t array_Size = 256 ;
    unsigned int quantities[array_Size];
	string items[array_Size];

    // for each item, quantity read, increment the count of items
    while( count < array_Size && get_item( items[count], quantities[count], count+1 ) ) ++count ;

    cout << "before merge shopping List:\n------------\n"  << endl;
    for( size_t i = 0 ; i < count ; ++i ) cout << items[i] << " : " << quantities[i] << '\n';
    
	myfile << "\n-------------";
	cout << "\n-------------";

    myfile << "\nafter merge Shopping List 1:\n-------------\n" << endl;
    cout << "\nafter merge Shopping List 1:\n-------------\n" << endl;
    // auto: http://www.stroustrup.com/C++11FAQ.html#auto
    // *** count is the logical size of the array before merge
    const auto new_sz = merge2( items, quantities, count ); // ****

    // *** and new_size is the logical size of the array after merge
    // start with position 0, upto position new_sz-1
    for( size_t i = 0 ; i < new_sz; ++i ) {
		myfile << items[i] << " : " << quantities[i] << '\n' ;
		cout << items[i] << " : " << quantities[i] << '\n' ;
	}

	myfile << endl << "-------------";
	cout << endl << "-------------";


	//FOR SECOND LIST
	    // for each item, quantity read, increment the count of items
    while( count < array_Size && get_item2( items[count], quantities[count], count+1 ) ) ++count ;

    cout << endl <<"before merge shopping List 1 and 2:\n------------\n" << endl;
    
    for( size_t k = 0 ; k < count ; ++k ) cout << items[k] << " : " << quantities[k] << '\n' ;

    myfile << "\nafter merge Shopping List 1 and 2:\n------------\n" << endl;
    cout << "\nafter merge Shopping List 1 and 2:\n------------\n"  << endl;
	// auto: http://www.stroustrup.com/C++11FAQ.html#auto
    // *** count is the logical size of the array before merge
    const auto new_sz2 = merge( items, quantities, count ); // ****

    // *** and new_size is the logical size of the array after merge
    // start with position 0, upto position new_sz-1
    for( size_t k = 0 ; k < new_sz; ++k ) {
		myfile << items[k] << " : " << quantities[k] << '\n' ;	
		cout << items[k] << " : " << quantities[k] << '\n' ;
	}
	// system("pause");
	return 0;
} 
Topic archived. No new replies allowed.