Quantcast

Maximum PC

It is currently Tue Aug 19, 2014 9:30 pm

All times are UTC - 8 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Beginner C++ Program NeEdS HeLp!!
PostPosted: Tue Dec 07, 2004 2:36 pm 
8086
8086

Joined: Fri Oct 29, 2004 11:59 am
Posts: 2
Hey everyone,
I am having alot of trouble trying to fiqure out how to finish my program. Just the sorting part. I am not sure how i should find something that is determined by the user's input to the program. I have posted the assignment and what I have completed so far. If anyone thinks they can help please let me know ASAP! thanks!
-meg-

-------------------------------

Inventory Reporting Program (feature addition)
Due: Friday December 10th, 2004
75 Points

Modify your Inventory Reporting Program from Assignment 4 so that it allows the user to select menu options that will display the inventory list in order of either Product Description or Product Code. Each of these 2 new menu items should be performed by a function designed specifically to respond to the choices. The respective functions should be called from within the switch statement that handles your menu. Additionally, the program must meet all requirements of Assignment #4.

You must download and use a new copy of the inventory.txt file for this assignment using this link. Please note that grading for this assignment will be quite particular. Ensure that your algorithm matches code logic. Do not use c++ code in your algorithm. Your program code should be clear, understandable, and modular in design.

Below is a sample of the output for the program using the new inventory file.



Welcome to the Inventory Reporting System.
Select an activity from the menu:

1) Update inventory quantity for a product
2) Report product with highest quantity in inventory
3) Report product with lowest quantity in inventory
4) Report average inventory quantity level for all products
5) List inventory by product description.
6) List inventory by product code.
7) Exit the program

Enter Choice (1 - 7): 5

Code Description Quantity

1272 Abilify 77MG Suspension liquid temp 619
1273 Accupril 616
1274 Activella 613
1275 Actos 610
1297 Acular 742
1298 Adalat CC* 739
1299 Adderall XR 736
1300 Adderall* 733
1301 Advair Diskus 730
1302 Aerobid 727
1258 Aggrenox 724
1257 Agrylin* 721
1256 Aldactazide* 718
1255 Aldactone* 715
1254 Aldomet* 712
1238 Alesse* 664
1237 Alphagan P 661
1236 Altace 658
1235 Alupent 655
1234 Amaryl 652
1283 Anafranil* 649
1282 Anaprox* 646
1281 Androgel 643
1280 Arava 640
1266 Aricept 637
1267 Arimedex 634
1268 Artane 631
1269 Asacol 628
1270 Astelin 625
1271 Atacand 622
1253 Atacand Hct 709
1252 Atarax* 706
1251 Ativan* 703
1284 Atrovent 781
1285 Auralgan Otic 778
1286 Avalide 775
1287 Avandia 772
1288 Avapro 769
1245 Axid* 685
1244 Aygestin* 682
1243 Azelex 679
1242 Azmacort 676
1241 Azopt 673
1240 Azulfidine* 670
1239 Bentyl* 667
1293 Benzac AC* 754
1294 Benzaclin 751
1295 Benzamycin* 748
1296 Betagan 745
1276 Betapace 607
1277 Betoptic S 604
1278 Brevicon 601
1279 Bumex* 598
1265 Buspar* 595
1264 Calan SR* 592
1263 Calan* 589
1262 Canasa 586
1261 Capoten* 583
1260 Capozide* 580
1259 Carafate* 577
1250 Carbatrol 200MG 700
1249 Cardizem CD* 697
1248 Cardizem* 694
1247 Cardura* 691
1246 Catapres* 688
1289 Catapres-TTS 766
1290 Celebrex 763
1291 Celexa 760
1292 Cenestin* 757



----------------------------------
code without option 5 &6

#include <iostream>
#include <fstream>
using namespace std;

const int MAX_PRODUCTS = 200,
UPDATE = 1,
HIGHEST = 2,
LOWEST = 3,
AVERAGE = 4,
EXIT = 5;

struct inventoryRecord
{
int productNum;
char productDesc[36];
long productQTY;
};

