We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,659 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Adding products to an order(adding items to array)

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.

2
Contributors
4
Replies
5 Hours
Discussion Span
1 Year Ago
Last Updated
5
Views
Question
Answered
BryantFury
Light Poster
45 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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.

raptr_dflo
Practically a Master Poster
605 posts since Aug 2010
Reputation Points: 76
Solved Threads: 83
Skill Endorsements: 1

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

BryantFury
Light Poster
45 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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! :)

raptr_dflo
Practically a Master Poster
605 posts since Aug 2010
Reputation Points: 76
Solved Threads: 83
Skill Endorsements: 1

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

BryantFury
Light Poster
45 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 1 Year Ago by raptr_dflo

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.1073 seconds using 2.73MB