and this is my code How can I write code for words from the file are read into an array of strings, one word per string.?

Dec 8, 2015 at 8:58am
Hello I try to write the code but it only displays the first line

for example my txt file has 100 line and one word per line
word1
word2
word3

and this is my code
How can I write code for words from the file are read into an array of strings, one word per string.?

1
2
3
4
5
6
7
8
9
10
11
  string filewords=100; 

for (int i=0;i<=99;i++) 
{ 
inData>>filewords[i]; 
cout<<filewords[i]; 

please help... !! 

i++; 
}
Dec 8, 2015 at 9:41am
First you need to declare your array properly - string filewords[100];

Then you need to open your file and check that it opened properly.

1
2
3
4
5
6
ifstream inData("Filename");
if (!inData)
{
  cout << "File error...";
  exit(1);
}


Finally you can read it:
1
2
3
4
5
int line = 0;
while(inData && line < 100)
{
  inData >> filewords[i];
}
Dec 9, 2015 at 1:40am
thanks ^^ :)
Topic archived. No new replies allowed.