In a battle I realize different situations would occur and it won't always be the same number but how many volleys would two sides of the line infantry, in say mid 18th century through to the Napoleonic Wars, exchange on average before they would engage in a bayonet charge?
And once the bayonet charge occured how would it have looked like? Would it be a wild melee or would they try to maintain a formation and engage in some sort of push of pike?
Last question, did officers use the bayonet charge to try to get the enemy to break once they've been withered down by the volleys? I imagine it was seen as less effective to charge in, rather than simply keep shooting the enemy from range, as I imagine more casualties were to occur in a melee than in an exchange of volleys.
Thank you advance!
This is something that varied by army a good deal through the 18th and 19th centuries. Some favored fire tactics, while others opted for shock; all used fire and the bayonet in combination, but there were a lot of different systems involved both in theory and on the battlefield.
A caveat at the beginning, a bayonet charge only rarely resulted in a melee of people getting stabbed; hand to hand combat is so terrifying that without defensive arms, it's almost impossible to make them stay in place. Those on the attack have forward momentum carrying them on, but the defenders usually abandoned their position in the face of an enemy charge.
Regarding fire tactics,
The Swedes at the beginning of the 18th century usually formed up in four ranks; their doctrine was to advance to close range, fire one salvo from the first two ranks, advance even closer, move the last two ranks into the gaps between men in the first two (thus forming a two rank line), firing a second salvo, and charging with bayonets, swords, and pikes. Often, though, they would dispense with the first salvo, and instead unleash a single massive salvo at clothing burning range before charging. Sometimes there was no attempt to deliver fire whatsoever before the charge, attacking with complete impetuosity without pausing.
The French in the time of Louis XIV delivered fire by ranks, with five or six ranks; the forward ranks knelt while the rearward ranks fired over their heads. Because of the difficulty in reloading while kneeling, french commanders tended to attack once the front rank's muskets were emptied. French doctrine was to not fire at all if the commander's intention was to storm the enemy position, though.
The British and Dutch of the early 18th century, fighting in three ranks, divided their battalions into platoons (18 in the British army), grouped into four 'firings', in which the last two ranks fired, the first being a reserve. Their emphasis was on fire; the intention was to break the enemy by a long succession of continuous volleys.
Frederick the Great inherited an army trained in this school of thought, but his biases inclined him more towards the Franco-Swedish method. However, as his enemies' firepower improved, he needed to synthesize his ideas with the necessity of fire. In this system, the infantry in three ranks would fire by platoons without kneeling during the attack, before charging with the bayonet. He estimated that the firefight should last fifteen minutes before the assault; in more fire-centric armies, this stage of combat could last for hours.
The thing is, these firefights would quickly cease to resemble the disciplined trading of volleys you're imagining; the men reload at different paces, orders get downed out, smoke is everywhere, men tire. Before long, what were distinct volleys blur into a continuous general discharge.
In the later 18th century, most armies tended to shift away from volley fire, following the example of the French armies of the Revolutionary Wars. While skirmishers had always been a part of European armies, they usually filled secondary roles, and were often made up of either specially trained bodies or auxiliary troops, such as the Pandurs and Grenzers in Austrian armies. Increasingly in the French Revolutionary Wars, though, skirmish fire became the armies' main way of conducting the firefight, aided by the increase in artillery. Whole battalions of regular infantry fought in this way. While in most armies specialist companies or the third rank of the battalion were designated the primary skirmishers, every soldier was expected to be able to skirmish as part of the regular course of combat. Soldiers spread out, using cover, and aiming and firing at their own pace were much more effective than crammed together in linear formations where their fellow soldiers interfered with attempts to fire.
As should be apparent, though, such dispersed formations weren't suitable for close combat; it wouldn't be until later in the 19th century, from the Crimean War to the Wars of German Unification that charges in skirmish order became common. For situations where rapid movement was crucial, such as reserves moving across the battlefield, assaults, and counterattacks, the column was the preferred formation. The column, though, is very poor at delivering fire, since depending on the organization and strength of a battalion, only 1/8th to 1/3rd can brig their muskets to bear. As such, the basic concept was to let the skirmishers wear out the enemy before making a brisk charge in column. If the enemy committed more troops to overwhelming the skirmish line, the effort would put them in such a state of disorganization that they would immediately break before the column's bayonet charge.
So it's hard to put any kind of number on this question, since armies in the age of the flintlock muskets ranged from zero to dozens, depending on the tactical system they favored.
Your last question is actually closer to the usual utility of bayonet charges, as you'll see in the answers below. I'm afraid I haven't managed to dig up anything about the average number of volleys traded, so that's open ground for anyone wanting to contribute a newer answer!
On the bayonet matters, these two threads are particularly good reading: