Another simple/stupid C++ question here.
I was just going to post a snippet, but I figured someone would point out an uninit'd variable or something else simple that was in my original code but not in the snipped, so here is the whole thing

Anyway, the major problem is how do I reference an element of a structure from an array of that structure type, which is itself a member of a struct. Ex:
Assume struct1 and struct2, with array1 being a member of struct1 and of type struct2. struct2 has a member, people.
struct1.array1[struct1.intval].people
But this produces pretty much gibberish. You can check the code out below. If you spot any other errors in the setup, I would love to hear about it. I know it must be something fairly easy that I'm just overlooking.
This line here is the problem. The member "people" is just gibberish, and I don't know why.
[This message was edited by russ-iha on March 22, 2002 at 20:06.]
I was just going to post a snippet, but I figured someone would point out an uninit'd variable or something else simple that was in my original code but not in the snipped, so here is the whole thing
Anyway, the major problem is how do I reference an element of a structure from an array of that structure type, which is itself a member of a struct. Ex:
Assume struct1 and struct2, with array1 being a member of struct1 and of type struct2. struct2 has a member, people.
struct1.array1[struct1.intval].people
But this produces pretty much gibberish. You can check the code out below. If you spot any other errors in the setup, I would love to hear about it. I know it must be something fairly easy that I'm just overlooking.
Code:
#include <iostream>
#include <fstream>
#define MAXQUEUE 100
using namespace std;
const int BUSARRIVAL = 0;
const int PEOPLEARRIVAL = 1;
struct group_type
{
int event;
int time;
int people;
};
struct queue_type
{
group_type items[MAXQUEUE];
int front;
int rear;
};
void initialize_queue(queue_type&);
void dequeue(queue_type&, group_type&, bool&);
void enqueue(queue_type&, group_type&, bool&);
void main()
{
group_type tmp;
group_type ppl;
queue_type theq;
bool overflow;
bool underflow;
int InLine = 0;
int TotalPeople = 0;
int TimeWaited = 0;
ofstream fout("c:\\output.dat");
ifstream fin("c:\\input.dat");
initialize_queue(theq);
while(fin) {
fin >> tmp.event;
fin >> tmp.people;
fin >> tmp.time;
if(tmp.event==PEOPLEARRIVAL)
{
InLine += tmp.people;
enqueue(theq, tmp, overflow);
}
else
{
// tmp.time = when bus arrived
// tmp.people = how many the bus can take
// num people in front of line: theq.items[theq.front].people
if(theq.items[theq.front].people > tmp.people)
{
theq.items[theq.front].people -= tmp.people;
TotalPeople += tmp.people;
TimeWaited += (tmp.people * ( tmp.time - theq.items[theq.front].time ));
}
else if(theq.items[theq.front].people == tmp.people)
{
dequeue(theq, ppl, underflow);
if(underflow)
{
break;
}
TotalPeople += ppl.people;
TimeWaited += (ppl.people * ( tmp.time - ppl.time ));
}
else
{
int Loaded = 0;
while(Loaded < tmp.people)
{
This line here is the problem. The member "people" is just gibberish, and I don't know why.
Code:
if(Loaded + theq.items[theq.front].people <= tmp.people)
{
dequeue(theq, ppl, underflow);
if(underflow)
{
//queue empty.
break;
}
else
{
TotalPeople += ppl.people;
TimeWaited += (ppl.people * ( tmp.time - ppl.time ));
Loaded += ppl.people;
}
}
else
{
int TookOut = 0;
TookOut = tmp.people - Loaded;
theq.items[theq.front].people -= TookOut;
TotalPeople += TookOut;
TimeWaited += (TookOut * ( tmp.time - theq.items[theq.front].time ));
}
} //end while
} //end if
}//end tmp.event==peoplearrival
}//end while(fid.eof == 0)
cout << endl << endl << TimeWaited << endl << TotalPeople << endl;
// TOTAL PEOPLE WHO RODE A BUS: totalpeople
// THE AVERAGE WAITING TIME IS: (totalpeople / timewaited)
}
void initialize_queue(queue_type& queue)
{
queue.front = 1;
queue.rear = 0;
}
void dequeue(queue_type& queue, group_type& remove, bool& undflow)
{
if (queue.front == queue.rear)
{
queue.front = 0;
queue.rear = 0;
undflow = true;
return;
}
undflow = false;
remove = queue.items[queue.front];
queue.front = (queue.front + 1) % MAXQUEUE;
return;
}
void enqueue(queue_type& queue, group_type& new_value, bool& ovflow)
{
queue.rear = (queue.rear + 1) % MAXQUEUE;
if (queue.front == queue.rear)
{
queue.rear = (queue.rear + MAXQUEUE - 1) % MAXQUEUE;
ovflow = true;
return;
}
ovflow = false;
queue.items[queue.rear] = new_value;
return;
}
[This message was edited by russ-iha on March 22, 2002 at 20:06.]