0

When I read in a .txt file with 5 words in it from my program and put into an array with 20 spaces the last word in my file fills up the last 16 places in my array. Any ideas why? The file I am inputting will have a maximum of 20 words.

newArray string[20];
if (inputFile) {
    while (i<20) {
        inputFile >> word;
        if (word.length()<2) {   //gets rid of single character words
            i++;
        }   
        else{
            newArray[i] = word;
            cout<<newArray[i]<< " ";
        }

    }
    inputFile.close();
}
Y3DII
  • 53
  • 1
  • 2
  • 9
  • Cleanup your code. How are you starting with an else if? – George Houpis Jan 17 '16 at 04:00
  • Sorry, the problem is within the while loop. I tried to not copy the other stuff. – Y3DII Jan 17 '16 at 04:04
  • Check how you are initializing your variables? Starting with "else if (word.length() < 2) {" is a syntax error. Try indenting the code too – George Houpis Jan 17 '16 at 04:07
  • Thanks for your help. Is there a way I could somehow get it to recognize that there are no other new words in the text file and to break the while loop? – Y3DII Jan 17 '16 at 04:22

2 Answers2

1

Your question is unclear, but I am sure that in your loop you are probably still adding the last word because the way you are using the while loop. You are not breaking out of the loop after you are done adding the words. you should break out of the loop if you are at the end of the file, and that should solve your issue of last word appearing multiple times.

The better approach would be to read the whole file into 1 string, and tokenize and add each word at a time in the array.

If that doesn't help then please provide the full code. Also I do not understand why you have i++ } twice. Is this a typo?

Hope this helps.

edit: try this code:

int i = 0;
string line;
ifstream myfile("names.txt");
if (myfile.is_open())
{
    while ( getline (myfile,line) )
    {
        arr[i] = line;
        i++;
    }
    myfile.close();
}

You will not add any lines after

deltashade
  • 386
  • 1
  • 13
  • I'm trying to do it without tokenizing. I want my while loop to stop as soon as I get to a point in the text file where I don't have any more text. – Y3DII Jan 17 '16 at 04:12
  • In your while loop you are doing else if. You cannot do that since there is no if statement above it. Maybe you have a typo. Also if you do not want to tokenize, and since you already are able to put words in an array, you can check if you have reached the end of the stream, and in that case you want to break out of the loop. – deltashade Jan 17 '16 at 04:21
  • I understand that I need to break out of the while loop. I just am not sure how to implement that. How would I know once the stream ends? – Y3DII Jan 17 '16 at 04:26
  • check the latest lines of code that i added. Instead of your while loop, use my. – deltashade Jan 17 '16 at 04:29
  • You can also just use the `eof()` method to check if you reached the end of the file stream. you can use by using the following code: `if(filehandle.eof()) break;` – deltashade Jan 17 '16 at 04:31
  • Thank you so much! if(filehandle.eof()) break; really helped me out! Your first method worked however it didn't get rid of the single character words I wanted to get rid of. if(filehandle.eof()) break; definitely solves my problem! Is there a way I can mark you as the best answer? – Y3DII Jan 17 '16 at 04:44
  • Hi, Sure you can just select my answer as the correct one I believe. You can also up vote me, and once you get 15 reputation it will show my as +1. You can just add an if statement in between the loop that can do the check for length as you had done in your own solution. – deltashade Jan 17 '16 at 06:16
1

Correct me if I'm wrong, but why do you need an array of 20 strings to read in 5 words? The code below is the standard way of reading from a file into an array.

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

using namespace std;

int main()
{
  string myArray[20];
  ifstream file("file.txt");
  string word;
  if(file.is_open())
    {
      int i = 0;
      while(file>>word)
        {
          if(word.length()<2)
            continue;
          else
            {
              myArray[i] = word;
              i++;
            }
        }
    }
}

Addendum: Edits will read all the words and stops when there's no more text. Your original problem was that the file stream does not read anything after all 5 words are read, therefore word remains the same, causing it to fill up the array.

user3813674
  • 2,553
  • 2
  • 15
  • 26
  • Well I want it to have a max of 20 words. For testing purposes I'm sending a file with 5 words in it. Which is why I chose to use a while loop, I want it to break out of my while loop once I don't have any more words to read. – Y3DII Jan 17 '16 at 04:26
  • @Y3DII: I think this answer is your solution. – Deidrei Jan 17 '16 at 04:47