Answer to Question #23539 in C++ for Rohan Saha

Question #23539
Write a menu driven program in C++ that will create a data file containing name of a country and its capital. Write an interactive, menu-driven program to do the following:
1.Determine the country given the capital
2.Determine the capital given the country
3.Quit the program
1
Expert's answer
2013-02-01T11:03:08-0500
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <string>
#include "string.h"
#include <cstring>
#include <vector>
#include <fstream>
using namespace std;
class tel{
public:
string country;
string capitol;
void init(string a, string b)
{
country=a;capitol=b;
}
void print()
{
cout<<"country : "<<country<<"& capitol : "<<capitol<<endl;
}

};
void outMENU(){
cout<<"1.Determine the country given the capital\n"<<
"2.Determine the capital given the country\n"<<
"3.Quit the program\n";
}

int main()
{
int n=1;
vector <tel> a;
tel temp;
string count, cap;
ifstream data("data.txt");
while (!data.eof()){
getline(data,count,' ');
getline(data,cap,'\n');
temp.init(count,cap);
a.push_back(temp);
}
for (int i=0;i<a.size();i++)
{
cout<<i<<" ";
a[i].print();
}

outMENU();
cin>>n;
while (n!=3){
bool match=0;
switch (n){
case 1:
cout<<"Enter country : ";
cin.ignore();
getline(cin,count,'\n');
for (int i=0;i<a.size();i++)
{
if (a[i].country==count) { cout<<" capitol "<<a[i].capitol; match=1;}
}
if (match==0) cout<<"no match\n";
break;
case 2:
cout<<"Enter capitol : ";
cin.ignore();
getline(cin,cap,'\n');
for (int i=0;i<a.size();i++)
{
if (a[i].capitol==cap) { cout<<" country "<<a[i].country;match=1;}
}
if (match==0) cout<<"no match\n";
break;
default: break;
}
cout<<endl;
outMENU();
cin>>n;
}
system("PAUSE");
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

Assignment Expert
27.01.14, 16:51

Dear customer, Please be more specific.

soumen
05.01.14, 13:24

the program has 10 defects

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS