I'm teaching myself how to work with Files with C++ and I ran into a small problem. I coded everything right, at least I think. When I run the program, it's not reading and outputting all of the contents in the file.
I tried other methods(getline, etc) but it's still not working right.
Code:
#include <iostream> //Library included for input and output functions
#include <fstream> //Library included for file stream functions
#include <string> //Library included for strings
using namespace std;
int main()
{
/*Variables. 'int counter'= the incrementor for the for loop.
'const int' = the constant variable used to declare the amount of contents in file
and tells the for loop what value to count up to and keep track of current position in file.
*/
const int MAX_SWORDS= 5;
int counter;
string Swords;
/*Declare ifstream function and inputFile object from function.
use '.open' to make retrieve the file from the HDD and open the file.
*/
ifstream inputFile;
inputFile.open("Swords.txt");
/*For loop that counts through the file called and until it reaches the value declared in 'MAX_SWORDS'
the counter variable will increment
*/
for(counter= 1; counter <= MAX_SWORDS; counter++)
{
/* For loop places all the contents of the file one by one and deplays them*/
inputFile >> Swords;
getline(inputFile, Swords);
cout << Swords<< endl;
}
//Close the file
inputFile.close();
//End program and wait for user input to close window
cin.ignore().get();
exit(0);
}
This the content of the file
Code:
Goddess Sword
Master Sword
Picori Blade
Elemental White Sword
Four Sword