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.
#include <iostream>
#include <string>
#include <conio.h>
#include <fstream>
usingnamespace 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;
}
#include <iostream>
#include <string>
#include <fstream>
usingnamespace std;
constint 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?
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?
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....