Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Basic Exception Handling

Reply
Thread Tools

Basic Exception Handling

 
 
Handy Handy is offline
Junior Member
Join Date: Apr 2009
Posts: 1
 
      04-13-2009
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[i];

}
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 by Handy; 04-13-2009 at 10:23 PM..
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
signal handling and (structured) exception handling Peter C++ 34 10-17-2009 10:03 AM
Exception of type 'System.Web.HttpUnhandledException' wasthrown.Exception has been thrown by the target of an invocation.System.WebSystem.Exception jobs ASP .Net 1 11-16-2007 05:57 PM
Basic question on exception handling in C++ masood.iqbal@lycos.com C++ 3 06-13-2007 06:26 AM
while executing my client program i get the exception javax.naming.LinkException: [Root exception is javax.naming.LinkException: [Root exception is javax.naming.NameNotFoundException: remaining if plz anybody know how to solve this problem then mahesh Java 0 03-08-2007 12:26 PM
SoapExtension for Global Exception handling; but prevent exception from propagating!! VSK ASP .Net Web Services 0 07-29-2003 05:39 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57