只要用户愿意接受数据

时间:2013-01-03 10:33:38

标签: c++

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

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

}

2 个答案:

答案 0 :(得分:0)

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

  • 您需要制作菜单

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

  • 您还需要添加更多选项。我的项目遇到了~300行

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

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

答案 1 :(得分:0)

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

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

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