Cin跳过并且不要求价值 [英] Cin skips and don't ask for value

查看:71
本文介绍了Cin跳过并且不要求价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class Order {
public:
	char customer[99];
	int numofOrd;
	int code[5];
	int quantity[5];

	void addOQ();
	bool updateOQ();
	void viewOQ();
};Order O[99];

void Order::addOQ() {
	char yn = 'y';
	cout << "Costumer Number " << orderNum;
	cout << "\nEnter customer name: ";
	cin >> O[orderNum].customer[99];
	cin.ignore('\n');
	
	for (int i = 1; tolower(yn) == 'y'; i++) {
		O[orderNum].numofOrd++;
		cout<< orderNum;
		cout << "Enter product code: ";
		cin >> O[orderNum].code[numofOrd];
			
		cout << "Enter quantity: ";
		cin >> O[orderNum].quantity[numofOrd];
		
		cout << "\nOrder added to queue\n";
		cout << "Add more?(Y|N)";
		cin >> yn;
	}
}





我的尝试:



它跳过询问代码和数量

这有什么问题吗?

当我把cin.ignore的值设为值' t存储在变量



What I have tried:

It skips asking the code and quantity
what is wrong in this?
when i put cin.ignore the value doesn't store in the variable

推荐答案

获取输入的循环具有测试yn等于'y'的条件,你用'y'初始化yn因此条件为真,循环甚至不执行一次。
The loop that gets the input has the condition to test yn equal to 'y', you initialise yn with 'y' therefore the condition is true and the loop does not execute even once.


这篇关于Cin跳过并且不要求价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