Hello rin103,
There are many things wrong. Some small and some big.
Overall the code is in bad need of proper indenting. And a few blank lines would help.
To start with:
1 2 3 4 5 6 7 8
|
#include <iostream>
#include <cstdlib>
#include <queue>
#include <fstream>
#include <ctime> // <--- Changed.
#include <string> // <--- Added.
using namespace std;
|
There are several old C header files that have been chanced to work with C++. For a list take the "
Reference" the link near the top left of the page. I just noticed that this section is being updated.
You are using "std::getline" and ".c_str()". Both of these require the "string" header file. If this code compiled for you I am thinking that you may need to adjust your compiler settings to catch more errors. It would also help if you mention what IDE/compiler you are using.
using namespace std;
This is a whole topic in its-self, but you should be learning to not use this.
Your "#defines" will work, but defining constant variables is a better choice. As an example:
1 2 3 4 5 6 7
|
// Input parameters
//#define SIMULATION_TIME 0
constexpr unsigned int SIMULATION_TIME; // <--- As a global variable there is a good chance this will be initialized to zero. Otherwise use "SIMULATION_TIME{}".
//#define ARRIVAL_RATE 1
constexpr unsigned int ARRIVAL_RATE{ 1u }; // <--- Or just make it an "int".
const std::string FILENAME{ "sim.txt" };
|
Whether you use an "int" or "unsigned int" is up to you. I have not gone through enough of the code yet to see how all the constants are used.
1 2 3
|
// Holds the current simulation parameters
double parameters[PARAMS_MAX];
double counters[COUNTERS_MAX];
|
As global variables these should be avoided as any line of code that follows can change them. That makes it more difficult to find where it went wrong. It is better to define them in "main" and pass them to any function that needs them. Better control over the variables.
The "randomInt" function may work, but it just does not look right. I will have to check on that.
In the "readNextSim" function the "getline" works when you include the header file "string".
In the line of code:
parameters[i] = atof(tmp.c_str());
you are over thinking it and doing to much work. This can be written as:
parameters[i] = stod(tmp);
.
The "string" class has several member functions to convert a string to a number.
The more I look at the function I wonder why you do not use something like
inFile >> parameters[i]
. Since you did not provide the input for everyone to see it is only a guess at this point. PLEASE provide the input file when a program requires one. If it is large a fair sample will work.
That gets down to "main". The input file would help with testing.
Andy