Thread: Library file problems

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    5

    Library file problems

    My programs compiles and runs, and it has a .TXT file it is supposed to refer to. I'm just testing right now and i only have two things in my .TXT file:

    Jimmy Eat World
    Jimmy Eat World
    Brandy
    Full Moon

    This is in notepad and the first line is the band and the second line is the CD name. When I enter Jimmy Eat World in my program it says it has it in file and shows the output, but when i enter Brandy it says it has it in file, but show the Jimmy Eat World information. Is there a problem with my code?

    Code:
    #include<fstream.h>
    #include<string.h>
    #include<stdlib.h>
    
    int main()
    {
    	// Declare variables
    	char band[40];
    	char cd_name[60];
    	int found;
    	ifstream infile;
    	found = 0;
    
    	cout << "Sheila Blakely\n";
    	cout << "Final 2002\n\n";
    	cout << "Music Library\n";
    
    	//Input from the user
    	cout << "Enter a singer or band name: ";
    	cin.get(band,80);
    	cin.ignore(80, '\n');
    	cout << '\n';
    
    	infile.open ("LIBRARY.TXT", ios::in);  // open the library file
    
    	if(infile)
    	{
    		do
    		{
    			infile.get(band, 80);  // get the band from the file
    			infile.ignore(80, '\n');
    			infile.get(cd_name, 80);  // get the CD name from the file
    			infile.ignore (80, '\n');
    
    			if ((strcmp (band, cd_name)) == 0 )
    			{
    				found = 1;
    				break;
    			}
    
    		}
    		while (found != 1 && !infile.eof());
    	}
    	else
    	{
    		"An error occured while opening the file.\n";
    	}
    
    	infile.close(); // close the input file
    
    	if (found == 1)
    	{
    		cout << "The book title that you requested is in our inventory.\n\n";
    		cout << "***CD Information*** \n\n";
    		cout << "CD Title: " << cd_name << '\n'; // print the title of the CD
    		cout << "Band/Singer: " << band << '\n' << '\n';    // print the band or singer name
    	}
    	else
    	{
    		cout << "The band or singer you typed in must not be very good\n";
    		cout << "we don't waste time with bad music.\n\n";
    	}
    
    	return 0;
    }
    Last edited by beila00; 06-03-2002 at 11:45 AM.

  2. #2
    Registered User
    Join Date
    Jun 2002
    Posts
    5
    actually, even if you enter a name not in the library, it gives the Jimmy Eat World information

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    194

    Re: Library file problems

    Look at the bold lines, you were comparing the wrong data, and overwriting what the user inputted.


    Code:
    #include<fstream.h>
    #include<string.h>
    #include<stdlib.h>
    
    int main()
    {
    	// Declare variables
    	char band[80];
                    char cd_name[80];
                    char lookfor[80]; // user input will be stored here
                 
    	int found;
    	ifstream infile;
    	found = 0;
    
    	cout << "Sheila Blakely\n";
    	cout << "Final 2002\n\n";
    	cout << "Music Library\n";
    
    	//Input from the user
    	cout << "Enter a singer or band name: ";
    	cin.get(lookfor,80); 
    	cin.ignore(80, '\n');
    	cout << '\n';
    
    	infile.open ("LIBRARY.TXT", ios::in);  // open the library file
    
    	if(infile)
    	{
    		do
    		{
    			infile.get(band, 80);  // i changed the declaration of band and cd_name to 80 chars, so you can read up to 80 chars
    			infile.ignore(80, '\n');
    			infile.get(cd_name, 80);  // get the CD name from the file
    			infile.ignore (80, '\n');
    
    			if ((strcmp (band, lookfor)) == 0 ) //you were comparing the band to the cdname, so jimmy eat world, the first entry in the text file would always be found
    			{
    				found = 1;
    				break;
    			}
    
    		}
    		while (found != 1 && !infile.eof());
    	}
    	else
    	{
    		"An error occured while opening the file.\n";
    	}
    
    	infile.close(); // close the input file
    
    	if (found == 1)
    	{
    		cout << "The book title that you requested is in our inventory.\n\n";
    		cout << "***CD Information*** \n\n";
    		cout << "CD Title: " << cd_name << '\n'; // print the title of the CD
    		cout << "Band/Singer: " << band << '\n' << '\n';    // print the band or singer name
    	}
    	else
    	{
    		cout << "The band or singer you typed in must not be very good\n";
    		cout << "we don't waste time with bad music.\n\n";
    	}
    
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  3. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  4. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM