Can anyone tell me why setprecision is causing this error at runtime:

terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
Aborted

The program ran fine; after I added << setprecision(2) << to a series of output, I received the error when trying to run the program. I have included iomanip and the program will run fine if I remove the setprecision manipulator. Thanks for any help.

Here is the snippet of code where I'm trying to use setprecision, avgWait is a float variable:

//Calculate average waiting time,
  //.005 added to round to nearest hundreth of a time unit
  avgWait = ((float)totalWait/totCust) + .005;
  
  cout<< endl << "The simulation ran for " << sTime << " time units" << endl
      << "Number of servers: " << numServers << endl
      << "Average transaction time: " << tTime << endl
      << "Average time between customer arrivals: " << tbArrival << endl
      << "Total waiting time: " << totalWait << endl
      << "Number of customers that completed a transaction: " << servedCust
      << endl
      << "Number of customers left at servers: "
      << servers.getNumberOfBusyServers() << endl
      << "Number of customers left in the queue: " << customers.size() << endl
      << "Average waiting time: " << fixed << setprecision(2) << avgWait << endl
      << "***************END SIMULATION***************" << endl;

Recommended Answers

All 2 Replies

.

After fixing other seemingly unrelated problems with the program, the error stopped occurring. Still, it would be nice to know how just adding the setprecision manipulator caused this error.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.