Reading words from a file; display to console

Hey all,

Need a little help for this final part of my console program. I'm in my 7th week in my C++ course, so I am a beginner. The problem is stated in my /* comment section*/. What is remaining is VERY short but the solution is evading me right now.

Any direction will be greatly appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <string>
#include <conio.h>
#include <fstream>

using namespace std;

int main()
{
	
		
	
	string words_1[4];                  // Words array to house the 5 words in memory	
	ifstream inFile_2;                  // File to read the words from	
	inFile_2.open("words_1.txt"); 
	

cout <<"\n\n=-=-=-=-=-=-=-=-=-=-=-=-=-FSTREAM (Words to Console)=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n" <<endl;

cout <<"This final portion will:\n\n" <<endl; 

cout <<"3. Read 5 words in from a THIRD file" <<endl;
cout <<"4. Display results of words to the CONSOLE\n\n" <<endl;

inFile_2 >> words_1[];                           /*I'm not sure WHAT to place in the array subscripting operator.
						   I've tried index & counter (declared them but deleted declaration when
						   it did not work) and [4] but to no avail.*/ 
	
	cout <<"The stored words in the THIRD file are below: \n"<<endl; 

	cout <<words_1[];                        /*I'm not sure WHAT to place in the array subscripting operator.
						   I've tried index & counter (declared them but deleted declaration when
						   it did not work) and [4] but to no avail.*/ r

		
	inFile_2.close();         // Closes words_1.txt file
	

cout <<"\n\n\nPress ENTER to exit the program....";		

cin.get();
cin.get();
return EXIT_SUCCESS;
}
Here ya go bud

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

const int SIZE = 10;

int main()
{

string words[SIZE];
fstream inFile_2;
inFile_2.open("words_1.txt" , ios::in);

for(int i=0;!inFile_2.eof(); i++) //This will read the words off the file 1 at a time and print them out as well
{ 
	inFile_2>>words[i];

	cout<<words[i]<<" "; //If you want your words to be on seperate lines in the console, add //the <<endl
	
}

inFile_2.close();

cout <<"\n\n\nPress ENTER to exit the program....";		

cin.get();
cin.get();
return EXIT_SUCCESS;

}
Thanks, but what is the significance of " i "? What does it mean? I've seen many examples that always use " i " as well but I can't find an explanation on what it means.

Also, when I use your help above, my console goes into a infinite loop displaying what seems to be almost like webdings. Any idea?

Thanks again!
Sorry bud had little time to post and rushed things due to my girlfriend waking up.

Think of and array as a count from 0 to whatever number.

An array stores number by starting at 0 rather than 1.

So say you are storing 5 words, it goes like...

0 - word
1 - word
2 - word
3 - word
4 - word

So when you go to print those numbers out after you have stored them in a array you would start at 0, this is where the integer i comes from.

When you start a for() loop you are executing the same command until you integer is equal or the file reads till the end like i did.

So...where does i come in is what you asking.

when i use words_1[0] i am calling for the 1st word in the array to come forward and like in the for loop that I have constructed, it will read the whole file and increment i so it reads all the words in the file.

So say our file looks like

saints win superbowl

The first time the for loop executes the array will be at words_1[0]

Our first word will be saints which is stored at the 0 spot in our array.
And each time it increments it will be the next word which will be "win"


Basically the integer "i" is taking us through each word in the array until we reach the end of the file
(.eof)

As far as the infinite loop i have wrote this program several times and haven't come across a infinite loop yet.

Which compiler are you using and are you manipulating the for() loops at all?

Hope this helps!!
WOW! Your explanation cleared up a lot of questions I had!! Awesome! As far as my infinite loop problem, I got it figured out but I used a slightly different approach.

I ended up using a similar version of yours, but not quite:

1
2
3
4
5
6
7
8

cout << "The stored words in the THIRD file are:\n\n" <<endl;

	for(int i = 0; inFile_2 >> words_1[i]; i++)   //Loop with declared index for preparation to output to console
	{
	  cout <<words_1[i] <<endl;                   //Output to console 
	}


Again, I really appreciate your help in explaining my questions. See ya bud....
You can actually use the same kind of loop to reverse your words that output. I saw you had a question about printing it in reverse.

Well manipulate your loop a bit and instead of incrementing our i value you would start at the end of the array and decrement our i value.

So if you have 5 words, you would set your value of i at 4 and decrement down to 0.

Glad I could help!!
Topic archived. No new replies allowed.