C++ program to find capital of a country entered using structure

Just copy this program in a notepad and save it as .cpp file now install any C++ environment the most common one is Turbo C++ install it in your windows drive. After installation go to the windows drive and search for a software as turbo c++ now open the folder you can see multiple file on it click on the file named as BIN now save the notepad file first saved in it now open the software and go to files and click on Files-Open a separate menu gets opened now select the file saved open it now press ctrl+f9 press enter the press alt+f9 you can see the program to get opened.
  This program basically takes input of a country and its capital name then give you an option to search capital for a particular country.

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct world
{
char country[30];
char capital[30];
};
void main()
{
clrscr();
world w[10];
int i=0,n;
cout<<"Enter no. of records (<=10): ";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter the country: ";
gets(w[i].country);
cout<<"Enter the capital: ";
gets(w[i].capital);

}
char ch,*cont;
int flag;

cout<<"\n\t1.Search for capital\n\t2.List all Records\n\t3.Exit";
cin>>ch;
do
{
if(ch=='1')
{
cout<<"\nEnter the country : ";
gets(cont);
flag=0;
for(i=0;i<n;i++)
{
if(strcmp(cont,w[i].country)==0)
{
cout<<"\n capital is: "<<w[i].capital;
flag=1;
break;
}
}
if(flag==0)
cout<<"\ncountry not found";
}
if(ch=='2')
{
cout<<"\nCOUNTRY\t\tCAPITAL";
cout<<"\n-------\t\t-------";
for(i=0;i<n;i++)
cout<<"\n"<<(w[i].country)<<"\t\t"<<(w[i].capital);

}


cout<<"\n\t1.Search for capital\n\t2.List all Records\n\t3.Exit";
cin>>ch;

}while(ch!='3');

getch();
}
For any kind of queries please feel free to comment.

Share this

Related Posts

Previous
« Prev Post