Class Notes:
Search program:

We need to understand how a search program works. Pg 122-123

An example of search program is www.google.com

How does a search program look like?

What do we need?
The story of little search engine.

  1. There is an interaction or interface (display and input) Display is the message asking for what you are looking for, then it waits for you to enter a value “input” that we call search id. The input goes to a variable. We give a name to an input variable searchid or searchkey.

     

Web    Images    Groups    News    Froogle    Maps    more »

 

 


  Advanced Search
  Preferences
  
Language Tools

o       another example of interface.

Search

 

  1. Think of what you need in order to perform a search.

o       You have to have a file of related information. A place where data is stored.
There are two ways to store data; in a file or in an
array.

  1. Bring the file into your program. Declare a variable from it. You need to declare your searchid.
    What else do we need?

o       A loop to go through possibly to entire the file or array

o       if statement to compare the searchid or searchkey to every value of associated variable in the file.

Compared it till success is encountered or failure when the loop has reached end of file without finding anything displaying there is no match.

 

CODE: We are going to examine a supermarket program which has an id and a price. Every item has an unique id. Example; garlic = 1, water = 2, etc.
Look at this program,
Can you identify interface?

File coming to the program?

Can you identify the loop?
Can you identify there is a comparison?

Can you identify there is a success and failure?
C++ CODE
#include<fstream.h>

main(){

Long int searchid;

Long int id;

float price;

cout<< “ENTER THE SEARCH ID: ”;

cin>>searchid;

ifstream fin (“itmprice.in”)

while(fin>>id>>price){

if(id=searchid){

cout<< “ITEM ID IS ” <<id<<endl;

cout<< “PRICE IS ” <<price<<endl;

fin.close();

}//IF

} //WHILE

fin.close();

cout<< “ITEM ID NOT FOUND: ” <<endl;

return 0;

}//MAIN

 

VISUAL BASIC CODE:
Code should be in a button click
     Dim id, searchid, i As Integer

        Dim price As Double

        Dim fin As IO.StreamReader = IO.File.OpenText("supermarket.txt")

        i = 1

        searchid = TextBox1.Text

       

        While (fin.Peek <> -1)

 

            id = fin.ReadLine

            price = fin.ReadLine

 

            If id = searchid Then

                TextBox2.Text = FormatCurrency(price)

                Exit While

            End If

            i = i + 1

        End While

        If fin.Peek = -1 Then

            TextBox2.Text = "ITEM NOT FOUND "

        End If