【发布时间】: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 没有人提到使用
class或struct。他们说你的代码很丑。 -
@rahulyadav 有太多错误无法一次修复。如果你是自学,你可能想从一本更好的书重新开始。
<conio.h>和<iostream.h>等标头已过时;它们表明您的学习资源已过时。见*.com/questions/388242/… -
@rahulyadav,他们不应该这样。这些头文件都不是 C++ 标准库的一部分。它确实有
<iostream>,但conio.h是特定于 Windows 的,因此不推荐使用。如果我无法使用那么旧的工具,我会找负责软件升级的人来解决。
标签: c++ validation