I have problems for calling a function. I don't quite understand why it couldn't work

1
2
3
4
5
6
7
8
9
void Order::Input() {
	Product::Input();
	cout << "So luong: ";// quantity
	cin >> quantity_;
	cout << endl;
	cout << "Gia ban: ";//sell price per product
	cin >> sell_price_;
	cout << endl;
}

1
2
3
4
5
6
7
void Order::GenerateOrders(Order orders[], int n) {
	for (int i = 0; i < n; i++)
	{
		cout << "\nNhap thong tin hoa don: " << i + 1 << endl;
		Order Input(orders[i]);// problem here!
	}
}

Last edited on
You made a contract your Order::Input function has no parameters.

Yet you called it with one.
#Furry Guy
1
2
3
4
5
6
7
void Order::GenerateOrders(Order orders[], int n) {
	for (int i = 0; i < n; i++)
	{
		cout << "\nNhap thong tin hoa don: " << i + 1 << endl;
		Order Input();
	}
}


Is it like this? If that is correct, it should continue doing the other line. But the output is just stop at the line 4 in Order:: GenerateOrders

Here is my product.h
1
2
3
4
5
void Product::Input() {
	GetName();// here it said that avoid unnamed objects with custom construction and destruction
	GetProductionYear();
	GetOriginalPrice();
}


1
2
3
4
5
6
string Product::GetName() {
	string ProductName;
	cout << "Ten San Pham: ";
	cin >> ProductName;
	return ProductName;
};

1
2
3
4
5
6
int Product::GetProductionYear() {
	int ProductionYear;
	cout << "Nam san xuat: ";
	cin >> ProductionYear;
	return ProductionYear;
}

1
2
3
4
5
6
long Product::GetOriginalPrice() {
	long OriginalPrice;
	cout << "Gia goc san pham: ";
	cin >> OriginalPrice;
	return OriginalPrice;
}
Last edited on
On line 5 in GenerateOrders(...) you create a new object of type Order. You do not call the Input() function.

I would guess you want something like this:
1
2
3
4
5
6
7
void Order::GenerateOrders(Order orders[], int n) {
	for (int i = 0; i < n; i++)
	{
		cout << "\nNhap thong tin hoa don: " << i + 1 << endl;
		orders[i].Input();// Now Input() is call for each order
	}
}
#coder777

Oh thanks, It worked now. Is it because the new object of type Order is the array to let the function Input() work in?
Is it because the new object of type Order is the array to let the function Input() work in?
Um, what?

In your code on line 5 you created a temporary variable named Input which is unrelated to the member function Input() (which is shadowed). orders[i] is provided to the constructor of that temporary variable Input.
#coder777

Thanks a lot now I understand.
Topic archived. No new replies allowed.