62 499
Assignments Done
98,8%
Successfully Done
In June 2018

Answer to Question #43102 in C++ for Manisha

Question #43102
A linear array of size 50 stores following information :
1. name of country
2. country's capital
3. 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 of the country.
b. to read the name of the capital city and display the country's name and per capita income.
Display an error message in case of an incorrect input.
Expert's answer
#include <iostream>
#include <string>
 using namespace std;
 class Country
 {
public:
string name;
string capital;
int capita;

Country(){};
void PrintforCountry()
{
cout << "The capital is " << capital << "\n";
cout << "The capita income " << capita << "\n\n";
}
void PrintforCapital()
{
cout << "The country is " << name << "\n";
cout << "The capita income " << capita << "\n\n";
}
 };
 int main()
 { 
Country Allc[50];
for (int i = 0; i < 50; i++)
Allc[i] = Country();
int count;
Allc[0].name = "USA";
Allc[0].capital = "Wasington D.C.";
Allc[0].capita = 380;
Allc[1].name = "Japan";
Allc[1].capital = "Tokyo";
Allc[1].capita = 420;
Allc[2].name = "Germany";
Allc[2].capital = "Berlin";
Allc[2].capita = 240;
Allc[3].name = "Canada";
Allc[3].capital = "Ottava";
Allc[3].capita = 310;
count = 3;
// We can add more Countries and information (till 50 countries) but we should change count, when we add.
bool b = true;
while(b)
{
bool boo = true;
string s;
int x;
cout << "What do you want to see :\n 1. Capital and per capita income of the country.\n 2. Name of the country and per capita income.\n 3. Exit \n Enter number of choice to choose:";
cin >> x;
switch(x)
{
case 1:
cout << "Enter the name of the country:";
cin >> s;
for (int j = 0; j <= count; j++)
{
if (Allc[j].name == s)
{
Allc[j].PrintforCountry();
boo = false;
continue;
}
}
if (boo)
cout << "There is no country with such name!\n";
break;
case 2:
cout << "Enter the capital of the country:";
cin >> s;
for (int j = 0; j <= count; j++)
{
if (Allc[j].capital == s)
{
Allc[j].PrintforCapital();
boo = false;
continue;

}
}
if (boo)

cout << "There is no country with that capital!\n";
break;
case 3:
b = false;
break;
default:
cout << "Please enter 1, 2 or 3 only.";
break;
}
}
return 0;
 }

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions