Thread: code problems
View Single Post
Old 03-11-2005, 08: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 02:46 AM.
Timber is offline   Reply With Quote