Thread: search array program

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    12

    search array program

    i need some help with a program. it is fairly large, so i will just attach a zip file with the source and input/output files. i just started to learn arrays and i am a little confused. don't worry about how the output is formatted. i can fix that later. i just need help with the arrays and functions.

    Code:
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    long isInInventory(string,string[],long);
    //  Parameters:
    //		codeToFind:	product code being searched for	
    //		code:		product codes in inventory
    //		count:		number of product codes in inventory
    //  Discussion:
    //	If a product with a code matching the parameter codeToFind is found in the array
    //	code then the index of this code is returned, otherwise -1 is returned
    
    long readInventory(string,string[],string[],double[]);
    //  Parameters:
    //		storeName:		name of store
    //		code:			product codes in inventory
    //		description:	product descriptions in inventory
    //		unitPrice:		unit prices in inventory
    //  Discussion:
    //	Reads data from inventory file into arrays above.
    //	Returns number of products actually read
    //	Data file is formatted as follows: 
    //		first line: name of the store (at most 40 characters)
    //		even-numbered lines: five-character product code followed by product description 
    //		other odd-numbered lines: unit price of product described in above line
    
    int main()
    {
        // Programmer: Mike Gozdiskowski
        // Date: 11/13/2002
        // Discussion: Allow user to search inventory of a small store.
    	// 
    	// Input:  User must enter product codes until a sentinel value is entered
    	// Output: If user enters a product code that corresponds to the inventory
    	//         all of the information for that product code will be displayed
    	//         on the screen and also printed to an output log file for future
    	//         reference. If the user enters a bad product code; they are
    	//         informed on the screen and also in the output file.
    	
    	//Declaration & description of variables
    	string storeName;						//Store name
    	string productCode;                     //Code entered by user
    	string A[30];							//Array to store values for code
    	string B[30];							//Array to store values for description
    	double C[30];							//Array to store values for price
    	long count;								//Number of positions of array A with meaningful values
    	long index;							    //Array position for array 
    
    	//Read data from file
    	count=readInventory(storeName,A,B,C);
    
        //Print heading to log file program5.out
    	ofstream fout("program5.out");
    	fout<<storeName;
    	fout<<"Product Code   Description                    Unit Price"<<endl;
    	fout<<"************   *************************      **********"<<endl;
    
    	//Welcome user to the program & ask for product code
        cout<<"Welcome to "<<storeName<<endl;
    	cout<<"Please enter a product code (Q to Quit): ";
    	cin>>productCode;
    
    	//Loop until sentinel is entered
    	while(productCode!="Q")
    	{
    		//Use a function to search the product code array for this code
    		index=isInInventory(productCode,A,count);
    
    
    		//If the product code is found print information to the log file 
    		//and the user screen otherwise print the product code and the error 
    		//message to both the log file and the user screen
    		if(index>=0)
    		{
    			cout<<"Product Code: "<<A<<endl;
    			cout<<"Description:  "<<B<<endl;
    			cout<<"Unit Price:   "<<C<<endl;
    			fout<<setw(5)<<A<<setw(25)<<B<<setw(40-(A[].length()+B[].length()))C[]<<endl;
    		}
    		else
    		{
    			cout<<productCode<<"ERROR: Invalid product code"<<endl;
    			fout<<productCode<<"ERROR: Invalid product code"<<endl;
    		}
    
    		//Ask user for next product code
    		cout<<"Next product code (Q to Quit): ";
    		cin>>productCode;
    	}
    
    	//Finish report and say goodbye to user
    	fout<<"End-of-Report"<<endl;
    	cout<<"Program terminated check program5.out for results"<<endl;
    
    	return 0;
    }//main
    
    long isInInventory(string codeToFind,string code[],long count)
    {
    	//Searches array for number; return index if found, otherwise returns -1
    	long index;                             //Array index
    
    	//Loop: search positions 0 through count-1 of array codeToFind
    	for(index=0;index<count;index++)
    		//Is this the code we are looking for?
    		if(codeToFind[index]==code)
    			return code;
    
    		//Searched entire array without finding code
    		return -1;                           //Failed to find code
    }//isInInventory
    
    long readInventory(string &storeName,string code[],string description[],double unitPrice[])
    {
    	//Reads data from file into arrays
    	string x;								//Code value read from file
    	string y;								//Description value read from file
    	double z;								//UnitPrice read from file
    	long codeCount, descCount, priceCount;  //Counters for arrays
    	ifstream fin("program5.txt");           //Data file
    
    	//Initialize number of values and get values from file
    	codeCount=0;
    	descCount=0;
    	priceCount=0;
    	fin>>x>>y>>z;
    
    	//Get store name
    	getline(fin, storeName);
    
    	//Loop: continue until eof has been reached
    	while(!fin.eof())
    	{
    		//Store value read in array code and update counter
    		code[codeCount]=x;
    		codeCount++;
    
    		//Store value in array description and update counter
    		description[descCount]=y;
    		descCount++;
    
    		//Store value in array unitPrice and update counter
    		unitPrice[priceCount]=z;
    		priceCount++;
    
    		//Read next values from file
    		fin>>x>>y>>z;
    	}//While
    
    	//Return number of values read
    	return codeCount, descCount, priceCount;
    }//ReadInventory

  2. #2
    Registered User
    Join Date
    Jul 2002
    Posts
    12
    i changed some code in the functions and it compiles well, but when i try to run it i am getting link errors. i don't know what they mean. if someone could run the code and help me debug i would appreciate it. i'm starting to get frustrated. i'm probably not using any of the functions right.

    Code:
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    long isInInventory(string,string[],long);
    //  Parameters:
    //		codeToFind:	product code being searched for	
    //		code:		product codes in inventory
    //		count:		number of product codes in inventory
    //  Discussion:
    //	If a product with a code matching the parameter codeToFind is found in the array
    //	code then the index of this code is returned, otherwise -1 is returned
    
    long readInventory(string,string[],string[],double[]);
    //  Parameters:
    //		storeName:		name of store
    //		code:			product codes in inventory
    //		description:	product descriptions in inventory
    //		unitPrice:		unit prices in inventory
    //  Discussion:
    //	Reads data from inventory file into arrays above.
    //	Returns number of products actually read
    //	Data file is formatted as follows: 
    //		first line: name of the store (at most 40 characters)
    //		even-numbered lines: five-character product code followed by product description 
    //		other odd-numbered lines: unit price of product described in above line
    
    int main()
    {
        // Programmer: Mike Gozdiskowski
        // Date: 11/13/2002
        // Discussion: Allow user to search inventory of a small store.
    	// 
    	// Input:  User must enter product codes until a sentinel value is entered
    	// Output: If user enters a product code that corresponds to the inventory
    	//         all of the information for that product code will be displayed
    	//         on the screen and also printed to an output log file for future
    	//         reference. If the user enters a bad product code; they are
    	//         informed on the screen and also in the output file.
    	
    	//Declaration & description of variables
    	string storeName;						//Store name
    	string productCode;                     //Code entered by user
    	string A[30];							//Array to store values for code
    	string B[30];							//Array to store values for description
    	double C[30];							//Array to store values for price
    	long count;								//Number of positions of array A with meaningful values
    	long index;							    //Array position for array 
    
    	//Read data from file
    	count=readInventory(storeName,A,B,C);
    
        //Print heading to log file program5.out
    	ofstream fout("program5.out");
    	fout<<storeName;
    	fout<<"Product Code   Description                    Unit Price"<<endl;
    	fout<<"************   *************************      **********"<<endl;
    
    	//Welcome user to the program & ask for product code
        cout<<"Welcome to "<<storeName<<endl;
    	cout<<"Please enter a product code (Q to Quit): ";
    	cin>>productCode;
    
    	//Loop until sentinel is entered
    	while(productCode!="Q")
    	{
    		//Use a function to search the product code array for this code
    		index=isInInventory(productCode,A,count);
    
    
    		//If the product code is found print information to the log file 
    		//and the user screen otherwise print the product code and the error 
    		//message to both the log file and the user screen
    		if(index>=0)
    		{
    			cout<<"Product Code: "<<A<<endl;
    			cout<<"Description:  "<<B<<endl;
    			cout<<"Unit Price:   "<<C<<endl;
    			fout<<setw(5)<<A<<setw(25)<<B<<setw(10)<<C<<endl;
    		}
    		else
    		{
    			cout<<productCode<<"ERROR: Invalid product code"<<endl;
    			fout<<productCode<<"ERROR: Invalid product code"<<endl;
    		}
    
    		//Ask user for next product code
    		cout<<"Next product code (Q to Quit): ";
    		cin>>productCode;
    	}
    
    	//Finish report and say goodbye to user
    	fout<<"End-of-Report"<<endl;
    	cout<<"Program terminated check program5.out for results"<<endl;
    
    	return 0;
    }//main
    
    long isInInventory(string codeToFind,string code[],long count)
    {
    	//Searches array for number; return index if found, otherwise returns -1
    	long index;                             //Array index
    
    	//Loop: search positions 0 through count-1 of array codeToFind
    	for(index=0;index<count;index++)
    		//Is this the code we are looking for?
    		if(codeToFind[index]==count)
    			return count;
    
    		//Searched entire array without finding code
    		return -1;                           //Failed to find code
    }//isInInventory
    
    long readInventory(string &storeName,string code[],string description[],double unitPrice[])
    {
    	//Reads data from file into arrays
    	string x;								//Code value read from file
    	string y;								//Description value read from file
    	double z;								//UnitPrice read from file
    	long codeCount,descCount,priceCount;    //Counters for arrays
    	ifstream fin("program5.txt");           //Data file
    
    	//Initialize number of values and get values from file
    	codeCount=0;
    	descCount=0;
    	priceCount=0;
    	fin>>x>>y>>z;
    
    	//Get store name
    	getline(fin, storeName);
    
    	//Loop: continue until eof has been reached
    	while(!fin.eof())
    	{
    		//Store value read in array code and update counter
    		code[codeCount]=x;
    		codeCount++;
    
    		//Store value in array description and update counter
    		description[descCount]=y;
    		descCount++;
    
    		//Store value in array unitPrice and update counter
    		unitPrice[priceCount]=z;
    		priceCount++;
    
    		//Read next values from file
    		fin>>x>>y>>z;
    	}//While
    
    	//Return number of values read
    	return storeName,codeCount,descCount,priceCount;
    }//readInventory

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    11
    I would try to figure out the whole problem, but I'm trying to work on my own program. One thing I do see that looks a little suspect is in this function:
    Code:
    long isInInventory(string codeToFind,string code[],long count)
    {
    
       long index;
    
    	for(index=0;index<count;index++)
    		if(codeToFind[index]==count)
    			return count;
    
       return -1;
    
    }//isInInventory
    maybe if you tried:
    Code:
       position = -1;
    
       for (long index = 0; index < count; index ++)
          if(code[index] == codeToFind)
          {
             position = index;
             break;
          }
    
       return position;
    That way, you are actually comparing the code passed into the function against the codes in the array. Also, you can set the default value of a variable "index" to -1. This way, if the code is found, the variable index will be set the array position the code was found at. Using that "break;" inside the "if" statement will break the function out of the "for" loop so that it can stop searching after it finds one instance of the product code.

    If "codeToFind" is in fact the product code you are searching for, it needs to remain the same... it shouldn't be "codeToFind[index]. If "code" is in fact the array that contains all the product codes in the store, this is the array you should be moving through: "code[index]"

    If the function searches through the entire array and never finds that product code, the value of "index" will remain at -1. Maybe this will at least solve one part of your problem.

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    12
    yes it solved a part of my problem. thanks.
    i was just changing a lot of the code around just so i could get it to compile. we haven't started working on arrays yet, but our teacher gave us this program early and i wanted to try it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. binary search function on string array
    By gandolf989 in forum C Programming
    Replies: 6
    Last Post: 10-02-2007, 01:47 PM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Need some help with a C program...
    By pityocamptes in forum C Programming
    Replies: 1
    Last Post: 03-25-2005, 06:43 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM