接受数据,只要用户希望

Accepting data for as long as the user wishes

本文关键字:用户 希望 数据      更新时间:2023-10-16

我正在做一个与超市计费相关的项目。我想知道如何接受来自用户的数据,只要他。我现在的代码如下:

#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 << "ntotalnnnnn";
    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<<"ntotalnnnnn";
                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;
            }

}

我已经看到了你的代码,据我所知,你需要有一个do-while循环在你的主。这里有一个演示。

Do {  
  here goes your main code....... 
  Cout<<"do you want to add more data? "; 
  char ans;
  cin>>ans; 
}while(ans!=n);

现在它将运行,直到用户输入'n'。

一些好成绩的建议,因为我做了我的项目3年前:)

  • 你需要制作一个菜单

  • 你需要添加一些输入验证

  • 你还需要添加更多的选项。我的项目有300行

对于依赖于用户输入的程序运行,您需要输入一个变量,然后使用它来控制循环。

然而,我强烈建议,如果你想要一个好成绩,你把它转换成一个菜单驱动的程序。这将自动解决这个问题。