I am having a rather difficult time with exception handling in this manner. My program can correctly calculate the number, mean, and median of integers in a vector, however, if I start throwing in characters or doubles into my vector and try to catch the exceptions, my program won't ignore the exceptions and will compute them anyway into number, mean and median of the values in my vector. Advice on what I can do would be very much appreciated.
// Read a list of integer data, terminated by an EOF.
// Display the number of values, arithmetic mean and median of
// the list on the standard output. Input data are handled with
// care to "ignore" extraneous junk such as decimal points and
// non-numeric characters.
#include <vector>
using std::vector;
#include <iostream>
using std::cout;
using std:: endl;
using std::cin;
#include <ios>
using std::ios_base;
#include <stdexcept>
#include <algorithm>
using std::sort;
#include <string>
using std::string;
int vectorvalue;
void DisplayInstructions()
{
cout << "We'll return the number of integers, mean and median." << endl;
}
void ReadTheData( vector<int>& TheData )
{
int temp;
cin.exceptions(ios_base::failbit);
//first value, if ctrl + z, then finish program
//Read data
//While not ^Z, input data, even bad data.
//Catch errors, such as chars or strings.
bool done = false;
while (!done) // while true
{
try
{
cin >> temp;
TheData.push_back(temp);
}
catch (ios_base::failure& e )
{
if (cin.eof())
{
break;
}
else
{
cin.clear();
cin.ignore();
}
}
}
}
void ComputeStats( vector<int>& TheData, int& num, double& mean, double& median )
{
//calculate sum
double vecelmean = 0;
int i;
int vecelsum = 0;
for (i = 0; i < TheData.size(); i++)
{
vecelsum = (vecelsum* 1.0) + TheData;
}
num = TheData.size();
//calculate mean
mean = (vecelsum * 1.0) /TheData.size();
// calculate median
sort(TheData.begin(),TheData.end() );
double vecelmedian;
double middle_position_one;
double middle_position_two;
int vector_size = num;
if((vector_size % 2) == 0)
{//even
middle_position_one = (vector_size /2);
middle_position_two = (vector_size /2);
vecelmedian = (TheData[middle_position_one] + TheData[middle_position_two])/2.0;
}
else
{//odd
middle_position_one = (vector_size /2);
vecelmedian = TheData[middle_position_one];
}
median = vecelmedian;
}
void DisplayStats( int num, double mean, double median )
{
cout << "********** Data Summary ********** " << endl;
cout << "Number of Values: " << num << endl;
cout << "Mean Value: " << mean << endl;
cout << "Median Value: " << median << endl;
}
int main()
{
vector<int> TheData;
DisplayInstructions();
ReadTheData( TheData );
int numValues;
double meanValue, medianValue;
ComputeStats( TheData, numValues, meanValue, medianValue );
DisplayStats( numValues, meanValue, medianValue );
return 0;
}
// Read a list of integer data, terminated by an EOF.
// Display the number of values, arithmetic mean and median of
// the list on the standard output. Input data are handled with
// care to "ignore" extraneous junk such as decimal points and
// non-numeric characters.
#include <vector>
using std::vector;
#include <iostream>
using std::cout;
using std:: endl;
using std::cin;
#include <ios>
using std::ios_base;
#include <stdexcept>
#include <algorithm>
using std::sort;
#include <string>
using std::string;
int vectorvalue;
void DisplayInstructions()
{
cout << "We'll return the number of integers, mean and median." << endl;
}
void ReadTheData( vector<int>& TheData )
{
int temp;
cin.exceptions(ios_base::failbit);
//first value, if ctrl + z, then finish program
//Read data
//While not ^Z, input data, even bad data.
//Catch errors, such as chars or strings.
bool done = false;
while (!done) // while true
{
try
{
cin >> temp;
TheData.push_back(temp);
}
catch (ios_base::failure& e )
{
if (cin.eof())
{
break;
}
else
{
cin.clear();
cin.ignore();
}
}
}
}
void ComputeStats( vector<int>& TheData, int& num, double& mean, double& median )
{
//calculate sum
double vecelmean = 0;
int i;
int vecelsum = 0;
for (i = 0; i < TheData.size(); i++)
{
vecelsum = (vecelsum* 1.0) + TheData;
}
num = TheData.size();
//calculate mean
mean = (vecelsum * 1.0) /TheData.size();
// calculate median
sort(TheData.begin(),TheData.end() );
double vecelmedian;
double middle_position_one;
double middle_position_two;
int vector_size = num;
if((vector_size % 2) == 0)
{//even
middle_position_one = (vector_size /2);
middle_position_two = (vector_size /2);
vecelmedian = (TheData[middle_position_one] + TheData[middle_position_two])/2.0;
}
else
{//odd
middle_position_one = (vector_size /2);
vecelmedian = TheData[middle_position_one];
}
median = vecelmedian;
}
void DisplayStats( int num, double mean, double median )
{
cout << "********** Data Summary ********** " << endl;
cout << "Number of Values: " << num << endl;
cout << "Mean Value: " << mean << endl;
cout << "Median Value: " << median << endl;
}
int main()
{
vector<int> TheData;
DisplayInstructions();
ReadTheData( TheData );
int numValues;
double meanValue, medianValue;
ComputeStats( TheData, numValues, meanValue, medianValue );
DisplayStats( numValues, meanValue, medianValue );
return 0;
}
Last edited: