Why this is accepting only one string ??

The following program is accepting only one city name value..

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
34
35
36
37
38
39
40
41
42
43
44
  #include<iostream>
#include<cstring>
using namespace std;
int main()
{
string search;
int k=1,i;
struct addr
   {
    string s;
    int zip;
     };
addr wb[100];

for(i=0;i<5;i++)
     {
       cout << "\n\nEnter City name : ";
       getline(cin , wb[i].s);
       cout << "\n\nEnter zip code : ";
       cin >> wb[i].zip;

           }



cout << "\n\n ---------------------------------------------------------";
cout << "\n\nEnter city name to get its zipcode ";
getline (cin,search);

for(i=0;i<=5;i++)
{
   if (wb[i].s == search)
        {
          cout << "\nCity name : " << search;
          cout << "\nZip Code  : " << wb[i].zip;
                k=2;
                    }
                        }
if(k!=2) cout << "\n\nNo match Found";

return 0;

}


This program is accepting only one string as city name .
HOW TO SOLVE THIS PROBLEM? Do I need to flush the buffer? how to use getline ?? or anything else..

Please describe the reason why it is taking only one "city name" value??
closed account (2b5z8vqX)
Data remains in the input buffer after the invocation of the std::basic_istream<>::operator >>() member function. The std::getline() function will extract the remaining data and assign it to the second argument. This is why the program no longer allows for user input after the first invocation of std::basic_istream<>::operator >>() in your loop.

This can be fixed by invoking the std::basic_istream<>::ignore() member function after this statement: "cin >> wb[i].zip;".

(Correct me if I am wrong, please.)
Topic archived. No new replies allowed.