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

C++ problem, please help me out! The question require me to read the txt file an

ID: 3783784 • Letter: C

Question

C++ problem, please help me out!

The question require me to read the txt file and count how many times file read,array search and array shift.

I have already set int fileCounter = 0;
    int searchCounter = 0;
    int shiftCounter = 0; and answer is 100,1506,276 but I don't know how to get it.

Here is my code:

struct Board{
    string type;
    int price;
    int statue;

};


void itemLeft(Board itemList[],int index){
    int cnt2 = 0;
    for(int i=0;i<index;i++){
        if(itemList[i].statue == 1 ){
        cout<<itemList[i].type<< ", for sale, "<<itemList[i].price<<endl;
        cnt2++;
        } else if (itemList[i].statue == 0 ){
            cout<<itemList[i].type<< ", wanted, " <<itemList[i].price<<endl;
            cnt2++;
        } else{continue;}
    }
    cout<<cnt2<<" unsold items"<<endl;
}


int main(){
    Board itemList[200];
    int loopCnt = 0;
    int fileCounter = 0;
    int searchCounter = 0;
    int shiftCounter = 0;
    int itemSold = 0;
#include <string.h>
    string fileName;
    cout<<"Please enter the file name, include the.txt"<<endl;
    cin>>fileName;

    ifstream ifs;
    ifs.open(fileName.c_str());
    //ifs.open("cjj.txt");
    if(ifs.fail()){
        cout<<"failed to open"<<endl;
    }
    else{
        string item;
        int cnt = 0;
        int value;
        string a,b,c;
        while(getline(ifs,item,' ')){///while loop start here
            loopCnt++;
//To recognition the type
            if(item.find("chicken")!= string::npos){
                itemList[cnt].type = "chicken";
            }else if(item.find("microwave")!= string::npos){
                itemList[cnt].type = "microwave";
            }else if(item.find("bike")!= string::npos){
                itemList[cnt].type = "bike";
            }else if(item.find("truck")!= string::npos){
                itemList[cnt].type = "truck";
            }else if(item.find("dresser")!= string::npos){
                itemList[cnt].type = "dresser";
            }
//To recognition the statue 0 = wanted   1 = sale
            if(item.find("wanted")!= string::npos){
                itemList[cnt].statue = 0;
                stringstream ss(item);
                ss>>a>>c>>value;
                itemList[cnt].price = value;
            }else if(item.find("sale")!= string::npos){
                itemList[cnt].statue = 1;
                stringstream ss(item);
                ss>>a>>b>>c>> value;
                itemList[cnt].price = value;
            }
            //For efficiency purpose,I will find what item will be sold inside this while loop and count the number

            for(int h =0;h<cnt;h++){///find match start here
                loopCnt++;
                if(itemList[cnt].type == itemList[h].type){ //Same type is the first requirement!
                    if(itemList[cnt].statue != itemList[h].statue){ //then the item should be different one for sale, one wanted
                        if(itemList[cnt].statue == 1){         /// If this item is for sale
                            if(itemList[cnt].price<=itemList[h].price){ // then it need to meet the price requirement
                                cout<<itemList[cnt].type<<" "<<itemList[cnt].price<<endl; //sold!
                                for(int y = h ; y < cnt ; y++){//Shifts the elements
                                    loopCnt++;

                                    itemList[y] = itemList[(y+1)];
                                }
                                cnt-=1;//itemList[cnt] has been shifted over 1

                                itemList[cnt] = {}; //delete it and because cnt-=1 last line so just like remove last item
                                cnt-=1;//start at the empty cell in the next iteration
                                itemSold++;
                                break; ///Finish sold process here=====================

                            }else{continue;}
                        }else if(itemList[cnt].statue == 0){ ///wanted
                            if(itemList[cnt].price >= itemList[h].price){
                                cout<<itemList[cnt].type<<" "<<itemList[h].price<<endl;
                                for(int y = h ; y < cnt ; y++){//Shifts the elements
                                    loopCnt++;
                                    itemList[y] = itemList[(y+1)];

                                }
                                cnt-=1;//itemList[cnt] has been shifted over 1
                                itemList[cnt] = {}; //delete it and because cnt-=1 last line so just like remove last item
                                cnt-=1;//start at the empty cell in the next iteration
                                itemSold++;
                                break; ///Finish sold process here=====================
                            }else{continue;}
                        }else{cout<<"Something go wrong here line 98"<<endl;}
                    }else{continue;}
                }else{continue;} //make sure the code can still run if it did not match the requirement before

            }///find match end here


            cnt++;
        }///while end here
    ifs.close();

    cout<<"Items Sold: "<<itemSold<<endl;
    cout<<"#"<<endl;
    itemLeft(itemList, cnt);
    cout<<"#"<<endl;
    cout<<"Loop iterations: "<<shiftCounter<<endl;
    return 0;
    } ///else end here

}

============txt file:================

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 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 tothe 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 stype>

Explanation / Answer

