Aug 21, 2017 at 5:51pm UTC
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 Aug 21, 2017 at 5:51pm UTC
Aug 21, 2017 at 6:37pm UTC
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 Aug 21, 2017 at 6:38pm UTC
Aug 23, 2017 at 10:29am UTC
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.
Aug 23, 2017 at 12:08pm UTC
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.
Aug 23, 2017 at 1:21pm UTC
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 Aug 23, 2017 at 1:27pm UTC
Aug 25, 2017 at 11:42am UTC
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 Aug 25, 2017 at 11:45am UTC
Aug 25, 2017 at 2:50pm UTC
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.