i having a problem with my C++ source code.....
can anyone help me to solve the problem with the below source code.... while running the program, if choose number 3 after key in the country or capital, it will show the answer with this "Sorry, no match found for.....". This "Sorry, no match found for....." will appear where the country or capital is match.
another problem is how to write the code to delete the data in the TEXT file one by one.
attention:
I'm using Turbo C++ 4.5
please enter a record before choosing number 2 and 3.
Code:
#include<iostream.h>
#include<stdio.h>
#include<fstream.h>
#include<stdlib.h>
#include<conio.h> //has the function clrscr()
#include<string.h> //has the function strcmp()
struct country{
char name[60];
char capital[60];
char name1[60];
char temp;
void input();
void output();
void specify();
};
void country::input()
{
cin.get(temp);
cout<<"\nEnter Country Name: ";
cin.getline(name, 60, '\n');
cout<<"Enter Country Capital: ";
cin.getline(capital, 60, '\n');
ofstream fout;
fout.open("data.txt", ios::app);
fout<<name<<endl;
fout<<capital<<endl;
fout.close();
clrscr();
}
void country::output()
{
ifstream fin("data.txt");
if(!fin)
{
cout<<"\nFile not found!"<<endl;
cout<<"Please record data first!"<<endl;
return;
}
else
cout<<"Country"<<" - "<<"Capital"<<endl;
while(!fin.eof())
{
fin.getline(name, 60, '\n');
fin.getline(capital, 60, '\n');
cout<<name<<" - "<<capital<<endl;
}
fin.close();
}
void country::specify()
{
ifstream fin("data.txt");
if(!fin)
{
cout<<"\nFile not found!!!"<<endl;
cout<<"Please record data first!"<<endl;
return;
}
else
if(fin)
{
cout<<"Enter country name or capital name
(x to main menu): ";
cin.get(temp);
cin.getline(name1, 60, '\n');
while(!fin.eof()&&strcmp(name1, "x")!=0)
{
fin.getline(name, 60);
fin.getline(capital, 60);
if(strcmp(name1, name)==0)
{
cout<<"The capital of "<<name1<<" is "<<capital<<endl;
}
if(strcmp(name1, capital)==0)
{
cout<<name1<<" is the capital of "<<name<<endl;
}
}
if(strcmp(name1, "x")!=0)
{
cout<<"\nSorry, no match found for "<<name1<<endl;
}
}
fin.close();
return;
}
void main()
{
struct country record;
char selection='0';
while(selection!='x')
{
cout<<"\n 1. Enter Data : "<<endl;
cout<<" 2. Display Record : "<<endl;
cout<<" 3. 123 : "<<endl;
cout<<" x. Exit"<<endl;
cout<<"\tSelection : ";
cin>>selection;
switch(selection)
{
case '1': clrscr();
record.input();
break;
case '2': clrscr();
record.output();
break;
case '3': clrscr();
record.specify();
break;
case 'x': clrscr();
exit(1);
break;
default: cout<<"\nInvalid Input!!!"<<endl;
}
}
}
Thanks for all your help..........