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.