Basic Exception Handling

Discussion in 'C Programming' started by Handy, Apr 13, 2009.

  1. Handy

    Handy

    Joined:
    Apr 13, 2009
    Messages:
    1
    Likes Received:
    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: Apr 13, 2009
    Handy, Apr 13, 2009
    #1
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Colin Hemmings

    Basic event handling

    Colin Hemmings, Feb 17, 2006, in forum: Java
    Replies:
    5
    Views:
    2,048
    Thomas Weidenfeller
    Feb 20, 2006
  2. Michael Satterwhite

    (Very) Basic question re: exception

    Michael Satterwhite, Apr 21, 2004, in forum: C++
    Replies:
    6
    Views:
    657
    John Carson
    Apr 21, 2004
  3. Engineer
    Replies:
    6
    Views:
    956
    Jeremy Bowers
    May 1, 2005
  4. masood.iqbal@lycos.com

    Basic question on exception handling in C++

    masood.iqbal@lycos.com, Jun 12, 2007, in forum: C++
    Replies:
    3
    Views:
    540
    =?iso-8859-1?q?Erik_Wikstr=F6m?=
    Jun 13, 2007
  5. Mark Tarver
    Replies:
    22
    Views:
    1,676
    J Kenneth King
    Apr 26, 2009
  6. Peter
    Replies:
    34
    Views:
    2,421
    James Kanze
    Oct 17, 2009
  7. VSK
    Replies:
    0
    Views:
    409
  8. Iñaki Baz Castillo
    Replies:
    1
    Views:
    472
    Iñaki Baz Castillo
    Apr 15, 2008
Loading...