【问题标题】:Validation in a C++ Program [closed]C ++程序中的验证[关闭]
【发布时间】:2012-12-19 14:08:44
【问题描述】:

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

#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)那么我应该怎么做才能让他知道它的输入错误并要求用户输入正确的形式

【问题讨论】:

  • 首先对main使用符合要求的签名,而不是使用goto,并使代码格式可读。
  • 请给出具体问题。 SO 不是一个“扔掉你的作业然后跑”的网站。
  • @rahulyadav 没有人提到使用classstruct。他们说你的代码很丑。
  • @rahulyadav 有太多错误无法一次修复。如果你是自学,你可能想从一本更好的书重新开始。 &lt;conio.h&gt;&lt;iostream.h&gt; 等标头已过时;它们表明您的学习资源已过时。见*.com/questions/388242/…
  • @rahulyadav,他们不应该这样。这些头文件都不是 C++ 标准库的一部分。它确实有 &lt;iostream&gt;,但 conio.h 是特定于 Windows 的,因此不推荐使用。如果我无法使用那么旧的工具,我会找负责软件升级的人来解决。

标签: c++ validation


【解决方案1】:

您可以使用 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;
}

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

【讨论】:

  • cin.clear() 使用哪个头文件
  • 它是 cin 的成员,所以显然它是在 cin(或 basic_istream 实际上是) 被声明。只需像您所做的那样包含 iostream 或 iostream.h。
最近更新 更多