Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In this assignment, you’re going to develop a simulated community message board

ID: 3783244 • Letter: I

Question

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 messageBoard.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 an input from the user

Require the user to enter the name of the file to open.

cin>>filename;

or

getline(cin, filename);

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.

Count loop iterations

As part of your program, you need to count the number of times that loops execute. Iteration of a loop is an example of an operation that scales with the size of the input data that needs to be processed. The block of code that reads each line from the file depends on a loop because the number of lines in the file can change if you use a different input file. The code needed to search the message board, or shift data in the array, also scale with data input because the size of the message board will change as items are added and removed.

At the end of your program, after displaying the items left on the message board, display the total number of loop iterations needed to read the file, search the array, and shift the array when an item is deleted. Each count should be displayed on its own line, formatted as:

cout<<”file read:”<<fileCounter<<endl; cout<<”search array:”<<searchCounter<<endl; cout<<”shift array:”<<shiftCounter<<endl;

Loop iterations example

After reading the first line in the file, your fileCounter variable should be 1 for reading one line from the file.

When the first sale occurs, all loop iterations needed to complete the sale should include: 1 for the read from the file, x for the number of iterations it takes to find the matching item in the array, and y for each shift in the contents of the array.

Format and ordering of program output

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.
# Loop Iterations

Explanation / Answer

//main.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

using namespace std;

struct notCraigslist{
int status;
string type;
int value;
};

void notSold(notCraigslist A[], int index){
int counter = 0;
for(int z = 0 ; z < index ; z++){
if(A[z].status == 2 ){
cout<<A[z].type<< ", for sale, "<<A[z].value<<endl;
counter++;
} else if (A[z].status == 1 ){
cout<<A[z].type<< ", wanted, " <<A[z].value<<endl;
counter++;
} else{continue;}
}
cout<<counter<<" unsold items"<<endl;
}

int main(int argc, char *argv[])
{
int arraySIZE = 100;
notCraigslist itemArray[arraySIZE];

//Open the file and check if it has the file
ifstream inFile;
inFile.open(argv[1]);
if(inFile.fail()){
cout<<"The file was not successfully opened"<<endl;
}
//Now to read the file
int place = 0;//coutner or index
string descript;
int itemsSold = 0;
int loopIterations = 0;

while(getline(inFile, descript, ' ')){
loopIterations++;
//Find and set TYPE
if(descript.find("chicken") != string::npos){// Looks for word then sets it as word.
itemArray[place].type = "chicken"; // Would've used stringstream except there's an comma next to word and for sale is 2 words where wanted is 1
} else if(descript.find("microwave") != string::npos){
itemArray[place].type = "microwave";
} else if(descript.find("truck") != string::npos){
itemArray[place].type = "truck";
} else if(descript.find("dresser") != string::npos){
itemArray[place].type = "dresser";
} else if(descript.find("chicken") != string::npos){
itemArray[place].type = "chicken";
} else if(descript.find("bike") != string::npos){
itemArray[place].type = "bike";
} else{cout<<"could not find TYPE"<<endl;}

//Determining and setting status:: for sale = 2 wanted = 1 ----
string t, f, s;
int value;
if(descript.find("sale") != string::npos){
itemArray[place].status = 2;
stringstream ss(descript); //Finds the value of the item
ss >> t >> f >> s >> value;
itemArray[place].value = value;
}
else if(descript.find("wanted") != string::npos){
itemArray[place].status = 1;
stringstream ss(descript);
ss >> t >> s >> value;
itemArray[place].value = value;
}
else{cout<<"could not find STATUS"<<endl;}

//Finding Appropriate matches
for(int x = 0; x < place; x++){
loopIterations++;
if(itemArray[place].type == itemArray[x].type){//Checks to see if they are even the same type

if(itemArray[place].status != itemArray[x].status){// makes sure they are no both for sale etc.
if(itemArray[place].status == 2){
if(itemArray[place].value <= itemArray[x].value){// checks to tee if the price meets the buyer's/sellers needs
cout<<itemArray[place].type<<" "<<itemArray[place].value<<endl;
for(int n = x ; n < place ; n++){//Shifts the elements in the array over
loopIterations++;
itemArray[n] = itemArray[(n+1)];
}
place -= 1;//since item[place] has been shifted over 1
itemArray[place] = {}; //Erase that cell
place -= 1;//so we start at the empty cell in the next iteration
itemsSold++;
break;//exits the large for loop
} else{ continue; }
} else if(itemArray[place].status == 1){
if(itemArray[place].value >= itemArray[x].value){
cout<<itemArray[place].type<<" "<<itemArray[x].value<<endl;
for(int n = x ; n < place ; n++){
loopIterations++;
itemArray[n] = itemArray[(n+1)];
}
place -= 1;
itemArray[place] = {};
place -= 1;
itemsSold++;
break;
} else{ continue; }
} else{ cout<<"ERROR"<<endl;}
} else{ continue; }

} else{ continue; }
}
place++;
}
inFile.close();
cout<<"#"<<endl;
cout<<"Items Sold: "<<itemsSold<<endl;
notSold(itemArray, place);
cout<<"#"<<endl;
cout<<"Loop iterations: "<<loopIterations<<endl;

return 0;
}

