Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 03-11-2005, 07:17 PM   #1 (permalink)
Timber
Registered User
 
Join Date: Jan 2005
Posts: 9
Timber is on a distinguished road
code problems

I am a total newbie. I have to write a program where I have to add number items in rows and columns. Problem is i can't be adding 20 items when I run the program. How do i do it so that the number is placed randomly instead instead in order? I have already initialised to zero but it still doesn't work?

And how I do link from the menu to the Add_item function? How to link the items to the description i.e pointers? Sorry for asking so many questions...
Code:
#include <iostream.h>
#include <stdlib.h>
#include <iomanip.h>
#include <string.h>

#define array_size 20

const int SHELF = 10;
const int ROW = 2;
char name[30];
int k, j;

void getData(int arr[ROW][SHELF]);
void displayData(int arr[ROW][SHELF]);
void displaySpecificData(int arr[ROW][SHELF]); 

int main()
{
  int arr[ROW][SHELF];
  int choice;
  do
  { 
	 cout<< "1.Input items"<<endl;
	 cout<< "2.Display Specific Items"<<endl;
	 cout<< "3.Store Data"<<endl;
	 cout<< "Enter choice (1-3): "<<endl;;
	 cin>> choice;  
	 if (choice==1) 
  	   getData(arr);
  	 else if(choice==2)
	   displaySpecificData(arr);	
     	 else if(choice==3)
	   Add_item.getdesc();
  }
  while (choice !=9);
  system("PAUSE");
  return EXIT_SUCCESS;
  return 0;
}

// used to prompt user to fill array with values 
void getData(int arr[ROW][SHELF])
{
   for (int k = 0; k < ROW; k++)
     for (int j = 0; j < SHELF; j++)
     {
       cout << "Enter number of items for [ROW " << k+ 1 << "] and [SHELF " << j + 1 << "]: ";
       cin >> arr[k][j];
       displayData(arr);	
     }
}
// end getData()

// used to display array contents 
void displayData(int arr[ROW][SHELF]) 
{
    cout<< setw(12)<<"Shelf1   |  Shelf2   |  Shelf3   |  Shelf4   | Shelf5     "<<endl;
    cout << endl;
    for (int k = 0; k < ROW; k++)
    {
      for (int j = 0; j < SHELF; j++)
           cout<<setw(4)<<arr[k][j] <<"     ";
      cout<< endl;
    }
}

// end displayData()



// Display specific array contents 
void displaySpecificData(int arr[][SHELF]) //Under construction
{
    cout<< setw(12)<<"Shelf1   |  Shelf2   |  Shelf3   |  Shelf4   |  Shelf5     "<<endl;
    cout << endl;
    for (int k = 0; k < ROW; k++)
    {
      for (int j = 0; j < SHELF; ++j)
          cout<<setw(6)<<k+1<<", SHELF "<< j+1<<arr[k][j];
      cout<< endl;
    }
}

// end displayData()

 class Add_item 
{
private:
 char desc[80];  
 int qty;   
 float price;  

public:
 Add_item(): desc(), qty(0), price(0.0)
 {}
 void getdesc()  //Obtain information from user
 {
  cout << "\n\nEnter Description:  "; 
  cin.get(desc, 80);
  cout << "\nEnter quantity:  "; 
  cin >> qty;
  cout << "\nEnter price:  "; 
  cin >> price;
 }
 void showdata()  //Display information
 {
  cout <<"\n\nDescription:  "<<desc;
  cout << "\nQuantity:  "<<qty;
  cout << "\nPrice:  "<<price;
 }
 };

Last edited by redhead; 03-12-2005 at 01:46 AM.
Timber is offline   Reply With Quote
Old 03-12-2005, 08:32 AM   #2 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
What?
Can you be more clear? Try to use proper english and try to be accurate. I can't look into your mind.

