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

massageBoard.txt It\'s like Craigslist, only different Read the entire assignmen

ID: 3783425 • Letter: M

Question

massageBoard.txt

It's like Craigslist, only different 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 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 thearray If a match is not found in the array, add the item to the array at the first unused position, eg, 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 ctypexspace

Explanation / Answer


#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <stdlib.h>
#include <cstdlib>
using namespace std;

struct saleItem{
    string type;
    int price; //e.g 100, 2987
    string itemStatus; //e.g wanted or for sale.... false or 0 is "for sale"
    //saleItem(){}; //constructor
};

int main(int argc, char *argv[]) {
   int index = 0;
   int counter=0;
   int itemsSold=0;
   int leftOver=0;
   saleItem itemArray[100]; //array from txt file
   saleItem wantItemArray[100];
   saleItem emptySaleItem;
       emptySaleItem.type = "";
       emptySaleItem.itemStatus = "no";
       emptySaleItem.price = 0;
   saleItem indexItem;
  
   for (int r=0; r<100; r++){ //clears array
       itemArray[r] = emptySaleItem;  
       wantItemArray[r] = emptySaleItem;
       counter++;
   }
  
   if (argc==2){ //checks for argument

       ifstream inFile;
       string data; // create an instance
       inFile.open(argv[1]); //open the file from argument

       if (inFile.good()){ //file open error check
           //cout<<"opened successfully"<<endl;
          
           while (getline(inFile, data)) { //read / get every line of the file & store it
               string token;
               stringstream ss(data); // create a string stream variable from string data
               ss << data;
               int indexTwo = 0;
               while (getline(ss, token, ',')){ //delimits by line
                   if (indexTwo == 0){
                       indexItem.type = token;
                   }
                   else if(indexTwo == 1){
                       indexItem.itemStatus = token;
                   }
                   else {
                       indexItem.price = std::stoi(token);
                   }
                   indexTwo++;
                   counter++;
               }

               if (indexItem.itemStatus == " wanted"){
                   wantItemArray[index]=indexItem;
                   itemArray[index]=emptySaleItem;
               }
               else{
                   itemArray[index]=indexItem;
                   wantItemArray[index]=emptySaleItem;
               }

               ss.clear();
               index++;
               counter++;
           }


           for (int q = 0; q<100;q++){
               int wantIndexPrice = wantItemArray[q].price;
               string wantIndexType = wantItemArray[q].type;
               for (int a = 0; a<100; a++){
                   int saleIndexPrice = itemArray[a].price;
                   string saleIndexType = itemArray[a].type;
                   if (wantItemArray[q].itemStatus == "DONE"){
                       //nothing, skip finished requests
                       break;
                   }
                   else{
                       if (saleIndexPrice > 0){ //not empty spot
                           if (wantIndexType == saleIndexType){ //same type
                               if (wantIndexPrice > saleIndexPrice){ //greater price
                                   //cout<<"ITEM FOUND AT: "<<a<<endl;
                                   cout<<itemArray[a].type<<" "<<itemArray[a].price<<endl;
                                   itemArray[a] = emptySaleItem;
                                   wantItemArray[q].itemStatus = "DONE";
                                   itemsSold++;
                                   break;
                               }
                           }
                       }
                   }
                   counter++;
               }
               counter++;
           }
           for (int y=0;y<99;y++){
               if (itemArray[y].itemStatus == "no"){
                   itemArray[y]=itemArray[y+1];
                   itemArray[y+1] = emptySaleItem;
               }
               else{
                   cout<<itemArray[y].itemStatus<<endl;
               }
           }
       }
      
       else {
           //cout<<"File unsuccessfully opened"<<endl;
       }
       for (int t=0;t<100;t++){
           if (itemArray[t].price>0){
               cout<<itemArray[t].type<<" checkcheck "<<itemArray[t].price<<endl;
               leftOver++;
           }
           counter++;
       }
       cout<<"Items Sold"<<itemsSold<<'#'<<endl;
       cout<<"Items remaining in the message board after reading all lines in the file."<<leftOver<<'#'<<endl;
       cout<<"Loop Iterations "<<counter<<endl;
       inFile.close(); //close the file
       }
       else{ // if no argument
           cout<<"ERROR: No Argument"<<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


note: try to compile new version of c++ then only stoi function will work