"The best way to cheer yourself up is to try to cheer somebody else up." Mark Twain

Friday, January 15, 2010

A Linear array if size 50 stores following informations : name the country, country's capital and per capita income of the country. Write a complete program in C++ to do the following:

(a) To read a country's name and display capital and per-capita income.
(b) To read name of the capital city and displays country's name  and displays country's name and per capita income.
Display an error message in case of an incorrect input.

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>

struct country{
char country_name[20];
char capital_name[20];
float incm;
};

typedef country C;

void main()
{

C c[50];

for(int i = 0; i<49; i++)
{
cout<<"\n Enter Country's name :";
cin>>c[i].country_name;

cout<<"\n Enter the name of Capital :";
cin>>c[i].capital_name;

cout<<"\n Enter per capita incme:";
cin>>c[i].incm;

}

char cname[20];
char capital[20];
char choice;
int n, flag = 0 ;
do
{

cout<<"\n ***********MENU***********\n\n";
cout<<" 1. Know Capital for a country :";
cout<<"\n 2. Know County for a capital :";

cout<<"\n\n Enter your choice :";
cin>>n;

switch(n)
{
case 1:

cout<<"\n Enter a country name :";

gets(cname);
for(i=0; i<50; i++)
if(strcmp(c[i].country_name, cname)==0)

cout<<"The capital of  "<< c[i].country_name <<" is " << c[i].capital_name ;
cout<<"\n Per caita income = Rs. "<< c[i].incm;
flag = 1;
break;
}
break;

case 2:cout<< "\n Enter a capital name :";

gets(capital);
for(i=0; i<50; i++)
if(strcmp(c[i].capital_name, capital)==0)
{
cout<<"\nThe capital of "<< c[i].capital_name  <<" is " << c[i].country_name ;
flag = 1;
cout<<"\n Per capita income = Rs. "<< c[i].incm;
break;



}
break;

default:
cout<<"\n Wrong Input! ";
}

if(flag == 0)
cout<<"\n No Information .";

cout<<"\n\n Do you want to continue (Y/N) :";
cin>>choice;
}while(choice == 'Y' || choice == 'y');
getch();


}

No comments:

Post a Comment

C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off.
Now Playing: Ballade Pour Adeline

About Me

My photo
I m an IT lecturer of a college. I love social-work. I want to do something beneficial for society before dying , that can promote our society, to some extent.