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.
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.
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 =(
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
}
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