string program

In this program i want to input several city names and display only those beginning with "B" or "C".my code is displaying the city name just after inputting the city name.
I want to display all the city name beginning with B or C together after inputting all of them.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
    string s;
    string s1("B");
    string s2("C");
   cout<<"enter the city names:"; 
    for(int i=0;i<7;i++)
    {
                    cin>>s;
    
     if(s.at(0)==s1.at(0) || s.at(0)==s2.at(0))
    {
             cout<<"city name:"<<s<<"\n";
    }
}
  system("pause");
}          
Last edited on
Instead of printing the cities right away you could add them to a vector (or array) and when the loop has ended you can loop through the cities and print them.
thanku .it worked :)
Topic archived. No new replies allowed.