Hello Zachy186,
Enoizat makes some good points:
Why do you declare all your variables at the beginning of functions? Do declare them where you really need them. |
I still do not understand why burring the variables in the program is a good idea just do what feels right to you and what you are comfortable with.
When I finished the prototypes several errors went away.
AS to the "pch.h" file we both know that VS generates this file, but it is best to leave this line out when posting code here. Most people can not use this file and it cuts out unnecessary comments.
When I started testing the program the first problem I had was checking that the file was opened. For me the return closed the console window before I could read the message.
This is my fix:
1 2 3 4 5 6 7 8
|
if (!inFile)
{
// Display an error message.
cout << "Error opening the file.\n";
std::this_thread::sleep_for(std::chrono::seconds(4)); // Requires header files "chrono" and "thread"
return 1; // <--- the 1 means there is a problem.
}
|
Then I had a problem reading the file. First I forgot to create the file so I did not see the message before the console window closed when the "return 0" was reached. BTW it should be "return 1" with the 1 meaning there was a problem and zero meaning there was no problem when the program ended.
Like "std::cin >>" "inFile >>" works the same way. First it will read up to the first white space or new line whichever comes first leaving anything that may be left in the input buffer. This worked up to where the next read was "first base". Only first was put into the variable and "base" was left in the input buffer. When "inFile >> score[count]" expected a number to be read all it got was a string causing the input file stream to fail and nothing else was read. For string variables it is best to use "std::getline(inFile, stringVariable);". This way if there is more than one word it will get every thing on the line.
You can mix "std::getline()" and "std::cin" or "inFile", but after the "std::cin" or "inFile" you need to clear the input buffer before the next "std::getline()". The following code fixed the read problem:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
for (int count = 0; count < SIZE; count++)
{
std::getline(inFile, names[count]);
inFile >> number[count];
inFile.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::getline(inFile, position[count]);
inFile >> score[count];
inFile.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << std::endl;
} //for loop applying .txt to arryfiles <--- Not the best description for reading a file.
|
Once it was reading the file and storing the information properly I tested case one and it worked as it should.
Now I have to test the rest.
Hope that helps,
Andy
P.S. forgot to mention when using a constant put it as your first variable definition before it is used. And if it is a global variable put soon after the includes.