C++ .txt file into an array

I'm new to the forums and hopefully someone can help me out. not just give me the answer.
i have to write a program that wants me to put information from the text file

baltimore
6214521
1452415
texas
1245131
1245211
and so on, an put it in an array. After that the person using the program has to type in a city and if the city is in my array, it has to send that info back. I'm having problems trying to get this info into an array and telling it which one is a city and which ones are the populations.
If someone can give me an example on how to get info from a text file and put it into an array, it would be much appreciated.
closed account (o3hC5Di1)
Hi there,

It's hard to tell where you're going wrong without looking at your code, so if you could share your code with us (don't forget to wrap it in code-tags) that would really help.

As a general hint, you are probably going to read everything into one array, you will want an array to be of type char:

1
2
3
char populations[50][50];  //holds 50 entries of 50 characters length

file >> populations[++i];  //you'll need a counter and loop here, this puts a line of the file into the array 


Now, will the whole file look like this, meaning a city name followed by 2 population numbers?
Otherwise, you search the array for that city, and print the next two entries of the array as the population:

1
2
//search the array for "texas", which is at key 3
std::cout << populations[4] << std::endl << populations[5];


Again, it's a bit hard to give advice without giving away too much and not seeing any code from your part, so if you would like mor specific help, please do come back to us including your code.

Hope that helps.

All the best,
NwN
Last edited on
Thank you soo much with the assit!! I've an idea now. Don't have the code yet because im still brainstorming what im gonna do. The text file has 20 states with 2 lines of population. How can i make the array save the cities so that when the it says "please enter a city" and they put dallas, it can find it as dallas?

Your last reply really helped alot. Just need help on the text file to arrays. Not a big fan of arrays =(
closed account (o3hC5Di1)
Hi there,

Just read everything into one array:

1
2
3
4
5
6
7
//after reading from file:
arr[0] = baltimore
arr[1] =6214521
arr[2] =1452415
arr[3] =texas
arr[4] =1245131
arr[5] =1245211


Now you can search for a state like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int state_key = -1;

for (int i=0; i< arr_max; i+=2) //states are 2 keys away from eachother
{
    if (arr[0] == search_string) //if we've found the state
   {
        state_key = i;  //save the position
        break; //stop searching
    }
}

//check here if state_key is higher than or equal to zero,if not no state was found

std::cout << "Pop1:" << arr[state_key+1];  //first population of the state is one index higher 


In order to read the file into the array, you will have to declare an object of ifstream which you can then read, just like you do std::cin:

1
2
3
4
5
int s = 0;
while (filestream.good()) //while there is stuff to read
{
   filestream >> array[++s]; //read a line into the array
}


Hope that helps.

All the best,
NwN
Thanks again for your help!! it works for the first state on the list. The thing is when someone enters "baltimore" which is the first thing in my text file, it gets the population just fine but when i put in a different state or whatever, it still gives me the popluation of baltimore. You can put in "irebbvflhervl" and it still gives the same pop. Need to know how to make it see each state the user puts in and goes and grab it from the array. Feel like im annoying you but your info really helps. Thanks in advance

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
 int main()
{ char array[100][100];
    ifstream inFile ("pops2000-2010.txt");
    
   int array_max = 100;
    string search_string;
  cin >> search_string;
       
int s = -1;
while (inFile.good()) 
{
   inFile >> array[++s];
}

int state_key = 0;

for (int i=0; i< array_max; i+=2) 
{
    if (array[0] == search_string)
   {
        state_key = i; 
        break;
    }
    
    
}



std::cout << "Pop1: " << array[state_key+1] << endl;  
std::cout << "Pop2: " << array[state_key+2] << endl;
Last edited on
closed account (o3hC5Di1)
Hi there,

No worries, we're here to help :)

The error is here:

17
18
19
20
21
22
23
24
for (int i=0; i< array_max; i+=2) 
{
    if (array[0] == search_string)  //array[0]  should be array[i], otherwise it always checks baltimore
   {
        state_key = i; 
        break;
    }
}


I would advise to make array of type std::string too (just like search_string):

1
2
char array[100][100];
string array[100];


Do you understand the actual process or do you need more explanation?

All the best,
NwN

Last edited on
Topic archived. No new replies allowed.