-5

首先 thnx 试图帮助我.....这是我程序的一部分......我想知道如何在 main() 中输入数据时进行验证......thnx

#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 serial number";
            cin>>itemno;

            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];

         a:

         cout<<"\n      1.      Add product one by one";
         cout<<"\n     2.      Add products in bulk";
         cout<<"\n     3.      Make Bill";
         cout<<"\n     4.      Display Bill";
         cout<<"\n     0.      Exit";
         cout<<"\n     Enter your choise(1,2,3,9)"     ;
         cin>>ch;


         switch(ch)
         {

         **case 1:          cout<<"\n press n to exit\n\n";
                                char con='y';
                                while(con=='y')
                                {
                                s1[i].addprod();
                                i++;
                                cout<<"do u wanna continue(y/n)";
                                cin>>con;
                                        if(con=='n')
                                        {
                                        goto a;
                             }
                                }
                                break;
            }**

这是我的学校项目,所以需要尽快帮助。就像如果一个人输入一个字符(a,b,c)那么我应该怎么做才能让他意识到它的输入错误并要求用户输入正确的形式

4

1 回答 1

-2

您可以使用if测试输入是否成功:

if (cin >> ch)
    ...

要让用户再次输入输入,您需要一个循环,并且您还需要调用cin.clear()来恢复流的状态:

cout << "\n     Enter your choice(1,2,3,9)":

cin >> ch;
while (!cin)
{
    cout << "Invalid input. Please try again." << endl;

    cin.clear();
    cin >> ch;
}

这就是您处理main中第一个输入项的方式。你可以为其他人做类似的事情。

于 2013-01-05T05:55:26.420 回答