I've created a text file with the numbers from 1-450. After writing code to retrieve and print out the contents in the text file, the compiler only printed out the numbers 124-450. Is there a reason why this is happening?
int main()
{
string line;
vector <string> text;
ifstream file ("nums.txt"); /// Calls the text file
if (file.is_open()) /// Check if file is open
{
while(file.good()) ///Check whether the file is valid or corrupt
{
getline(file, line); /// Get lines from text file
text.push_back(line);
}
for (int i = 0; i <= text; i++)
{
cout << text[i] << endl;
}
file.close();
} /// if & while loops open and shows the data in the text file.
return 0;
=====================================================
And this is the text file:
1
2
3
4
5
.
.
.
450
lets say that the current file has/is
a) a space between two numbers on one line (as an error)
b) just a test for future files that he will have to do some operation on each line.
if he does cin he has to remember to change it if he needs to grab each line later. there are others but ive almost finished my first pandarian ;)
^ I tried your code and it works. But I need each number to be on a separate line. I modified std::cout << text[i]; and changed it to std::cout << text[i] << endl; and encountered my same issue.