Linear Search

Discussion in 'Software' started by button, Apr 25, 2004.

  1. button

    button Private E-2

    The search function I have isn't working, possibly because of the scope of the while & if functions or that I am using the wrong functions.
    Please see if a different search or maybe if the code needs changing, Thank you button.

    Code:
    #include <iostream.h>		  //Pre Processor Directives
    #include <fstream.h>		   //Libary for Input funcitons
    #include <iomanip.h>		  //Input/Output manipulation libary
    #include <string.h>
    
    void main()
    {
    ofstream file("Database");			//Create a new file
    file.open("Database");
    
      char* name[9];
      name[0] = NULL;
      name[1]="HP4p";
      name[2]="HP5L";
      name[3]="HP6p";
      name[4]="Cannon2A";
      name[5]="Cannon3B";
      name[6]="Epson2";
      name[7]="Epson3";
      name[8]="Epson4A";
    
      float cost[9];
      cost[1]=32.99;
      cost[2]=24.99;
      cost[3]=30.00;
      cost[4]=23.45;
      cost[5]=31.99;
      cost[6]=25.00;
      cost[7]=30.00;
      cost[8]=27.99;
    
      int quantity[9];
      quantity[1]=100;
      quantity[2]=50;
      quantity[3]=75;
      quantity[4]=30;
      quantity[5]=80;
      quantity[6]=10;
      quantity[7]=25;
      quantity[8]=30;
    
     char name8[13]="Cartridge";
     char cost8[11]="£00.00p";
     char quantity8[12]="Quantity";
    	{
      for (int x = 1; x < 8; x++)
    	file<<name[x]<<' '<<cost[x]<<' '<<quantity[x];
    	}
    	file.close();					//close the file
    
    int num=0,x;
    char sought[15];			//Declare Variables
    
     cout<<"Display all records press '1'  "<<"Do a Search press '2'  "<<" '0' to Quit "<<endl;
    	cin>>num;
    
    	if (!num==0)
    	{
    	   if (num==1)
    		{
    		  cout<<"Total Items in stock \n"<<endl<<endl;
    
    		ifstream file("Database",ios::in);	//ios::in for input from the file
    
    		   cout<<setw(20)<<setfill(' ')<<setiosflags(ios::left)<<name8;
    	 cout<<setw(10)<<setfill(' ')<<setiosflags(ios::right)<<cost8;
    	   cout<<setw(12)<<setfill(' ')<<setiosflags(ios::right)<<quantity8<<endl;
    
    			for (int x = 1; x < 8; x++)
    			 {
    			  cout<<endl<<setw(20)<<setfill(' ')<<setiosflags(ios::left)<<name[x];
    			  cout<<setw(8)<<setfill(' ')<<setiosflags(ios::right)<<cost[x];
    			  cout<<setw(8)<<setfill(' ')<<setiosflags(ios::right)<<quantity[x];
    			 }
    		}
    
    	  cout<<endl<<endl<<"Display all records press '1'  ";
    	  cout<<"Do a Search press '2'  "<<" '0' to Quit "<<endl;
    	  cin>>num;
    
    	if (num==2)
    	 {
    		 cout<<endl<<"To search by the printers make type in the name e.g HP4p: Type 'N' for no more"<<endl;
    	   cin>>sought;
    
    	 ifstream file("Database");		//open file for input FROM file
    	 //while(!file.eof())
    		//{ //file.seekg(0);			  //set file pointer to first character
    
    	   if (strcmp(sought,"N")!=0)
    			{
    			delete [] name[0]; //it is safe to call delete [] on NULL pointer.
    	name[0] = new char[strlen(sought)];
    
    			strcpy(name[0],sought);
    
    	x=8;
    
    		 while (strcmp(name[x],sought)!=0)
    		  x--;
    
    	if (strcmp(name[x],sought)==0)
    		 {
    			 cout<<endl<<setw(20)<<setfill(' ')<<setiosflags(ios::left)<<name[x];
    			  cout<<setw(8)<<setfill(' ')<<setiosflags(ios::right)<<cost[x];
    			  cout<<setw(8)<<setfill(' ')<<setiosflags(ios::right)<<quantity[x]<<endl;
    			 }
    			else
    		 {
    			cout<<"No such item in stock"<<endl<<endl;
    		 }
    
    		 file.close();
    
    		 cout<<"Display all records press '1'  ";
    		 cout<<"Do a Search press '2'  "<<" '0' to Quit"<<endl;
    		 cin>>num;
    		}
    	 }
    
    	}
    }
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds