Thread: My program is not doing what it's supposed to do, help!!

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    15

    Angry My program is not doing what it's supposed to do, help!!

    Hello,

    I'm simply frustrated as crap because I have absolutely no idea why my program I'm writing for a college assignment is doing what it's doing. I am supposed to open up a data file (in this case filea.txt, which contents are at the bottom) and be able to manipulate with it. But when I try to access an item with a catalog number that was streamed in from the data file, it always returns the error message I have placed that says the item couldn't be found (ie when the function returns -1). Here, have a look at my program for yourself. And yes, I do know that void main() is evil. lol

    Code:
    //	Include standard C++ header file
    #include <iostream.h>
    
    //	Include file streaming header file
    #include <fstream.h>
    
    //	Include C++ string header file
    #include <cstring.h>
    
    //	Begin a sequential search algorithm for integer types
    int seqSearch(long arr[], int max, long key)
    {
    	int i;	//  Indexing variable
    
    	for (i = 0; i < max; i++)
       {
    		if (key==arr[i])
          {
    			return (i);
          }
       }
    
    	// If not found, return a negative 1.
    	return (-1);
    }
    
    //	Begin program with main statement of type void
    void main()
    {
    	//	Declare 7 parralel arrays, with vegetable type and variety being string types, and price
    	//	per package being of type double
    	long catalogNumber[50];
    	string vegType[50];
    	string variety[50];
    	double pricePackage[50];
    	int binNumber[50];
    	int packagesHand[50];
    	int restockPoint[50];
    
    	int i;	//	Indexing variable
    	int ind2;	//	Indexing variable
    	long key1; //	Long type key
    
    	//	Declare an ifstream object 
    	ifstream fin;
    
    	//	Open a file for streaming
    	fin.open("C:\\C++\\filea.txt");
    	if (!fin)
    	{
    		cout << "Unable to open file." << endl;
    		exit(1);
    	}
    
    	//	Declare an indexing variable count
    	int count = 0;
    
    	//	Begin streaming in information from file
    	fin >> catalogNumber[count] >> vegType[count] >> variety[count] >> pricePackage[count]
    		>> binNumber[count] >> packagesHand[count] >> restockPoint[count];
    
    	//	Keep streaming in until EOF
    	while (fin)
    	{
    		fin >> catalogNumber[count] >> vegType[count] >> variety[count] >> pricePackage[count]
    		>> binNumber[count] >> packagesHand[count] >> restockPoint[count];
    		count++;
    
    	}
    
    	int menu; // Variable to hold user's selection in the menu.
    
    	// Do the following steps
    	do
    	{
    	//	Introduce user to program and present menu
    	cout << "// The Kentucky Seed Catalog Company //" << endl;
    	cout << "Welcome to TKSCC inventory application.  Here you can find up to 50 products." << endl;
    	cout << endl;
    	cout << "Menu of options: " << endl;
    	cout << "1. Change the price of an item." << endl;
    	cout << "2. Print the price of an item." << endl;
    	cout << "3. Add a new item." << endl;
    	cout << "4. Delete an item." << endl;
    	cout << "5. Exit." << endl;
    	cout << "Please enter a selection -- ";
    	cin >> menu;
    	cout << endl;
    
    
    	//	Do a switch flow statement for menu
    	switch(menu)
    	{
    		case 1:		cout << "** CHANGE PRICE OF ITEM **" << endl;
    					cout << "Please enter an item by catalog number -- ";	// Ask user which number item they want to change the price for
    					cin >> key1;
    					i = seqSearch(catalogNumber, count, key1);	// Search by pricePackage array if there is an item in that inventory number
    					if (i == -1)
    						cout << "ERR: Item not found" << endl << endl;
    					else
    					{
    						cout << "You have chosen the following item:" << endl;	// Show user which item was selected
    						cout << "Vegetable Type -- " << vegType[i] << endl;
    						cout << "Variety -- " << variety[i] << endl;
    						cout << "Current Price -- " << pricePackage[i] << endl;
    						cout << "What would you like to set the new price to? --";
    						cin >> pricePackage[i];	// Input the new price point
    					}
    					break;		// Break out of the switch
    
    		case 2:		cout << "** PRINT PRICE OF ITEM **" << endl;
    					cout << "Please enter an item catalog number for the item you want to see displayed--";	// Ask user which catalog number they want to see the item for
    					cin >> key1;
    					i = seqSearch(catalogNumber, count, key1);
    					if (i == -1)
    						cout << "ERR: Item not found" << endl << endl;
    					else
    					{
    						cout << "You have chosen the following item:" << endl;	// Show user which item was selected
    						cout << "Vegetable Type -- " << vegType[i] << endl;
    						cout << "Variety -- " << variety[i] << endl;
    						cout << "Current Price -- " << pricePackage[i] << endl;	// Show current price
    					}
    					break;	// Break out of the switch
    
    		case 3:		cout << "** ADD A NEW ITEM **" << endl;
    					i = count + 1;	// Upgrade count information into i
    					cout << "Please enter the following to add a new item:" << endl;
    					cout << "Catalog number for the new item -- ";
    					cin >> catalogNumber[i];
    					cout << "Vegetable Type -- ";
    					cin >> vegType[i];
    					cout << "Variety -- ";
    					cin >> variety[i];
    					cout << "Price Per Package -- ";
    					cin >> pricePackage[i];
    					cout << "Bin Number -- ";
    					cin >> binNumber[i];
    					cout << "Packages On Hand -- ";
    					cin >> packagesHand[i];
    					cout << "Restock Point -- ";
    					cin >> restockPoint[i];
    					count++; //	Increment count
    					break;
    					
    		case 4:     cout << "** DELETE AN ITEM **" << endl;
    					cout << "Please enter an item by catalog number -- ";
    					cin >> key1;
    					i = seqSearch(catalogNumber, count, key1);
    					if (i == -1)
    						cout << "ERR:  Item not found" << endl << endl;
    					else
    					{
    						cout << "You have chosen to delete the following item:" << endl; // Show user which item was selected
    						cout << "Vegetable Type -- " << vegType[i] << endl;
    						cout << "Variety -- " << variety[i] << endl;
    						cout << "Current Price -- " << pricePackage[i] << endl;
    					}
    
    					for (ind2 = i + 1; ind2 < count; ind2++)		//  Move all items one space to left
    					{
    						catalogNumber[ind2 - 1] = catalogNumber[ind2];
    						vegType[ind2 - 1] = vegType[ind2];
    						variety[ind2 - 1] = variety[ind2];
    						pricePackage[ind2 - 1] = pricePackage[ind2];
    						binNumber[ind2 - 1] = binNumber[ind2];
    						packagesHand[ind2 - 1] = packagesHand[ind2];
    						restockPoint[ind2 - 1] = restockPoint[ind2];	
    					}
    
    					cout << endl;
    					cout << "Item deleted." << endl;
    
    					count--;
    
    					break;
    
    					}
    
    	}
    	while (menu != 5);
    
    	fin.close();
    
    }
    Here is the contents of filea.txt

    Code:
    50473
    pole_beans
    goldmarie
    2.25
    5
    4
    3
    50322
    pole_beans
    kentucky_blue
    1.95
    2
    1
    1
    51398
    pole_beans
    landfrauen
    1.95
    5
    1
    1
    50295
    pole_beans
    kentucky_wonder
    1.15
    4
    2
    2
    50271
    pole_beans
    blue_lake
    1.15
    10
    3
    3
    55625
    cabbage
    darkri_hybrid
    1.45
    3
    4
    4
    50788
    cabbage
    golden_cross_hybrid
    1.45
    10
    5
    5
    57287
    cabbage
    green_jewel_hybrid
    1.35
    5
    6
    6
    57453
    cabbage
    red_rookie
    1.65
    2
    7
    7
    57299
    cabbage
    tropic_giant
    1.75
    1
    1
    1
    55752
    savoy_cabbage
    spivoy_hybrid
    1.65
    5
    3
    3
    50699
    savoy_cabbage
    savoy_king
    1.50
    Thank you for your help

  2. #2
    Registered User
    Join Date
    Sep 2002
    Posts
    15
    Fixed the problem, now I discover my add a new item ability of the program doesnt seem to be working... any ideas?

    Thank you

  3. #3
    Add some DEBUG statements to see whats happening

    Code:
    //	Begin a sequential search algorithm for integer types
    int seqSearch(long arr[], int max, long key)
    {
    	int i;	//  Indexing variable
    
    	for (i = 0; i < max; i++)
       {
                std::cout<<"entering for loop of seqSearch"<<endl;
    		if (key==arr[i])
          {
               std::cout<<"entering if statment in for loop"<<endl;
    
    			return (i);
          }
       }
               std::cout<<"Not found, returning -1"<<endl;
    
    	// If not found, return a negative 1.
    	return (-1);
    }
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    15
    Thank you for your help.

    I'm now having trouble with the adding item function. It seems to add in but I can't access it with the change price or print price abilities of the program, just says its not found.

    Any ideas?

    Thank you

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    15
    okay now its getting strange

    I just inputted two items with catalog number 28533, and gave different attributes to each

    the first time I input the item 28533, it err's when i try to access it with other options

    but when i put in another 28533, the program successfully finds the first 28533 item.

    whats going on here?

  6. #6
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    So what's the prize for sorting through pages and pages of code for a logic error? I suggest you a) Break your code up into logical functions/objects. b) Comment something. c) Fix the indenting, it's all over the place (unless it's just the boards).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  2. can't get this program to run correctly
    By Amyaayaa in forum C++ Programming
    Replies: 3
    Last Post: 02-05-2008, 04:16 PM
  3. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  4. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM