#include<iostream>
#include<cstring>
usingnamespace 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??
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;".