long getInventory(inventoryRecord invList[]);
bool openInvFile (ifstream &file);
int getChoice(void);
void updateInventory(inventoryRecord invList[], long numProducts);
void findHighest_QTY(inventoryRecord invList[], long numProducts);
void findLowest_QTY(inventoryRecord invList[], long numProducts);
void average_QTY(inventoryRecord invList[], long numProducts);

int main (void)
{ inventoryRecord inventory[MAX_PRODUCTS];
long numberOfProducts = 0;
int choice;
numberOfProducts = getInventory(inventory);
do //Menu loop
{ switch (choice = getChoice())
{
case UPDATE : updateInventory(inventory, numberOfProducts);
break;
case HIGHEST: findHighest_QTY(inventory, numberOfProducts);
break;
case LOWEST : findLowest_QTY(inventory, numberOfProducts);
break;
case AVERAGE: average_QTY(inventory, numberOfProducts);
break;
case EXIT : break;
default : cout << "\nERROR: Invalid choice. Try again.";
break;
} //End of switch
}while (choice != EXIT); //End of menu loop
return 0;
}

long getInventory(inventoryRecord invList[])
{ ifstream invFile;
long productCount = 0;
while (!openInvFile(invFile))
cout << "\nERROR: File not found. Try again.";
do
{ invFile >> invList[productCount].productNum;
invFile.ignore(1);
invFile.getline(invList[productCount].productDesc, 36, '\t');
invFile >> invList[productCount].productQTY;
productCount++;
}while (!invFile.eof() && productCount < MAX_PRODUCTS);
return productCount;
}

bool openInvFile (ifstream &file)
{ char fileName[30];
cout << "\nEnter the name of the inventory file: ";
cin >> fileName;
file.open(fileName);
if (!file)
return false;
else
return true;
}

int getChoice(void)
{ int choice = 0;
cout << "\n\nInventory Menu"
<< "\n\t1)\tUpdate inventory quantity for a product"
<< "\n\t2)\tReport product with highest quantity in inventory"
<< "\n\t3)\tReport product with lowest quantity in inventory"
<< "\n\t4)\tReport average inventory quantity for all products"
<< "\n\t5)\tExit"
<< "\n\nEnter Choice: ";
cin >> choice;
return choice;
}

void updateInventory(inventoryRecord invList[], long numProducts)
{ long targetProduct = 0;
long newValue = 0;
int x = 0;
bool found = false;
do
{
x = 0;
cout << "\n\nEnter the product number to update: ";
cin >> targetProduct;
while (x < numProducts && invList[x].productNum != targetProduct)
x++;
if (x < numProducts)
found = true;
else
cout << "***ERROR: invalid product code. Try again: ";

}while (!found);
cout << "\nProduct description for product #"
<< invList[x].productNum << "\n\t" << invList[x].productDesc
<< "\n\n" << "The product quantity for product # "
<< invList[x].productNum << " is " << invList[x].productQTY
<< " units.";
cout << "\nEnter the new value: ";
cin >> newValue;
invList[x].productQTY = newValue;
cout << "\nThe new quantity for product #"
<< invList[x].productNum << " is " << invList[x].productQTY
<< " units.";
}

void findHighest_QTY(inventoryRecord invList[], long numProducts)
{
long highest = 0, prodNumber = 0;
highest = invList[0].productQTY;
for (int x = 1 ; x < numProducts ; x++)
if (invList[x].productQTY > highest)
{ highest = invList[x].productQTY;
prodNumber = invList[x].productNum;
}
cout << "\n\nThe highest quantity is " << highest
<< ".\nThe product number is " << prodNumber;
}

void findLowest_QTY(inventoryRecord invList[], long numProducts)
{
long lowest = 0, prodNumber = 0;
lowest = invList[0].productQTY;
for (int x = 1 ; x < numProducts ; x++)
if (invList[x].productQTY < lowest)
{ lowest = invList[x].productQTY;
prodNumber = invList[x].productNum;
}
cout << "\n\nThe lowest quantity is " << lowest
<< ".\nThe product number is " << prodNumber;
}

void average_QTY(inventoryRecord invList[], long numProducts)
{ long sum = 0, avg = 0;
for (int x = 0 ; x < numProducts ; x++)
sum = sum + invList[x].productQTY;
avg = sum / numProducts;
cout << "\nThe average quantity for all products is " << avg;
}


