Community Garage Sale Solution

$30.00 $24.90


Read the entire assignment carefully before beginning. In this assignment, you’re going to develop a simulated community message board that monitors items wanted and items for sale and looks for matches. When a match is found, e.g. there is a bike for sale for $50 and a bike wanted, where the buyer will pay up to $60, then the item is removed from the message board.

There is a file on Moodle called garageSale.txt that includes up to 100 wanted or for sale items in five categories: bike, microwave, dresser, truck, or chicken. Each line in the file is one item. Your program needs to open the file, read each line, and use an array of structs to store the available items. You can assume there will never be more than 100 lines in the file, therefore, you can declare an array of structs with a fixed size of 100 to represent the items available on the message board. Each struct represents an item and has a type, such as bicycle or truck, a price, and whether it is for sale or wanted. (You can treat for sale or wanted as an integer or Boolean, where 0 is for sale and 1 is wanted, for example.)

Your program needs to read the file until it reaches the end, and you can’t assume there will always be 100 lines, there may be less. As lines are read from the file, you need to check if there is a match with the existing items in the message board. There are two options to consider:

Match is not found in the array

If a match is not found in the array, add the item to the array at the first unused position, e.g. if there are four items, add the item to position five.

Match is found in the array

If a match is found, use the first match found and stop searching the array. Do not add the new item read from the file to the array. Remove the matched item from the array and shift the array to fill the gap left by the removed item. (Section 3.2.4 of your book shows the algorithm for deleting an item from an array and shifting.) Write the action performed to the terminal, formatted as <type><space><price>, such as bike 50. Your printing should be done with the command:

cout<<itemArray[x].type<<” “<<itemArray[x].price<<endl;

where itemArray is the array of structs and x is the index where the item was found in the array. The type is one of the following: bike, microwave, dresser, truck, or chicken. The price is the actual item cost, not what the user is willing to pay.

Other things your program needs to do

Handle the file name as a command line argument

Require the user to enter the name of the file to open as an argument to the program. When the TAs test your code, the filename we use will not be garageSale.txt. We will run your program from the command line, such as

./Assignment1 testFile.txt

where Assignment1 is the name of the executable they build when they compile your code, and testFile.txt in the filename. Your program needs to use argv[1] as the filename in the ifstream.

Print array contents after all lines read from file

After all lines have been read from the file and all possible matches have been made, there will be items left in the array that no one wanted. Include a function in your program that prints out the final state of the message board, and call the function after displaying the matched items. The function parameters and return values are at your discretion, but the function needs to correctly print the contents of the array using the command:

cout<<itemArray[x].type<<”, ”<<”for sale”<<”, “<<itemArray[x].price<<endl;

for “for sale” items and

cout<<itemArray[x].type<<”, ”<<”wanted”<<”, “<<itemArray[x].price<<endl;

for “wanted” items

Format and ordering of program output

It’s important that the output of your program is formatted and ordered in a certain way. You should use the cout statements given in the above sections and output your results in the following order:

Items sold.


Items remaining in the message board after reading all lines in the file.


Don’t output anything other than what’s specified.

How to know if your output is correct?

There is a Piazza forum for this class and you are welcome to post the output you get for the garageSale.txt file on Piazza and ask if other students get the same answer. If you want to test your code with a smaller data set, create a new .txt file and use that file as the input file when you run your code. Please don’t post your code on Piazza.

What is a Struct?

For more information on what structs are and how to create them, there are links on Moodle under Useful YouTube Videos that describe structs in more detail

Submitting Your Code:

Submit your assignment to Moodle

Submit your .cpp file through Moodle using the Assignment 1 Submit link. Make sure your code is commented enough to describe what it is doing. Include a comment block at the top of the .cpp file with your name, assignment number, and course instructor.

What to do if you have questions

There are several ways to get help on assignments in 2270, and depending on your question, some sources are better than others. There is a discussion forum on Piazza that is a good place to post technical questions, such as how to shift an array. When you answer other students’ questions on Piazza, please do not post entire assignment solutions. The CAs are also a good source of technical information, especially questions about C++. If, after reading the assignment write-up, you need clarification on what you’re being asked to do in the assignment, the TAs and the Instructor are better sources of information than the CAs. We will be monitoring Piazza regularly.