私はスーパーマーケットの請求に関連するプロジェクトに取り組んでいます。ユーザーがいる限り、ユーザーからデータを受け入れる方法を知りたいです。私の現在のコードは次のとおりです。
#include < stdio.h >
#include < iostream.h >
#include < conio.h >
class product //start of class
{
int itemno;
char name[100];
char itemtype[50];
float price;
float quantity;
float total;
public:
void addprod();
void calculate();
void accept();
void display();
}; //end of class
void product::addprod() //starting of addproduct()
{
cout << "enter the name of the poduct:";
gets(name);
cout << "enter its type:";
gets(itemtype);
cout << "enter its price:";
cin >> price;
} //end of addproduct()
void product::accept() //starting of accept()
{
cout << "enter the item name:";
gets(name);
cout << "enter the quantity:";
cin >> quantity;
}
void product::calculate() {
total = price * quantity;
}
void product::display() {
cout << "\nName";
cout << name;
cout << "\nPrice";
cout << price;
cout << "\nquantity";
cout << quantity;
cout << "\ntotal\n\n\n\n\n";
cout << total;
}
void main() {
product s1[3];
for (int i = 0; i < 3; i++) {
s1[i].addprod();
}
for (i = 0; i < 3; i++) {
s1[i].accept();
}
for (i = 0; i < 3; i++) {
s1[i].calculate();
}
for (i = 0; i < 3; i++) {
s1[i].display();
}
}
私main()
はすべてを3回受け入れますが、ユーザーが望む限りすべてを選択したいと思います。どうすればこれを達成できますか?
これを確認してください......
#include<stdio.h>
#include<iostream.h>
#include<conio.h>
int i;
class product //start of class
{
int itemno;
char name[100];
char itemtype[50];
float price;
float quantity;
float total;
public:
void addprod() ;
void calculate();
void accept();
void display() ;
} ; //end of class
void product::addprod() //starting of addproduct()
{
cout<<"enter the name of the poduct:";
gets(name) ;
cout<<"enter its type:";
gets(itemtype);
cout<<"enter its price:";
cin>>price;
} //end of addproduct()
void product::accept() //starting of accept()
{
cout<<"enter the item name:";
gets(name) ;
cout<<"enter the quantity:";
cin>>quantity;
}
void product::calculate()
{
total=price*quantity;
}
void product::display()
{
cout<<"\nName";
cout<<name;
cout<<"\nPrice";
cout<<price ;
cout<<"\nquantity";
cout<<quantity;
cout<<"\ntotal\n\n\n\n\n";
cout<<total;
}
void main()
{
int ch;
product s1[3];
cout<<"\n 1. Add product";
cout<<"\n 2. Make Bill";
cout<<"\n 3. Display Bill";
cout<<"\n 0. Exit";
cout<<"\n Enter your choise(1,2,3,9)" ;
cin>>ch;
switch(ch)
{
case 1: cout<<"\n press 0 to exit";
for(i=1;i!=0;i++)
s1[i].addprod();
break;
}
}