Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -------Practical1.cpp-------
- #include "lib.h"
- #include "product.h"
- #include <iostream>
- using namespace std;
- int main ()//Main Program
- {
- int selection = 0;
- int productnumber=0;
- int count2 =0;
- funct function;
- do{
- cout << endl;
- cout << "Enter a menu aoption" << endl;
- cout << "1 = Add new item" << endl;
- cout << "2 = Print items" << endl;
- cout << "3 = Find a price" << endl;
- cout << "4 = Sort by ID" << endl;
- cout << "5 = Delete by ID" << endl;
- cout << "6 = Find the Lowest Price" << endl;
- cout << "7 = Exit" << endl << endl;;
- cin >> selection;
- if(selection==1)
- {
- cout << "Enter the the new ID" << endl;
- cin >> products[count2].id;
- cout << "Enter the the new price" << endl;
- cin >> products[count2].price;
- count2++;
- }
- else if(selection==2)
- {
- int count =0;
- cout <<"ID PRICE";
- cout <<"" << endl;
- while(count<count2)
- {
- cout <<products[count].id;
- cout <<" ";
- cout <<products[count].price << endl;
- count++;
- }
- }
- else if(selection==3)
- {
- int pricematch = function.GetPrice();
- }
- else if(selection==4)
- {
- cout <<"Sorting by ID" << endl;
- function.Sort(count2);
- cout <<"Sorted!" << endl;
- }
- else if(selection==5)
- {
- int selection=0;
- cout <<"Please select an Id to delete" << endl;
- cin >> selection;
- function.SetToZero(selection,count2);
- count2--;
- }
- else if(selection==6)
- {
- unsigned int lowestprice = function.FindLowest(count2);
- cout << "lowest price is" << endl;
- cout << "ID Price" << endl;
- cout << products[lowestprice].id << " " << products[lowestprice].price;
- }
- }
- while(selection!=7);
- return 0;
- }
- --------lib.cpp------
- #include <iostream>
- using namespace std;
- #include "lib.h"
- #include "product.h"
- unsigned int funct::GetPrice()
- {
- int productchoice=0;
- int tempvaule=0;
- cout << "Please enter in a ID" << endl;
- cin >> productchoice;
- cout << "your price for that item is" << endl;
- cin >> products[productchoice].price;
- tempvaule=products[productchoice].price;
- return tempvaule;
- }
- void funct::Sort(int count2)
- {
- int i,j;
- for(i=0;i<count2;i++)
- {
- for(j=0;j<i;j++)
- {
- if(products[i].id<products[j].id)
- {
- int temp=products[i].id; //swap
- int temp1=products[i].price;
- products[i].id=products[j].id;
- products[i].price=products[j].price;
- products[j].id=temp;
- products[j].price=temp1;
- }
- }
- }
- }
- void funct::SetToZero(int selection,int count2)
- {
- selection--;
- products[selection].id=0;
- products[selection].price=0;
- int i = selection;
- int j = i;
- while (i<count2)
- {
- if (products[i].id == 0)
- {
- j++;
- int temp = products[j].id;
- int temp1 = products[j].price;
- products[i].id = temp;
- products[i].price = temp1;
- products[j].id = 0;
- products[j].price = 0;
- }
- i++;
- }
- }
- unsigned int funct::FindLowest(int count2)
- {
- int i;
- int counter=0;
- unsigned int tempprice=100;
- for(i=0;i<count2;i++)
- {
- if(products[i].price<tempprice)
- {
- tempprice=i;
- }
- }
- return tempprice;
- }
- -------product.h-----------
- #pragma once
- class product//struct to hold products
- {
- public:
- int id;//ID FOR EACH PRODUCT
- unsigned int price;//price for each product
- }products[10];
- -----lib.h----
- #pragma once
- #include "product.h"
- class funct
- {
- public:
- unsigned int GetPrice();//Function to get a price from an ID
- void Sort(int count2);//Sorts by ID
- void SetToZero(int selection, int count2);//Sets price and ID to zero
- unsigned int FindLowest(int count2);//Finds the ID and price of the lowest price
- };
Advertisement
Add Comment
Please, Sign In to add comment