Finding 2 words in array

I have 1 dimensional two arrays each consisting of names of cities and number of items delivered to that particular city.
Now I need to find a two letter word from the array list of city name (say, New York) from a user defined list.
Can i please get an idea how to perform the task?

Last edited on
Look at each element of the array.

Compare each element to the search term (e.g. "New York").

If they match, you've found it.

Which of these steps is giving you trouble first?
Last edited on
But the input can change, could be any two letter word city, and not just New York. Since the input is not fixed by the programmer.
Look at each element of the array.

Compare each element to the search term the user typed in.

If they match, you've found it.
OP, I don't think you're being very clear in your problem.

If the input is not "fixed", how are you getting input? Is the array of city names fixed? ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>
int main()
{
    std::string arr[] = { "New York", "Albany", "San Francisco", "Newark", "Des Moines", "Atlanta" };

    std::string city_name;
    std::cout << "Type city name: ";
    std::getline(std::cin, city_name); // getline needed if user inputs names with spaces!

    for (int i = 0 ; i < 6; i++)
    {
        if (arr[i] == city_name)
        {
            std::cout << "City found!" << std::endl;
        }
    }
}

That should be plenty to get you started.
Last edited on
Actually the input is end-user given not programmer defined. And the input that you've provided is fixed by the programmer. And also this is my program.

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
32
33
#include <iostream>
#include <conio.h>
#include <string>

using namespace std;

int search (string);

main()
{
    string a[100];
    int b[100];
    int i;
    
    cout<<"Enter the cities: ";
    for(i=0 ; i<10 ; i++)
    {
        cin>>a[i];
    }

    cout<<"Enter items: ";
    for (i=0 ; i<10 ; i++)
    {
        cin>>b[i];
    }

     cout<<"City     No of items \n";
    for (i=0 ; i<10 ; i++)
    {
         cout<<a[i]<<"  "<<b[i];
         cout<<endl;
    }
}
Last edited on
As already pointed out by Ganado if your strings have spaces you need to use getline(), the extraction operator stops processing strings when it encounters whitespace.

Topic archived. No new replies allowed.