Quote:
I have to add number items...
What are "number items".
Quote:
...i can't be adding 20 items when I run the program.
?
Quote:
How do i do it so that the number is placed randomly
What number?
Quote:
And how I do link from the menu to the Add_item function? How to link the items to the description i.e pointers?
What is the class for you've created to start with?

Can you explain the goal of the application, and its requirements? What am I dealing with? Etcetera.
__________________
Valmont is offline   Reply With Quote
Old 03-16-2005, 06:27 PM   #3 (permalink)
Timber
Registered User
 
Join Date: Jan 2005
Posts: 9
Timber is on a distinguished road
Hi Valmont I'm sorry for my last post. what i meant was I need this for the program:

a)to add an item to an inventory which is in a row cloumn format e.g [10][2]
b)display all contents
c)display total of everything
d)find specific item at row column
e)store name of supervisor

the problem is for I also need to provide details on the items like description and cost. But I wrote the initial program, then i realise i need to update and display the data stored in each column that contains the no. of items and a pointer to the item's details. Here's a preview on what I want to do but I am not good with objects, pointers & data structures. My previous version of my program is sadly now void
Code:
class item
{

}

item item [2];

class shelf
{
    public
    int quatity
    item*item
}


class warehouse
{
    shelf shelf [10][2];
    Add item
    &item[1]
    item=&item[0]
    shelf[r][s].quantity

void Item::Update(void)
{
char temp[200];

cout << "Enter name: ";
cin.getline(name, 30);

cout << "Enter description: ";
cin.getline(temp, 200);

int size = strlen(temp);
description = new char [size + 1];
strcpy(description, temp);

cout << "Enter cost: $";
cin >> cost;
cin.ignore();
}

void Item::Display(void)
{
cout << "Name: " << name << endl;
cout << "Description: " << description << endl;
cout << "Cost: $" << cost << endl;
system("pause");
system("cls");
}

Last edited by Timber; 03-16-2005 at 07:28 PM.
Timber is offline   Reply With Quote
Old 03-17-2005, 03:21 AM   #4 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
I'll take a peek later.
__________________
Valmont is offline   Reply With Quote
Old 03-17-2005, 06:11 AM   #5 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Ok, what is the meaning of warehouse.
Does it contain a maximum of 10 shelfs, and each shelf maximum of 2 items?
Or vice versa:
Warehouse contains a maximum of 2 shelfs, and each shelf a maximum of 10 items?

What exactly is this: shelf[10][2]

Am I on the right track here:
Code:
#include <cstdlib>
#include <string>
#include <iostream>

using namespace std;

//-------------------------------------------

struct Item
{
  Item(const string& name = "", const string& descr = "", const double cost=0) :
    sName_(name), sDescription_(descr), dCost_(cost)
  {}
  //--
  string sName_;
  string sDescription_;
  double dCost_;
};

//-------------------------------------------

class Shelf
{
public:
  Shelf() : nQuantity_(0)
  {}
  ~Shelf()
  {}
public:
  void add_item(const string name, const string descr, const double cost)
  {
    if(nQuantity_ != 9)
    {
      theItems[nQuantity_].sName_ = name;
      theItems[nQuantity_].sDescription_ = descr;
      theItems[nQuantity_].dCost_ = cost;
      ++nQuantity_;
    }  
  } 
private:
  unsigned nQuantity_;
  //10 items per shelf.
  Item theItems[10];
};

//-------------------------------------------

int main()
{
  Shelf theShelf;
  theShelf.add_item("keyboard", "Nice black Logitech", 50.00);
  theShelf.add_item("mouse", "A blue/black MX510", 49.00);  
  
  system("PAUSE");
  return 0;
}
__________________
Valmont is offline   Reply With Quote
Old 03-17-2005, 06:47 AM   #6 (permalink)
Timber
Registered User
 
Join Date: Jan 2005
Posts: 9
Timber is on a distinguished road
A warehouse will have a maximum of 10 rows and 2 shelfs. The items are placed on the shelfs. During add_items function, need to add the number of items to this shelves. I'm sorry if I misled you with my bad programming.
Timber is offline   Reply With Quote
Old 03-17-2005, 07:30 AM   #7 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
What are the rows?
Quote:
During add_items function, need to add the number of items to this shelves.
Where? Do you or your teacher realize that the add_item() method belongs in the Shelf class?
I'll explain:
Object Orient programming is programming by behaviour, not by value. That means that a Shelf should manage it's own items.
And the Warehouse should manage it's own Shelves. All the Warehouse can do is restrict the amount of Shelves and Items on a Shelf.

So an array like: Warehouse[10][2] is a bad design.

Explaining is hard isn't it? Right now, you haven't got a clue how many interpretations I could have .
__________________
Valmont is offline   Reply With Quote
Old 03-17-2005, 08:02 AM   #8 (permalink)
Timber
Registered User
 
Join Date: Jan 2005
Posts: 9
Timber is on a distinguished road
Now I'm totally lost...and helpless ...i thought in 2 dimensional arrays [10][2] or meant 10 rows and 2 columns...I guess Object oriented is different then
Timber is offline   Reply With Quote
Old 03-17-2005, 09:09 AM   #9 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
See if this comes close to what you want. Then tell me what changes need to be made:
Code:
#include <cstdlib>
#include <string>
#include <iostream>

using namespace std;

//-------------------------------------------

struct Item
{
  Item(const string& name = "", const string& descr = "", const double cost=0) :
    sName_(name), sDescription_(descr), dCost_(cost)
  {}
  //--
  string sName_;
  string sDescription_;
  double dCost_;
};

//-------------------------------------------

class Shelf
{
public:
  Shelf(const string& name = "") : 
    nItemQuantity_(0), theItem_(0), sShelfName_(name)
  {}
  ~Shelf()
  {
    if(theItem_ !=0 )
    {
      delete[] theItem_;
    }  
  }
public:
  void add_item(const string& name, const string& descr, const double cost)
  {
    increase_item_size();
    theItem_[nItemQuantity_-1].sName_ = name;
    theItem_[nItemQuantity_-1].sDescription_ = descr;
    theItem_[nItemQuantity_-1].dCost_ = cost;
  }
  unsigned get_quantity() const
  {
    return nItemQuantity_;
  }
  void set_shelf_name(const string& name)
  {
    sShelfName_ = name;
  }
  string get_shelf_name() const
  {
    return sShelfName_;
  }  
  Item* get_item() const
  {
    return theItem_;
  }  
private:
  string sShelfName_;
  unsigned nItemQuantity_;
  Item* theItem_;
private:
  void increase_item_size()
  {
    Item* itm = new Item[nItemQuantity_+1];
    for(unsigned i = 0; i < nItemQuantity_ ; ++i)
    {
      itm[i] = theItem_[i];
    }
    delete[] theItem_;
    theItem_ = itm;
    ++nItemQuantity_;
  }  
};

//-------------------------------------------

class Warehouse
{
public: 
  Warehouse() : theShelf_(0), nSize_(0)
  {}
  ~Warehouse()
  {
    if(theShelf_ != 0)
    {
      delete[] theShelf_;
    }  
  }
public:
  void add_shelf(const string& shname,  const string& itmname,  const string& itmdescr,
    const double itcost)
  {
    increase_shelf_size();
    theShelf_[nSize_-1].set_shelf_name(shname);
    theShelf_[nSize_-1].add_item(itmname, itmdescr, itcost);
  }
  Shelf* get_shelf() const
  {
    return theShelf_;
  }
  unsigned get_size() const
  {
    return nSize_;
  }  
private:
  unsigned nSize_;
  Shelf* theShelf_;
private:
  void increase_shelf_size()
  {
    Shelf* sh = new Shelf[nSize_+1];
    for(unsigned i = 0; i < nSize_ ; ++i)
    {
      sh[i] = theShelf_[i];
    }
    delete[] theShelf_;
    theShelf_ = sh;
    ++nSize_;
  }  
};

//-------------------------------------------

int main()
{
  Warehouse theWarehouse;
  theWarehouse.add_shelf("Computers", "Mouse", "Logitech", 50.00);
  
  cout<<"*** "<<theWarehouse.get_shelf()[0].get_shelf_name()<<" ***"<<endl;
  cout<<theWarehouse.get_shelf()[0].get_item()[0].sName_<<endl;
  cout<<theWarehouse.get_shelf()[0].get_item()[0].sDescription_<<endl;
  cout<<theWarehouse.get_shelf()[0].get_item()[0].dCost_<<endl;
    
  system("PAUSE");
  return 0;
}
__________________
Valmont is offline   Reply With Quote
Old 03-21-2005, 03:59 AM   #10 (permalink)
Timber
Registered User
 
Join Date: Jan 2005
Posts: 9
Timber is on a distinguished road
hi Valmont,

Is there any way I can input the manager name or address or input an item instead of having it hard coded in the program and why do I get the number 3435973838 in
the output? Will be eternally grateful to you for your help.Thanks
Timber is offline   Reply With Quote
Old 03-21-2005, 06:44 AM   #11 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
I'll check later.
__________________
Valmont is offline   Reply With Quote
Old 03-21-2005, 06:55 AM   #12 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Below is the updated code. Copy it exactly.
To use a menu for user input, you'll need to make it yourself. Currently the main() is only demonstrating the usage of the classes. It is still crappy, but because you didn't learn more features this will need to do.

For example, I don't free up memory and such.

Code:
#include <cstdlib>
#include <string>
#include <iostream>

using namespace std;

//-------------------------------------------

struct Item
{
  string _sName;
  string _sDescription;
  double _dCost;
};

//-------------------------------------------

class Shelf
{
//This needs to be called first by any user of this class.
public:
  //Since we didn't learn constructors/destructos yet, we will use this.
  void init()
  {
    _nItemQuantity = 0;
    //Let's make sure the shelf can hold at least one item.
    _theItem = new Item[1];    
  }  
public:
  void add_item(const string& name, const string& descr, const double cost)
  {
    _theItem[_nItemQuantity]._sName = name;
    _theItem[_nItemQuantity]._sDescription = descr;
    _theItem[_nItemQuantity]._dCost = cost;
    ++_nItemQuantity;
    
    //Let's increase the Shelf so it can hold a next item if needed.
    Item* temp = new Item[_nItemQuantity+1];
    for(unsigned i = 0; i < _nItemQuantity; ++i)
    {
      temp[i] = _theItem[i];
    }
    delete[] _theItem;
    _theItem = temp;
     
  }
  unsigned get_quantity() const
  {
    return _nItemQuantity;
  }
  
