-3

我正在从事与超市计费相关的项目。我想知道如何从用户那里接受数据,只要他。我当前的代码如下:

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

}

4

2 回答 2

0

一些关于好成绩的建议,就像我 3 年前做的项目一样 :)

  • 你需要做一个菜单

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

  • 您还需要添加更多选项。我的项目已经运行了约 300 行

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

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

于 2013-01-03T10:51:41.477 回答
0

我已经看过你的代码,据我了解,你需要在你的 main 中有一个 do-while 循环。这是一个演示。

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

现在它将运行直到用户输入'n'。希望它会有用。

于 2013-01-03T12:03:13.973 回答