Okay, so the prompt has us writing a program to read in a text file full of "wanted" and "for sale" text lines. We are to treat this as a somewhat realtime craigslist and read in each line, separating them into "wanted" and "for sale" categories and then match them to complete a transaction. So a buy array would read in any "wanted" items, then compare it to an array that would read in "for sale" items and as long as the products were the same and the price was <= the for sale price, a transaction would occur. I don't have much experience using vectors but everything I've read seems to hint that this would be useful, so it is not out of the question, I just don't fully understand the syntax.
eg. one line might read "chickens, wanted, 60" and another would read "chickens, for sale, 50"
The program would then write a new text file detailing this transaction and remove it from the searchable information until all possible transactions were complete.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
#include<iostream>
#include<iomanip>
#include<fstream>
#include<string>
#include<sstream>
#include<vector>
using namespace std;
struct transaction
{
string item;
int cost;
};
int main()
{
transaction buy[100];
transaction sell[100];
int price;
int w, s;
ifstream myfile.open ("messageBoard.txt");
ofstream results.open ("results.txt");
string line;
}
myfile >> getline(myfile, buy[w], '\n');
if
if(!myfile)
{
cout<<"Error opening input file"<<endl;
return -1;
}
while(myfile, line)
{
if(line.find("wanted") != string::npos);
{
getline(myfile,buy[w].item);
getline(myfile,price);
stringstream(price) >> buy[w].cost;
}
}
while(myfile, line)
{
if(line.find("for sale") != string::npos);
{
getline(myfile,buy[s].item);
getline(myfile,price);
stringstream(price) >> buy[s].cost;
}
}
for(w=0; w<100; w++)
{
for(s=0; s<100; s++)
{
if(buy[w].item == sell[s].item && buy[w].cost <= sell[s].cost)
if (results.is_open())
{
results << buy[w] << " matches " << sell[s] << "\n";
}
else
cout << "Unable to open output file." << endl;
buy[w] = 0;
sell[s] = 0;
}
}
results.close();
return 0;
}
|
I've been trying so many different options, hence all the #include libraries, and then this code compiles fine for me but just sits there during execution. I know the error is with my searching the file for the "wanted" string but I'm not sure how to fix it...
Any help would be extremely appreciated