  Item get_item(unsigned shelf)
  {
    return _theItem[shelf-1];
  }  
private:
  string _sShelfName;
  unsigned _nItemQuantity;
  Item* _theItem;
};

//-------------------------------------------

class ShelfRow
{
//Accessors only.
public:
  Shelf* get_shelf()
  {
    return _theShelf;
  }
  unsigned max_index() const
  {
    //Max 2 items, so a max array index of 1. Arrays in C++ start with 0!
    return 1;
  }  
private:
  //Each ShelfRow contains 2 Shelfs.
  Shelf _theShelf[2];
};

//-------------------------------------------

class Warehouse
{
public:
  void init()
  {
    ShelfRow SR;
    const unsigned maxrows = SR.max_index();
    for(unsigned i = 0; i < 10; ++i)
    {
      for(unsigned j = 0; j < maxrows+1; ++j)
      {
        _theShelfRow[i].get_shelf()[j].init();
      }  
    }  
  }  
  
//Specific (controlling/managing) warehouse behaviour.
public:
  bool add_item(unsigned shelfrow, unsigned shelf, const string& itmname, 
  const string& itmdescr, const double cost)
  {
    
    if(bonds_ok(shelfrow, shelf) == true)
    {
      _theShelfRow[shelfrow-1].get_shelf()[shelf-1].add_item(itmname, itmdescr, cost);
      return true;
    }
    return false;
  }
  
