I need to read the 5 words from a input file mine is called words.txt I am unable to get to work correctly everytime I put inFile >> words[5]; instead of cin >> words[5]; I get a debug error and prog shuts down and nothing is showing up for errors or warnings

here is my code:

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{

	  ifstream inFile;
	  ofstream outFile;

      int numbers[5];
      int counter;
      string words[5];

	  inFile.open("numbers.txt");
	  outFile.open("output.txt");

	  cout << "Processing information from numbers file...." << endl;

      for(counter=0; counter < 5; counter++)
      {
            inFile >> numbers[counter];
      }
	  cout << "\nInformation stored successfully! "<<endl;

      for(counter = 4; counter >= 0; counter--)
      {
            outFile << numbers[counter] << endl;
      } 
      
	  cout << "\nProcessing five words already input from file...." << endl;

	  inFile.close();

	  inFile.open("words.txt");

      for(counter = 0; counter < 5; counter++)
      {
            inFile >> words[counter];
      }
      
	  words[counter] = "end_of_array";

      counter = 0;

      do
      {
            cout << words[counter].substr(0,1) << words[counter].substr(2,1) << endl;
            counter++;

      }
	  
	  while(words[counter]!= "end_of_array");

      inFile.close();
	  outFile.close();
      
	  return 0;
}

lines 39 and 42 are wrong. The array only has 5 elements and you are attempting to access a 6th element -- won't work. And line 39 should use the loop counter variable like you did on line 30 and elsewhere.

Everything works fine except I need to it read from a input file it reads the words from my inputing them and displays everything correctly just need to modify that to read from a file

I think you misread, Ancient.

>> string words[6];

so there is a words[5].

You're right -- I did misread it. :icon_redface: But the comment about line 39 stands.

Comments
We all make mistakes :)

>>just need to modify that to read from a file
Where ?? what line number from your original post ? Why not do it the same way you did on line 24? Same code, but use words[counter] instead of numbers[counter]

Begin by writing a separate program which, for the sake of brevity, does nothing else but read in a file.

Once you have understood that apply it to your existing problem.

[search]c++ read file[/search]


If you have specific questions about what you have found ask them here.

This article has been dead for over six months. Start a new discussion instead.