Searching with loops

how to set up correctly to display right information

Page 1 of 1

1 Replies - 912 Views - Last Post: 25 March 2008 - 04:25 AM Rate Topic: -----

#1 goblue89x   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 05-March 08

Searching with loops

Posted 24 March 2008 - 07:42 PM

my program reads a file and stores the last name, first name, and student number of a person. then asks to search for a person using loops. the problem im getting now is with using the loop is they way i have it set up its going through the loop and if the name isnt the first one on the list it will give me my "name is not found" message til the loop reaches the name.

i used the do-while statement so if the name isnt found you have the option of re-doing your search til you find it

with still using a loop, i could i avoid getting that error message every single time?



//Date: 3-13-08
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>

using namespace std;


int main() {  

	string lastname, firstname, studentnum, temp, searchname;

	string lastnamearr[8], firstnamearr[8], studentnumarr[8];

	long int studentNUM;
	char ch, c;
	string fileOne = "A7.txt";
	string fileTwo = "A7N.txt";
	ifstream myfile ("A7.txt");
	ifstream myfile2 ("AN7.txt");
	ifstream inFile;
	ofstream outFile;
	int i;


	
	if (myfile.is_open()) {
		int i = 0;
	   while (!myfile.eof() && i < 8) {
		  getline (myfile,lastname);
		  lastnamearr[i] = lastname;	

		  getline (myfile,firstname);
		  firstnamearr[i] = firstname;

		  getline (myfile,studentnum);
		  studentnumarr[i] = studentnum;

		  getline (myfile,temp);
		  
		  i++;
	   }
	   //printing the arrays
	   for(i=0; i<8; i++){
		   cout << lastnamearr[i] <<endl<< firstnamearr[i] <<endl<< studentnumarr[i] <<endl<< endl;
	   }

	   myfile.close();
	}
		else cout << "Unable to open file";
	
		  try {
				 inFile.open(fileOne.c_str());							 //if file error it will be thrown
				 if (inFile.fail()) throw fileOne;
		  }
		  catch (string in) {
			   cout<<"the input file"<<in
			   <<" was not successfuly opened."<<endl;
			   cout<<"No new file created."<<endl;
			   exit(1);
		  }
		  
cout<<"_______________________________________________________________________________"<<endl;
	 
cout<<"\nEnter the last name to be searched for:";
cin>>searchname;
do
{
for(i= 0; i < 8; i++)
  {
	  if(lastnamearr[i] == searchname)
	  {
	  cout<<"The matches found are:"<<endl<<endl;
	  cout<<lastnamearr[i]<<endl<<firstnamearr[i]<<endl<<studentnumarr[i]<<endl<<endl;
	  }
	  else
	  {
	  cout<<"There are no matches from the name entered"<<endl<<endl;
	  cout<<"Would you like to enter a new name?"<<endl;
	  cin>>c;
	  }
		
   }
}while ( c == 'y' || c== 'Y');	  

			
	cin.ignore();cin.ignore();

	return 0; 
}


This post has been edited by goblue89x: 24 March 2008 - 07:43 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Searching with loops

#2 syazhani   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 34
  • Joined: 21-March 08

Re: Searching with loops

Posted 25 March 2008 - 04:25 AM

View Postgoblue89x, on 24 Mar, 2008 - 07:42 PM, said:

cout<<"\nEnter the last name to be searched for:";
cin>>searchname;
do
{
for(i= 0; i < 8; i++)
{
if(lastnamearr[i] == searchname)
{
cout<<"The matches found are:"<<endl<<endl;
cout<<lastnamearr[i]<<endl<<firstnamearr[i]<<endl<<studentnumarr[i]<<endl<<endl;
}
else
{
cout<<"There are no matches from the name entered"<<endl<<endl;
cout<<"Would you like to enter a new name?"<<endl;
cin>>c;
}

}
}while ( c == 'y' || c== 'Y');


Your logic is flawed from what I can see. Once you're in the else block, it doesn't matter what you answer for cin >> c, as the for loop will only terminate once i >= 8, then only you get to the while loop which checks for c.

What I think you'd want:
bool found = false;
char choice = 'y';
while(c == 'y' || c == 'Y')
{
	cin >> searchStr; //Do you really want cin? Probably getline since you use getline to store the names earlier
	for(size_t i = 0; i < 8; ++i)
	{
		if(nameArray[i].compare(searchStr) == 0) 
		{
			cout << "Found " << searchStr << " at location " << i << endl; 
			found = true;
			break;
		}
	}
	if(!found) std::cout << "Name not found..." << endl;
	cout << "Continue searching? [y/Y]: ";
	cin >> c;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1