mercredi 21 janvier 2015

Trouble searching through "craigslist" array to find a match and deleting rows that match and shifting everything up one

I have the majority of this program down but I have an error I can't seem to fix.

I am given a text file that looks like this:



chicken, wanted, 60
microwave, wanted, 100
bike, for sale, 6
bike, wanted, 50
chicken, for sale, 200
microwave, wanted, 10
cat, for sale, 10
microwave, wanted, 30
microwave, for sale, 1
dog, for sale, 50


Then I go through (in my program) and create an array of structs, I think that should be fairly easy to do if you read my code below. Where I'm stuck is the big block of if statements, when I find a match, for example the bike wanted 50 and bike for sale 6, I need to delete those from my array and start at the next element, chicken for sale 200, so that that is the second element in my array and I keep adding the next elements one by one until I find a match. The program I have is ok for doing this if I only have one match, but I run into problems when I reach the microwave for sale 1 line in my program. The program then spits out the remaining items in the wrong order, it will print out



chicken, wanted, 60
microwave, wanted, 10
microwave, wanted, 30
chicken, for sale, 200
cat, for sale, 10
dog, for sale, 50


for my category Items left on the message board. Doin this on paper it seems as if when it was going through the for loop for microwave for sale 1, it found the match at microwave wanted 100 and then stuck the remaining microwave types there since the typese matched and the sale and for sale were opposite. I want to keep the message board in order so it will say the remaining items are



chicken, wanted, 60
chicken, for sale, 200
microwave, wanted, 10
cat, for sale, 10
microwave, wanted, 30
dog, for sale, 50


I hope this is clear enough and anyone that can spot this problem is a life saver! If anything is unclear please please ask so I can get help!



#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>

using namespace std;

struct item
{
string type;
bool sale; //0 = sale, 1=wanted
int price;
};

void printLeftover(struct item array[],string wanted_or_not, int num);

int main(int argc, char*argv[])
{
//open file
ifstream fn;
fn.open(argv[1]);
//create array
struct item itemArray[100];
//open file
int counter = 0;
if (fn.is_open())
{
string line;
string type;
int price;
string if_sale;
string line2;
bool sale;
cout<<"Items sold"<<endl;
cout<<"#"<<endl;
//get line from file
while ( getline (fn,line))
{
//split into type, sale, and price by substring
int index = line.find(",");
type = line.substr(0,index);
line2 = line.substr(index+1, line.length());
int index2 = line2.find(",");
if_sale= line2.substr(0,index2);
price= atoi(line2.substr(index2+1, line2.length()).c_str());
//treat for sale and wanted as bool
if (if_sale == " wanted")
sale = true;
else
sale = false;
//create new item and put in array
item newItem={type,sale,price};
itemArray[counter] = newItem;
counter++;

//cout<<counter<<endl;


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

if ((itemArray[i].type == newItem.type) && (itemArray[i].sale != newItem.sale))
{
//cout<< "matching type and wanted for sale" << endl;
if(itemArray[i].sale == 0)//for sale
{
if(itemArray[i].price <= newItem.price)
{
cout<< itemArray[i].type << " " << itemArray[i].price<<endl;

for(int j=i;j<counter;j++)
{
itemArray[j]=itemArray[j+1];
}
counter = counter -2;
numsold++;

break;
}
}

if(itemArray[i].sale ==1) //wanted
{
if (itemArray[i].price >= newItem.price)
{
cout << newItem.type<< " " << newItem.price<<endl;
for(int j=i;j<counter;j++)
{
itemArray[j]=itemArray[j+1];

}
counter = counter -2;
numsold++;
break;
}
}

}
}

}

fn.close();
cout<<"Items remaining in the meessage board after reading all lines in from the file."<<endl;
cout<<"#"<<endl;
printLeftover(itemArray,"wanted", counter);
printLeftover(itemArray,"for sale", counter);

}
};

void printLeftover(struct item array[],string wanted_or_not, int num)
{
int i;

if (wanted_or_not == "for sale")
for(i=0; i< num; i++)
{
if(array[i].sale == 0)
cout<< array[i].type << ", " << "for sale" <<", " << array[i].price <<endl;
}
else
for(int i=0; i< num; i++)
{
if(array[i].sale == 1)
cout<< array[i].type <<", " << "wanted" <<", "<< array[i].price <<endl;
}
}


my final output should read



Items sold
#
bike 6
microwave 1
Items remaining in the meessage board after reading all lines in from the file.
#
chicken, wanted, 60
chicken, for sale, 200
microwave, wanted, 10
cat, for sale, 10
microwave, wanted, 30
dog, for sale, 50

Aucun commentaire:

Enregistrer un commentaire