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; }



LinkBack URL
About LinkBacks


