0

so i have a char array that can hold up to 1000 items.. but it might not be filled up completely. How can i prevent the program from printing garbage on the screen after printing the real values from the array. btw i am reading from a file, sorting by description and using a for loop to read in and display output.

file example:

item_a 255 78.25
item_b 150 45.62

void Inventory::ReadFromFile(ifstream &fin)
{
	for(int n=0; n<SIZE; n++)
	{
		fin >> description[n] >> quantity[n] >> unitcost[n];
	}
}

void Inventory::SortByDescription()
{
	BubbleSort(description, SIZE);
}

void Inventory::Output()
{
	
	for(int n=0; n<SIZE; n++)
	{
		cout << description[n] << quantity[n] << unitcost[n];
	}
}
2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by WaltP
0

Keep a count of how many values you actually read into the array.

When you read the file, be sure you stop when you hit the end of the file -- EOF.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.