Hello jw209,
Many things.
First you have not provided a complete program that can be compiled. Without the include files and "main" it is only a guess at what you are doing and if it is correct.
You refer to an input file, but you have neglected to post what the file contains. Giving everyone the same information to use helps and reduces confusion. Also it shows how the file is laid out. If it is large file a fair usable sample will work.
You are using "float" and this may work, but "double" is the preferred floating point type.
In the function "getChoice" you are overusing the "setfill" and not using ii in the best way that you could.
In several places you use:
while (cin.fail()
this will work, but
while (!cin
will do the same thing.
In the function "makeId" you have:
1 2 3 4 5 6
|
void makeId(int& id)
{
srand(time(0));
id = ((rand() % 99999) + 1);
}
|
First "srand" should be near the beginning of "main" as it only need done once not each time you call the function. A better way to write that is:
srand(static_cast<size_t>(time(nullptr)));
. Where "size_t" is an alais for "unsigned int".
Then there is the call to "rand". The largest number "rand" is likely to return is 32767, so 32767 % 99999 would leave you with 32767. You could have said
id = rand() + 1;
. Either would produce the same result. You may want to rethink your mod number. Usually this number is to limit the range of numbers to use.
In the function "printFromFile" you have:
1 2 3 4 5 6 7
|
string newChoice = " ";
string timeOf = " ";
log.open("transactionLog.txt", std::fstream::in);
log.clear();
log.seekg(0, std::ios::beg);
|
First when you define a string it is empty and does not need initialized unless you need something there.
Line 4 opens a file or does it? You have not way to know if the file stream is usable.
Lines 6 and 7 do not really do much because if the stream opened the bad bits are already set to (0) zero and there is no reason to reset what is already set. Since the stream is opened for input the file pointer is already at the beginning.
1 2 3 4 5 6 7 8
|
log.open("transactionLog.txt", std::fstream::in);
if (!log)
{
std::cout << "\n Error message\n";
return 1;
}
|
The return should get you back to "main" where you can deal with the file not opening and leave the program properly.
Earlier I referred to the function "getChoice". I will need a little to revise that to show you what you could do.
Andy