Top
  Profile  
 
 Post subject:
PostPosted: Mon Dec 20, 2004 4:33 pm 
V||V
V||V
User avatar

Joined: Thu Dec 16, 2004 3:39 pm
Posts: 180
Location: Hillsboro, OR
*bump*

-R


Top
  Profile  
 
 Post subject:
PostPosted: Mon Dec 20, 2004 5:49 pm 
Team Member Top 100
Team Member Top 100

Joined: Fri Sep 17, 2004 5:35 pm
Posts: 1176
Please use code tags.
It says you cannot use C++, so I assume you must use C...

How I would do it would be to create three arrays, each for one section of the product information, of sizes that can hold all the products.

Now, create a modified bubble sort function. Here's what I mena by this, because I am unsure if you can see my intentions:

Code:
int ListbyDesc(inventoryRecord invList[], unsigned long num_products)
{
   long num_arr[MAX_PRODUCTS];
   long qty_arr[MAX_PRODUCTS];
   char desc_arr[36][MAX_PRODUCTS];//the arrays

   long num_temp;
   long qty_temp;
   char desc_temp[36];//temp variables

   unsigned long firstloop, secondloop;//the counters.. see below

   //first, we need to copy the inventory list to these three arrays:
   for(firstloop = 0; firstloop < num_products; ++firstloop)
   {
      num_arr[firstloop] = invList[firstloop].productNum;
      qty_arr[firstloop] = invList[firstloop].productQTY;
      strcpy(desc_arr[firstloop], invList[firstloop].productDesc);
   }//copy to arrays...

   //now we buble sort.  See if you can follow along
   for(firstloop = 0; firstloop < num_products; ++firstloop)
   {
      for(secondloop = 0; secondloop < num_products-1; ++secondloop)
      {
         if( strcmp(desc_arr[secondloop], desc_arr[secondloop+1]) > 0)//if the first string is greater than second string
         {
            num_temp = num_arr[secondloop];
            qty_temp = qty_arr[secondloop];
            strcpy(desc_temp, desc_arr[secondloop]);//copy to temp

            num_arr[secondloop] = num_arr[secondloop+1];
            qty_arr[secondloop] = qty_arr[secondloop+1];
            strcpy(desc_arr[secondloop], desc_arr[secondloop+1]);//copy from second to first
            
            num_arr[secondloop+1] = num_temp;
            qty_arr[secondloop+1] = qty_temp;
            strcpy(desc_arr[secondloop+1], desc_temp);//copy from temp (first) to second
         }
      }//end of second loop
   }//end of first loop

   for(firstloop = 0; firstloop < num_products; ++firstloop)
   {
      cout << num_arr[firstloop] << " " << desc_arr[firstloop] << " " << qty_arr[firstloop] << endl;
   }//list them all

   return 0;
}


I'll explain this in pseudocode:
1.) Copy the inventory into three separate arrays that hold one member of the inventory struct. So we copy into an array of product numbers, an array of quantity numbers, and an array of descriptions.

2.) For product_num times, move anything in any array one. It sorts it by description. If one description is greater (later in the alphabet) than the next, then we will swap those. Hence, we can only move an element ONCE in the second loop. Since we loop for num_products times, we can move an object 1 x num_products times. Therefore we cna move the product from the end to the beginning of the array if we must. While we sort by description, notice that we swap all three arrays. This is so that the product and quantity numbers still correlate, whihc is required!

3. List them, after they've been sorted by description.

EDIT: Of course, if you can use C++ and the STL, then it becomes much shorter and easier.


Top
  Profile  
 
 Post subject:
PostPosted: Tue Dec 21, 2004 8:38 am 
V||V
V||V
User avatar

Joined: Thu Dec 16, 2004 3:39 pm
Posts: 180
Location: Hillsboro, OR
great post! thanks for helping!

-R


Top
  Profile  
 
 Post subject:
PostPosted: Tue Dec 21, 2004 5:13 pm 
Team Member Top 100
Team Member Top 100

Joined: Fri Sep 17, 2004 5:35 pm
Posts: 1176
Sure, any time.


Top
  Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group