Hi ive made a program where it displays 10 products from a website. The customer then chooses a certain amount of those products. When the customer is finished choosing, the program will display all products the customer has chosen. Here is the full program. it works.

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

class product
{
      private:
      public:
      int model;
      string item;
      int price;
      void show();
      void title();
      };

class order
{     
      private:
      int a;
      int r;
      public:
      int mod;
      string ite;
      int cost;
      void add(product all[],order alld[],int count);
      };

void product::title()
{
    cout<<"Model Number...Product Name...Price"<<endl;
}

void product::show()
{
  cout
  <<model<<" "
  <<item<<"..."
  <<"$"<<price<<"\n";
}

void order::add(product all[],order order[], int count){
   cout<<"\nPlease choose your items: "<<endl;
   cout<<"Choose your product via its model number:"<<endl;
   cout<<"Input 11 to confirm order."<<endl;
   r=0;
   for(int c=0; c<10; c++){
   cin>>a;
   if(a>9)
   break;
   order[0].mod=all[a].model;
   order[0].ite=all[a].item;
   order[0].cost=all[a].price;
   r++
  }
  cout<<"You have chosen: "<<endl;
  for(int c=0; c<r; c++){
  cout<<order[0].mod<<" "<<endl;
  cout<<order[0].ite<<" "<<endl;
  cout<<order[0].cost<<" "<<endl;
}
}


int main(){
    product prod;
    order ord;
    int count=0;
    product arrayp[10];
    order arrayo[10];
    ifstream infile;
    infile.open("C:\\products.dat");
    while(infile.peek()!=EOF){
       infile
       >>arrayp[count].model
       >>arrayp[count].item
       >>arrayp[count].price;
    infile.ignore(numeric_limits<int>::max(),'\n');
    count++;
    }
    infile.close();
    prod.title();
    for(int i=0; i<10; i++){
    arrayp[i].show();
    }

    ord.add(arrayp,arrayo,count);
    system("pause");
    return 0;    
}

The problem here is, i dont know how to change the code to allow the customer to select multiple products to be displayed. Right now the customer may only choose one product. The problem code is here:

void order::add(product all[],order order[], int count){
   cout<<"\nPlease choose your items: "<<endl;
   cout<<"Choose your product via its model number:"<<endl;
   cout<<"Input 11 to confirm order."<<endl;
   r=0;
   for(int c=0; c<10; c++){
   cin>>a;
   if(a>9)
   break;
   order[r].mod=all[a].model;
   order[r].ite=all[a].item;
   order[r].cost=all[a].price;
   r++;
  }
  cout<<"You have chosen: "<<endl;
  for(int c=0; c<r; c++){
  cout<<order[r].mod<<" "<<endl;
  cout<<order[r].ite<<" "<<endl;
  cout<<order[r].cost<<" "<<endl;
}
}

i attempted to use r but when i run it. random numbers pop up in place of items themself. I simply do not know how to change the code, so that the customer can 1) select more one item, 2) finish and display all items(more than 1).

as of right now the code can only display 1 choice.
here is the txt file for products:

0 Book 20
1 Pencil 5
2 Pen 10
3 Paper 2
4 Eraser 6
5 Scissors 4
6 CD 1
7 USB 3
8 Textbook 50
9 Ruler 15

Any help is Greatly appreciated.

Recommended Answers

All 4 Replies

Instead of having your order class look a lot like an individual product (but with different names for the members), have it encapsulate the array of desired product instances selected by the user. Using the same idea, you could wrap your arrayp from main() in a catalog class that encapsulates the number of different products and an array of unique product instances.

im sorry i dont quite understand what you mean? could you re-explain or provide an example?

Currently, your order class has mod, ite & cost members. These are attributes of a product, not of an order. An order, conceptually, consists of a set of desired products. You have a method-function called add(), but it takes and/or returns outside arrays of products and orders. What does an array of orders even mean? Rethink your 'order' class as set of products and functions which allow you to operate on that set. (The items that the customer wishes to purchase should be an array of products, which should be a member of the 'order' class.

Does this help? If not, I'll try to remember to check on you again tomorrow! :)

yeh it does. so basically i have been working incorrectly with my class. thanks ill rewokr the classes.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.