/* This is what I wrote.. */
Code:
#include <string.h> //strcmp
#include <iomanip.h>
#include <fstream.h> //file stream and cin ,cout
#include <iostream.h> //setiosflags, setprecision
const int max = 9; // 10 records in file
struct SalesRecord // array of struct (which is sorted)
{
char item_code[8]; //given
int quantity_on_hand;
float unit_selling_price;
};
void main (void)
{
SalesRecord Sales[max];
ifstream ifsServ; //Input file stream
ofstream ofsServ; //Ouput file stream
char inputfile[30];
char outputfile[30];
cout << "Enter input path: " << endl;
cin >> inputfile;
cout << "Enter output path: "<<endl;
cin >> outputfile;
ifsServ.open(inputfile); //Open the input file stream
ofsServ.open(outputfile); //Open the output file stream
char code[8];
int quantity;
float value;
int lowx = 0; //index ranges
int hix = max -1;
int midx = 0;
int i = 0;
if (!ifsServ)
{
cout << "Error opening input file" <<endl;
}
else
{
if (!ofsServ)
{
cout << "Error opening output file" << endl;
}
else
{
//Read first line:
ifsServ >> Sales[i].item_code[8]
>> Sales[i].quantity_on_hand
>> Sales[i].unit_selling_price;
}
}
cout << "Enter Item Code: " ;
cin >> code;
while (strcmp (code,"quit") !=0)
{
cout << "Enter Quantity Order: " ;
cin >> quantity;
// binary search (in the book)
midx = (hix + lowx) /2;
while ( (hix > lowx) && (Sales[midx].item_code != code))
{
if (Sales[midx].item_code < code)
{
lowx = midx+1;
}
else
{
hix = midx - 1;
}
midx = (hix + lowx) / 2;
}
if (Sales[midx].item_code == code) //if found
{
cout << "Found a MATCH!" << endl;
}
else
{
cout << "Item Code Not Recognized!" << endl; //if not found
}
cout << "Enter Item Code: " ;
cin >> code;
i++;
ifsServ >> Sales[i].item_code[8]
>> Sales[i].quantity_on_hand
>> Sales[i].unit_selling_price;
}
}
/*
It compiles but cannot find a match with the code entered by the user? For example enter code = ABC1110 and in the record there is a ABC1110 in item_code. Any specific help would be aprreciated! */
[code][/code]tagged by Salem