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