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.
o another example of interface.
|
|
|
|
|
|
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.
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

