Originally Posted by
carpeltunnel
I'm so overwhelmed, paypal for help!
Hi, I'm sorry it's taking me so long to get back with you. It sounds like you're giving up though. I don't want you to do that.
Originally Posted by
carpeltunnel
If nobody wants that offer lol,
Code:
while
(inFile)
{
std::string lineRead;
char command;
inFile.getline(inFile) >> lineRead;
lineRead.getChar() >> command;
switch(command)
}
Is there away to take it string by string like an array.
So I can do something like
Code:
//get.char[0]>>command;
//get.pair{I,i+1)
or something to that extent
Please try to be patient. Speaking as a person from a higher perspective, it takes time to work on your problem. It can help you feel less stressed if you trust that people are trying to help you.
I have some bad news. That is a pretty awful attempt at reading a file. Luckily, I think I put something together that will help you understand the steps you need to take to process the file. That's later on in the post. I want to try rewriting my answers to your questions first.
-Would I be better off doing everything in one source file and having functions instead of having a header and file associated with?
If you do the assignment in C++ (the assignment seems ambiguous on the language), I think it would be best to turn in a header file for a priority queue class, a source file with a priority queue implementation, and another source file that tests the implementation in a main() function, where you read the file and respond accordingly.
Will a tradition heap process both letters, numbers (including floats) and how does it work? i.e. What has priority etc? Or is that something I am supposed to design.
A normal heap would work fine. The queue element has to be designed though, because in C++, most data structures, including heaps, are homogeneous, meaning they work on one kind of thing. So to complete the assignment, you need to design a queue element that fits your data. Again, you'll see my ideas on this in a moment.
Also, is there any advice/references to figure out how to take the first character of each row (the command letters) and then use the rest for the heap?
I can give you something that might help. There are a few things I am worried about though. It looks like you aren't very familiar with C++ file stream workings or syntax. You need to be comfortable with those things if you want to do it in C++. Most of the tutorials on that subject are inferior to books that teach C++, so you might want to reread a college textbook or something. Again, the assignment seems ambiguous about the language requirement, so, you might just want to do it in an easier language for you.
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
struct qelement {
string data;
float priority;
// This operator makes it concise for me to display information.
friend ostream& operator << (ostream& outstr, const qelement& elem);
// This operator makes it concise for me to read information.
friend istream& operator >> (istream& instr, qelement& elem);
};
ostream& operator << (ostream& outstr, const qelement& elem) {
outstr << "priority: " << elem.priority << ", " << "data: " << elem.data << '\n';
return outstr;
}
istream& operator >> (istream& instr, qelement& elem) {
qelement result;
if (instr >> result.data >> result.priority) {
elem = result;
}
return instr;
}
int main() {
// 1. Open the file from the thread.
ifstream file("carpeltunnel.txt");
if (file.is_open() == false) {
cout << "couldn't open carpeltunnel.txt\n";
return 0;
}
char operation;
string ln;
// 2. Read each line.
while (getline(file, ln)) {
// 3. Parse the operation/read the operation from the line.
stringstream buffer(ln);
buffer >> operation;
// 4. Display the operation.
cout << "Operation is " << operation << ":\n";
// 5. Parse any queue elements that are part of the line.
qelement elem;
while (buffer >> elem) {
// 6. Show what we received as input.
cout << '\t' << elem << '\n';
}
}
return 0;
}
The output of the above code is:
Code:
C:\Users\Josh2>readfile
Operation is B:
priority: 9.1, data: foo
priority: 2.1, data: bar
priority: 1, data: a
priority: -4, data: b
priority: 7, data: c
priority: 2, data: d
priority: -4, data: e
priority: 5, data: f
priority: 7, data: g
priority: 9, data: h
priority: 4, data: i
Operation is D:
Operation is U:
priority: 2, data: f
priority: 11, data: d
priority: 3, data: c
Operation is P:
Operation is R:
Operation is R:
Operation is R:
Operation is D:
C:\Users\Josh2>
This is to show that we successfully parsed the entire file. You can see the operations and the relevant data.
The way that I attacked the problem is, to go more into detail about it than what's in the comments: I think it is really important to read the file one whole line at a time, storing the line in a big string. After you do that, you can focus on turning the big string into whatever you really need.
Usually that means breaking up the string into tiny parts. Using stringstream, I was able read in an operation and modify the elem object as many times as I needed to on one line.
If I wrote this for real, some things would change. There would be more logic to decide what to do with the data I was receiving. Build would make me insert the elem I just read into the queue, and keep going until there is no more stuff to put in queue. I would have to update an existing item in the heap when I read operation U: that item should look like elem. The other operations, even though they don't involve data from the file, would mean that I would have to change or display the queue.
Anyway, I really hope this helped. I tried to present a really clean example so I don't really know how else to inspire you.