  unsigned row_shelf_item_count( unsigned row, unsigned shelf) 
  {
    if(bonds_ok(row, shelf) == true)
    {
      return _theShelfRow[row-1].get_shelf()[shelf-1].get_quantity();
    }  
  }
  
  void print_row_shelf_items(unsigned row, unsigned shelf)
  {
    unsigned max = _theShelfRow[row-1].get_shelf()[shelf-1].get_quantity();
    for(unsigned i = 1; i < max+1; ++i)
    {
      cout<<_theShelfRow[row-1].get_shelf()[shelf-1].get_item(i)._sName<<endl;
      cout<<_theShelfRow[row-1].get_shelf()[shelf-1].get_item(i)._sDescription<<endl;
      cout<<_theShelfRow[row-1].get_shelf()[shelf-1].get_item(i)._dCost<<endl;
      cout<<"**************"<<endl;
    }  
  }  

//Accessor functions only.
public:
  void set_manager_name(const string& name)
  {
    _sManagerName = name;
  }
  void set_manager_address(const string& addr)
  {
    _sManagerAddress = addr;
  }  
  void set_warehouse_name(const string& name)
  {
    _sWarehouseName =  name;
  }
  string get_manager_name() const
  {
    return _sManagerName;
  }  
  string get_manager_address() const
  {
    return _sManagerAddress;
  }  
  string get_warehouse_name() const
  {
    return _sWarehouseName;
  }

private:
  //Each Warehouse contains 10 ShelfRows.
  ShelfRow _theShelfRow[10];
  string _sWarehouseName;
  string _sManagerName;
  string _sManagerAddress;
  
//Various helper method(s).
private:
  bool bonds_ok(unsigned row, unsigned shelf) const
  {
    if(row > 10)
    {
      return false;
    }
    if( shelf-1 > _theShelfRow[shelf-1].max_index() )
    {
      return false;
    }    
    //All O.K.
    return true;  
  }  
};

//-------------------------------------------

int main()
{
  Warehouse theWarehouse;
  theWarehouse.init();
  
  theWarehouse.set_manager_name("Valmont");
  theWarehouse.set_manager_address("Baker Street 5");
  theWarehouse.set_warehouse_name("Woodland Industrial Park 2");
  
  //Place an item in shelfrow 1, on shelf 2.
  //add_item() will check if that is possible.
  theWarehouse.add_item(1, 2, "Keyboard", "Logitech Media", 50.00);
  theWarehouse.add_item(1, 2, "Mouse", "Logitech MX510", 49.99);
  theWarehouse.add_item(1, 2, "Mouse Pad", "The Simpsons Model", 5.1);
  
   //Find the number of items stored at a specified row and shelf.
   cout<<"Total Items: "<<theWarehouse.row_shelf_item_count(1, 2)<<endl<<endl;
   
  theWarehouse.print_row_shelf_items(1,2);
   
  theWarehouse.add_item(5, 1, "banana", "Nice and yellow", 1);
  theWarehouse.add_item(5, 1, "tree", "A big one", 800.01);
  theWarehouse.add_item(5, 1, "house ", "Wrecked by the Simpsons", 200);
  
  cout<<endl<<"-----------On row 5, shelf 1:"<<endl<<endl;
  theWarehouse.print_row_shelf_items(5,1);
   
      
  system("PAUSE");
  return 0;
}
__________________

Last edited by Valmont; 03-21-2005 at 08:36 AM.
Valmont is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
{perl} HTTP Error Code Response gty All Other Coding Languages 2 09-08-2004 11:09 AM
Cisco Code breaking sde Code Newbie News 0 05-21-2004 07:10 AM
Microsoft probes Windows code leak redhead Code Newbie News 0 02-13-2004 12:41 AM
Code Section? rdove Feedback 5 02-18-2003 10:04 AM


All times are GMT -8. The time now is 02:49 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting