Basic Exception Handling

Joined
Apr 13, 2009
Messages
1
Reaction score
0
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;
}
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Cannot find my infinite loop 1
Programming math challenge gives wrong answer 2
Filter sober in c++ don't pass test 0
I need help 1
Infinite loop problem 1
Calculater code 0
TF-IDF 1
Crossword 14

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top