The main problem in your program is the way you are handling the file handler. There is an syntax error .

please refer the below program with few correction with respect to using the file functions.

#include <iostream>

#include <string>

#include <fstream>

#include <sstream>


using namespace std;

struct Board
{

std::string type;

    int price;

    int statue;


};


void itemLeft(Board itemList[],int index)
{

    int cnt2 = 0;

    for(int i=0;i<index;i++){

        if(itemList[i].statue == 1 )
{

        cout<<itemList[i].type<< ", for sale, "<<itemList[i].price<<endl;

        cnt2++;

        }
else if (itemList[i].statue == 0 )
{

            cout<<itemList[i].type<< ", wanted, " <<itemList[i].price<<endl;

            cnt2++;

        }
else
{
continue;
}

    }

    cout<<cnt2<<" unsold items"<<endl;

}

int main(int argc, char** argv)

{

    Board itemList[200];

    int loopCnt = 0;

    int fileCounter = 0;

    int searchCounter = 0;

    int shiftCounter = 0;

    int itemSold = 0;


    std::string fileName;

    cout << "Please enter the file name, include the.txt" << endl;

    cin >> fileName;


    std::ifstream ifs;

    //ifs.open("fileName.txt");


    ifs.open(fileName.c_str());

    //ifs.open("cjj.txt");

    if(ifs.fail())
{

        cout << "failed to open" << endl;

    }

    else
{

        std::string item;

        int cnt = 0;

        int value;

        std::string a,b,c;

        while(getline(ifs,item,' '))
{
///while loop start here

           loopCnt++;

//To recognition the type

            if(item.find("chicken")!= string::npos)
{
                itemList[cnt].type = "chicken";

            }
else if(item.find("microwave")!= string::npos)
{

                itemList[cnt].type = "microwave";

            }
else if(item.find("bike")!= string::npos)
{

                itemList[cnt].type = "bike";

            }
else if(item.find("truck")!= string::npos)
{
                itemList[cnt].type = "truck";

            }
else if(item.find("dresser")!= string::npos)
{
                itemList[cnt].type = "dresser";

            }

//To recognition the statue 0 = wanted   1 = sale

            if(item.find("wanted")!= string::npos)
{
                itemList[cnt].statue = 0;

                stringstream ss(item);

                ss>>a>>c>>value;

                itemList[cnt].price = value;

            }else if(item.find("sale")!= string::npos)
{
                itemList[cnt].statue = 1;

                stringstream ss(item);
  
            ss>>a>>b>>c>> value;

                itemList[cnt].price = value;

           }
          
//For efficiency purpose,I will find what item will be sold inside this while loop and count the number


            for(int h =0;h<cnt;h++)
{
///find match start here

              loopCnt++;
    
          if(itemList[cnt].type == itemList[h].type)
{
//Same type is the first requirement!
               
   if(itemList[cnt].statue != itemList[h].statue)
{
//then the item should be different one for sale, one wanted

                     if(itemList[cnt].statue == 1)
{
         /// If this item is for sale
  
                        if(itemList[cnt].price<=itemList[h].price)
{
// then it need to meet the price requirement
             
                 cout<<itemList[cnt].type<<" "<<itemList[cnt].price<<endl;
//sold!
                      
        for(int y = h ; y < cnt ; y++)
{
//Shifts the elements
         
                         loopCnt++;


                                
itemList[y] = itemList[(y+1)];
                           
   }
         
                     cnt-=1;//itemList[cnt] has been shifted over 1

       
                       itemList[cnt] = {}; //delete it and because cnt-=1 last line so just like remove last item

                               cnt-=1;//start at the empty cell in the next iteration

                              itemSold++;
                       
       break;
///Finish sold process here=====================

   
                       }
else{continue;}
                
      }
else if(itemList[cnt].statue == 0)
{
///wanted
  
                        if(itemList[cnt].price >= itemList[h].price)
{

                                cout<<itemList[cnt].type<<" "<<itemList[h].price<<endl;

                                for(int y = h ; y < cnt ; y++)
{
//Shifts the elements
                              

               loopCnt++;
   
                               itemList[y] = itemList[(y+1)];


                                }

                                cnt-=1;//itemList[cnt] has been shifted over 1

                               itemList[cnt] = {};
//delete it and because cnt-=1 last line so just like remove last item

                                cnt-=1;//start at the empty cell in the next iteration

                                itemSold++;

                                break;
///Finish sold process here=====================

                            }
else
{
continue;
}

                        }
else
{
cout<<"Something go wrong here line 98"<<endl;
}

                    }
else
{
continue;
}

                }
else
{
continue;
}
//make sure the code can still run if it did not match the requirement before


            }
///find match end here

            cnt++;

        }
///while end here

    ifs.close();


    cout << "Items Sold: " << itemSold << endl;

    cout << "#" << endl;

    itemLeft(itemList, cnt);

    cout<<"#"<<endl;

    cout<<"Loop iterations: "<<shiftCounter<<endl;

    return 0;

    }
///else end here

}

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