Help with a matching program.

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
Last edited on
line 27: That's not how you use getline(). You used it correctly later, so why are you using it wrong here?
lines 27 and 38: w has not been set to anything yet
line 28: there's a lonely 'if'

To create a vector of transactions you would use vector<transaction> buy;
To add an item you would use buy.push_back ( item ); where item is of type 'transaction'.
Do I even need line 27? I'm beginning to over think this, I think. Heh.

Would it be easier to get rid of lines 27-51 and replace with the vector code you suggested? I'm not sure how to then compare the elements in the vector to see if they match for a "transaction."

I really appreciate your help, thank you!
If the array is big enough and it works, it's probably not worth switching over to a vector. I don't think it would save you much.
this code compiles fine for me
would you please tell me which compiler you are using? or which code you are compiling. This code should not compile.

1
2
    ifstream myfile.open ("messageBoard.txt");
    ofstream results.open ("results.txt");

the open method is only needed if you want to create without the constructor for some reason simply remove the .open so ifstream myfile("messageBoard.txt"); also you should fix the compiler errors :P The often times tell you the exact problem and where it is.
http://cpp.sh/9ok
http://coliru.stacked-crooked.com/a/7fe944338b60a215
http://ideone.com/gE52f3

By the way another thing your main function ends on line 26..
Last edited on
Topic archived. No new replies allowed.