Hello Marcos8701,
As
ne555 said
show enough code to reproduce your problem.
What you have posted is not even complete to compile.
In "bank.h", for the struct, you should initialize the variables so that you are not working with garbage values.
This should work for you:
1 2 3 4 5 6 7 8
|
struct Customer
{
int waitTime{};
int arrivalTime{};
int transTime{};
char name{};
int departTime{};
};
|
The empty {}s will set the variables to zero, available form C++11 on. If this is a problem you can use " = 0" and let me know what IDE you are using I might be able to help you change the IDE.
The class is missing some forward declarations and also the closing "}:" at the end of the class. You have forward declared the default ctor, but you also need to forward declare the default dtor. Once you say there is a default ctor you also have to say that there is a default dtor. Once you declare a default ctor the compiler does provide the default ctor or dtor. This also happens when you define an overloaded ctor. The compiler does not provide the default ctor or dtor. This is left up to you.
You forward declare the functions "isEmpty" and "size", but never provide the functions. This is not a problem and does nor cause any errors at compile time, but it is not complete.
You are missing the forward declaration for "startSimulation" Nor a problem here, but it is in the "Bank.cpp" file.
In the "Bank.cpp" file:
You have:
1 2 3 4 5
|
#include "Bank.h"
#include <queue>
#include <fstream>
#include <iostream>
using namespace std;
|
Some have said the order should not make any difference, but sometimes I find that it does. I found that this makes a big difference:
1 2 3 4 5 6 7
|
#include <queue>
#include <fstream>
#include <iostream>
#include "Bank.h"
using namespace std; // <--- Best not to use.
|
By putting "Bank.h" last the previous header files are compiled into the program first thus covering anything that would be used in the header file.
Since you have forward declared the default ctor and detor you need to provide these functions in the ".cpp" file.
The ctor should be something like this:
1 2 3 4 5 6
|
Bank::Bank()
{
itemCount = 0; // # of elements in Queue
tellerAvailable = 0;
clock = 0; //simulates time
}
|
Here "itemCount is a good example. Left uninitialized it will contain a garbage value this is usually "-858993460" on my computer for an int. Just adding "1" to this would leave you with "-858993450". Not what you want.
A default dtor would be
Bank::~Bank() {}
unless you decide to put something between the {}s.
Leaving out the function definitions for "isEmpty" and "size" that is OK since they are never used anywhere yet.
In the "readInCustomers" function, and as
ne555has pointed out,
ifstream infile ("inputFile.txt");
, The first part definde the file stream for "infile". The second part "("inputFile.txt")" provides the file name to use. All together this will create "infile" and open the file at the same time. This is the shortest way to do this and most often used.
The next line
infile.open("inputFile.txt");
is trying to open a file stream that is already open, so it fails. This causes this line to fail, so when you get to the if statement the condition will evaluate to false and you end up at the else part even if the file stream did open with the first line.
The if/else statements work and your code is short enough not to complain about, but a better if statement would be:
1 2 3 4 5
|
if (!infile)
{
std::cout << "\n Error. File did not open\n" << std::endl;
/*return 1;*/ exit(1); // if not in "main"
}
|
Zero means a normal return or exit and any number greater than zero means that there is a problem. The reason that you return or exit is because there is no point in continuing with a file stream that is not open and usable.
In the
while(!infile.eof())
. This does not work the way you think it does. By the time it finds that the "eof' bit has been set you have processed the last read twice and left 'index" one number higher than it should be and that is not what you want.
A more common way of writing the while condition is:
while (infile >> c.name >> c.arrivalTime >> c.transTime)
. This way when you try to read past "eof" the "eof" bit is set and the while condition becomes false thus ending the while loop.
Since the function
void Bank::startSimulation()
was not forward declared in the header file none of this code would work.
Once I fixed the problems it worked.
I did have to create a "main" function to get things started. This may not be what you have. I do not know because you failed to include this with you other code.
One thing that did occur to me late is the use of a header guard. It would be something like this:
1 2 3 4 5 6
|
#ifndef BANK_H
#define BANK_H
// your code here.
#endif // !BANK_H
|
The name, "BAND_H", can be anything you like, but usually looks something like this and in all caps.
Hope that helps,
Andy