Inputing data from .txt file into array

I posted on stack overflow, but everyone just downvotes my questions as I am not a seasoned programmer. I will try to lay it out in a better format. I'm trying to see if I read in the data into my arrays correctly. Here is my code. Shouldn't it print out the 52nd element of my boys array? It doesn't however any thoughts or pointers would be much appreciated! Please let me know If I can clarify anything. Thanks again!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main(){
string boys[1000], girls[1000], target;
int loc, rank, i;

ifstream fin;
fin.open("babynames.txt");
        while (fin >> rank){
        fin>>boys[i]>>girls[i];
        i++;
        }

         cout<<boys[52]<<endl;
         loc = search(boys, target);
         loc = search(girls, target);
}
Last edited on
Initialize i to zero after you declare it. After the while loop, print i to see how many records you read. If you still don't get the expected result, post your input file.
Last edited on
Thank you boo, I initialized was 1000 meaning it read in everything correctly, is there a way to check a certain value in each array to make sure that all the data was read in correctly?

The data file was set up like this (number boysName girlsName)
i.e. 1 Bill Katie
is there a way to check a certain value in each array to make sure that all the data was read in correctly


You could have your program iterate over the array and print out all values.

Even better - learn how to use a debugger, so that you can inspect the contents of the array while the program is running.
cout << "I have just read in the strings " << boys[i] << " and " << girls[i] << endl;
Topic archived. No new replies allowed.