======================================================================

messageBoard.txt

chicken, for sale, 60
microwave, wanted, 201
bike, for sale, 60
bike, wanted, 50
microwave, for sale, 200
chicken, for sale, 25
chicken, wanted, 25
microwave, wanted, 10
microwave, for sale, 2
chicken, for sale, 100
bike, wanted, 100
chicken, for sale, 5
truck, wanted, 1000
bike, for sale, 50
chicken, for sale, 5
bike, for sale, 500
chicken, for sale, 1
chicken, for sale, 25
bike, wanted, 60
truck, wanted, 2000
truck, for sale, 2500
bike, wanted, 100
truck, for sale, 300
bike, for sale, 100
chicken, for sale, 10000
truck, for sale, 2000
truck, wanted, 1000
dresser, for sale, 20
truck, wanted, 9000
truck, wanted, 8000
truck, for sale, 4000
dresser, for sale, 2
dresser, wanted, 800
microwave, wanted, 70
truck, for sale, 2000
truck, for sale, 2000
truck, wanted, 1000
microwave, for sale, 60
dresser, for sale, 2000
dresser, wanted, 60
dresser, wanted, 50
truck, wanted, 1000
truck, for sale, 500
truck, for sale, 1500
dresser, for sale, 100
dresser, wanted, 200
dresser, for sale, 450
truck, for sale, 2000
truck, wanted, 1000
truck, for sale, 500
dresser, for sale, 500
dresser, wanted, 200
chicken, wanted, 5
chicken, for sale, 5
truck, wanted, 1000
chicken, for sale, 1
chicken, for sale, 25
chicken, wanted, 16
truck, wanted, 2000
chicken, for sale, 15
chicken, wanted, 5
chicken, for sale, 2
microwave, wanted, 15
microwave, wanted, 75
microwave, for sale, 65
chicken, for sale, 5
bike, for sale, 50
chicken, for sale, 1
chicken, for sale, 25
bike, for sale, 50
chicken, wanted, 25
microwave, wanted, 10
microwave, for sale, 2
chicken, for sale, 1
chicken, for sale, 25
bike, for sale, 50
chicken, wanted, 25
bike, wanted, 30
chicken, wanted, 16
chicken, for sale, 15
microwave, wanted, 70
microwave, for sale, 60
microwave, wanted, 50
bike, wanted, 75
truck, wanted, 1000
microwave, wanted, 201
microwave, for sale, 200
truck, wanted, 1000
chicken, for sale, 25
chicken, wanted, 25
truck, wanted, 1000
microwave, wanted, 10
bike, for sale, 10
truck, for sale, 2000
microwave, for sale, 2
truck, wanted, 4000
truck, for sale, 2000
microwave, for sale, 2
truck, wanted, 4000
bike, for sale, 10

=========================================================================

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote