接受数据,只要用户希望
Accepting data for as long as the user wishes
我正在做一个与超市计费相关的项目。我想知道如何接受来自用户的数据,只要他。我现在的代码如下:
#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 << "ntotalnnnnn";
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<<"ntotalnnnnn";
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;
}
}
我已经看到了你的代码,据我所知,你需要有一个do-while循环在你的主。这里有一个演示。
Do {
here goes your main code.......
Cout<<"do you want to add more data? ";
char ans;
cin>>ans;
}while(ans!=n);
现在它将运行,直到用户输入'n'。
一些好成绩的建议,因为我做了我的项目3年前:)
-
你需要制作一个菜单
-
你需要添加一些输入验证
-
你还需要添加更多的选项。我的项目有300行
对于依赖于用户输入的程序运行,您需要输入一个变量,然后使用它来控制循环。
然而,我强烈建议,如果你想要一个好成绩,你把它转换成一个菜单驱动的程序。这将自动解决这个问题。
相关文章:
- C++如何通过用户输入删除列表元素
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 如何使用用户输入在C++中正确填充2D数组
- C++MySQL C api用户输入行
- 使用用户定义函数的字符串反转
- 用户定义函数中的指针和输入
- 为什么即使使用-cudart-static进行编译,库用户仍然需要链接到cuda运行时
- 如何只允许用户输入正整数
- C++如何计算用户输入的数字中的偶数位数
- 通过for循环使用用户输入填充列表
- 这是我尝试让用户将值输入到数组中.然后将其隐藏为大量的星号
- 如何指定我希望我的LIB链接到的DLL文件?-Visual Studio 2019
- 在C++中,我如何接受不同于同一行的用户输入
- 当用户超过按钮点击限制时报告
- 如何在 c++ 中将密码和用户名保存到 .txt 文件中.如果用户尝试登录,我仍然希望能够检索它们
- 从 mfc 应用程序点击网络服务,并希望从网络服务登录方法读取用户代码返回
- 如何向用户发出多个提示,具体取决于他们希望被C++提示的次数
- 如果用户使用了我希望他们使用的值表之外的值,我如何提示他们重新输入?
- 输入并存储一个字符串,该字符串可以是用户希望的任何长度
- 接受数据,只要用户希望