Thread: Problem with..........

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    6

    Question Problem with..........

    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..........

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Inside your specify function you need some other marker for when you got a match or not. If the user types in a valid country or capital, the value of name1 will be what they typed in. You compare that with "x" and if it isn't equal then you say "no match". Obviously, every time they type a country or capital that isn't "x" they will get that displayed. Add a bool or use some other flag to indicate that a match was found.

    As far as deleting the data one at a time, now that we can see your code it shouldn't be too hard for you. At the minimum you can read in the entire file one record at a time. If the record you want to delete comes up, ignore it, otherwise write the record out to a temporary file. Then when you are done, delete the old "data.txt" and rename the temporary file to "data.txt". You could also read all the information into a data structure so you don't have to worry about the temporary file.

    As a side note, you are using some old and non-standard stuff in your code (e.g. <iostream.h>, void main()). At some point you might want to consider updating that to more standard C++ that will have a much higher chance of continuing to work correctly in the future. Do a search on the FAQ or on the board for standard headers and "void main evil", or ask if you are curious about it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM