URGENT HELP NEEDED Pt2!!!

Hi so this is another part to the "URGENT HELP NEEDED!" post I made yesterday. I figured out how to do everything in regards to sorting single parallel arrays and merging them together, obviously with the immense help of the community, however I am lost at another part of this assignment. The third part states:

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

As well as a bonus (which I desperately need):
"Include search functionality for the "final list" in Program #3.
Make PROGRAM 4 - which would be Program 3 (The quoted text above), but now it should handle as many lists as the user wants to enter. You may accomplish this using any method."

My code is as follows:
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
  #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;
} 
This program will allow the user to enter two separate shopping lists into TEXT FILES


The program you have currently created the list in one whole text file... Is it now wanting you to create a function that will write in separate files?

If that is what it wants, it is easy. After you are done reading input from the first list, you just have to open a different text file with the ofstream object.

but now it should handle as many lists as the user wants to enter. You may accomplish this using any method


This can be easily accomplished by using vectors. You will have to change your code, however.
www.cplusplus.com/reference/vector/vector/

If you want to read input from a file, do something like 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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
    ifstream infile; // create object
    infile.open("list.txt");
    if(!infile) // If fail to open list (doesn't exist or some other error)
    {
        cerr << "Error: unable to open list" << endl;
        return -1;
    }
    else
    {
        while(!infile.eof())
        {
            int read_itemQuantity;
            string read_itemName;
            getline(infile, read_itemName, ':');
            infile >> read_itemQuantity;
            infile.ignore();
            cout << read_itemName << ": " << read_itemQuantity << endl;
            if(infile.peek() == infile.eof()) // prevents from printing extra crap
                break;
        }
    }


    return 0;
}


Below is the list text file I am reading from:

1
2
3
pineapple:10
apple:20
suction cup:100


Edit: Code itself isn't good, as it's a little buggy. But it's just for example. You obviously don't want to use it exactly.

More edit: Fixed my code...
Last edited on
I appreciate the clarification fiji885, however how would this look like in my code? Would I need to just replace the cins' with infile? If not how would I implement this feature into the code I have. I understand the concept, however am lost on trying to implement it. I apologize for my ignorance. Thank you for the help in advance.
Topic archived. No new replies allowed.