Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
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;
	}
}


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
Posted
Updated 19-Mar-17 22:14pm

1 solution

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.
 
Share this answer
 
Comments
Aeroneeee 20-Mar-17 4:31am    
If I do this

char yn='y';
while(tolower(yn)=='y'){
O[orderNum].numofOrd++;

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

the loop dont stop. I think the problem is in the cin?
It don't ask the user to enter value.
Michael_Davies 20-Mar-17 4:39am    
Sorry, the loop should stop when you enter any character except y, are you saying it does not stop if you enter say an n?

If you want a loop to execute at least once use Do... While, it will test the condition after the code has executed rather than before.
Aeroneeee 20-Mar-17 4:52am    
I tried that too but it won't prompt the user to enter any value.
But it prints the Enter code and Enter quantity.
It means that the loop runs ok.
Michael_Davies 20-Mar-17 4:59am    
Does the cout for Add More not show, please be clear.

Pause the program before exit and copy the console text of a session and paste it here so we can see what is happening.
Aeroneeee 20-Mar-17 5:04am    
void Order::addOQ() {
char yn = 'y';
cout << "Costumer Number " << orderNum;
cout << "\nEnter customer name: ";
cin >> O[orderNum].customer[99];

while(tolower(yn)=='y'){
O[orderNum].numofOrd++;

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;

system("pause");
}
}


The output is this

Costumer Number 1
Enter customer name: Finn
Enter product code: Enter quantity:
Order added to queue
Add more?(Y|N)Press any key to continue . . .
Enter product code: Enter quantity:
Order added to queue
Add more?(Y|N)Press any key to continue . . .
Enter product code: Enter quantity:
Order added to queue
Add more?(Y|N)Press any key to continue . . .

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900