Debug Assertion Failed! due to the use of ifstream
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 21

Thread: Debug Assertion Failed! due to the use of ifstream

  1. #1
    Join Date
    Dec 2013
    Posts
    14

    Exclamation Debug Assertion Failed! due to the use of ifstream

    Hi all,

    I am trying to run the code below but I receive the following error message :

    Debug Assertion Failed!

    Program: C:\Test\Debug\Test.exe
    File: c:\program files\microsoft visual studio 10.0\vc\include\vector
    Line:932

    Expression:vector subscript out of range
    Thanks in advance for your help.

    Code:
    #include <fstream>
    #include <cstdlib>
    #include <iostream>
    #include <strstream>
    #include <cstring>
    #include <cmath>
    #include <map>
    #include "Swap.h"
    #define SIZE_X 100
    
    void main()
    {	
        cout.setf(ios::showpoint);
        cout.precision(8);
    
        cout << "Swap Pricing Pay Fixed  " << endl << endl;
        Date start = "3/29/2004";  
        cout << "Start date = " << start << endl;
        Date maturity = "3/28/2013";
        cout << "Maturity = " << maturity << endl;
        Date valuation = "10/14/2005"; //today";
        cout << "Valuation = " << valuation << endl;
        Date effectiveDate = "today";
        double notional = 3300000;
        cout << "Notional = " << notional << endl;
        double swapRate = 0.03969;
        cout << "Swap Rate = " << swapRate << endl;
    
        std::vector<double> mat;
        std::map<double,double> libor;
        std::map<double,double> discRate;
        char buffer[SIZE_X];
        char dataBuffer[SIZE_X];
        char* str = NULL;
        double yr = 0.0;
        double rate = 0.0;
        int swapType = 1;  // receive fixed-pay float ; 1 = pay fixed-receive float
    
        
        const char* file = "C:\WAR ROOM\FINANCE\Cpp\Test\Test\swapData.txt";
        ifstream fin;             // input file stream
        fin.clear();
        fin.open(file);
    	
        if (fin.good())
        {
    	while (!fin.eof())
    	{
    	    fin.getline(buffer,sizeof(buffer)/sizeof(buffer[0]));
    	    //cout << buffer << endl;
    	    istrstream str(buffer);
    	    // Get data
    	    str >> dataBuffer;
    	    yr = atof(dataBuffer);
    
    	    str >> dataBuffer;	
    	    if (strcmp(dataBuffer,"MO") == 0)
    		yr = (double) yr/12;
    	    else if (strcmp(dataBuffer,"WK") == 0)
    		yr = (double) yr/52;
    	    else if (strcmp(dataBuffer,"DY") == 0)
    		yr = (double) yr/365;
    	    mat.push_back(yr);
    
    	    str >> dataBuffer;
    	    rate = atof(dataBuffer);
    	    libor[yr] = rate;
    
    	    str >> dataBuffer;
    	    rate = atof(dataBuffer);
    	    discRate[yr] = rate;
    	}
         }
         else
    	cout << "File not good!" << "\n";
    
         fin.close();
    
         Swap s(notional,maturity,start,start+1,valuation,libor,discRate,swapRate,swapType);
    }

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,621

    Re: Debug Assertion Failed! due to the use of ifstream

    1) You have :

    Code:
    const char* file = "C:\WAR ROOM\FINANCE\Cpp\Test\Test\swapData.txt";
    It should be

    Code:
    const char* file = "C:\\WAR ROOM\\FINANCE\\Cpp\\Test\\Test\\swapData.txt";
    2) Although <strstream> and its functions are OK, <sstream> is recommended instead.

  3. #3
    Join Date
    Dec 2013
    Posts
    14

    Re: Debug Assertion Failed! due to the use of ifstream

    Quote Originally Posted by Philip Nicoletti View Post
    1) You have :

    Code:
    const char* file = "C:\WAR ROOM\FINANCE\Cpp\Test\Test\swapData.txt";
    It should be

    Code:
    const char* file = "C:\\WAR ROOM\\FINANCE\\Cpp\\Test\\Test\\swapData.txt";
    2) Although <strstream> and its functions are OK, <sstream> is recommended instead.
    Unfortunately I still have the same error message as above using 1).

    Using <sstream> as advised in 2) make the program to not compile.

  4. #4
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,556

    Re: Debug Assertion Failed! due to the use of ifstream

    What is the code in 'swap.h'? As the code stands as shown it shouldn't compile as cout is part of the std namespace and nornally you would use either std::cout or have a statement using namespace std after the STL includes. Also you are using a type Date which doesn't seem to be defined (from datecl.h? - but where is this header file included?) Also you are using vector without including the vector header file.

    Why are you using c-style strings for the file read? Why not use the c++ string class? Why read the data a line at a time and then convert the read line into the required variables? Why not just extract the data straight into those variables?

    I'm not sure what format your data file has, but you could do something like this (not tried)
    Code:
    string yrtype;
    double lib, drate;
    
         while (fin >> yr >> yrtype >> lib >> drate) {
              if (yrtype == "MO") yr /= 12.;
                 else if (yrtype == "WK") yr /= 52.;
                    else if (yrtype == "DY") yr /= 365.;
    
              mat.push_back(yr);
              libor[yr] = lib;
              discRate[yr] = drate;
         }
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  5. #5
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,621

    Re: Debug Assertion Failed! due to the use of ifstream

    Where does the error occur ? The only vector you have is the variable: mat
    And you only do a push_back() with that variable. It is not used anywhere else.
    Does the "Swap" constructor end up using a vector somewhere ? It seems to
    me the error is somewhere in Swap. What happens if you comment out that line ?

    Also, have you verified that : mat , libor , and discRate have the correct values ?

  6. #6
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,471

    Re: Debug Assertion Failed! due to the use of ifstream

    Quote Originally Posted by eBooster View Post
    I am trying to run the code below but I receive the following error message :
    Debug Assertion Failed!

    Program: C:\Test\Debug\Test.exe
    File: c:\program files\microsoft visual studio 10.0\vc\include\vector
    Line:932

    Expression:vector subscript out of range
    Whenever you get the "Debug Assertion Failed!" message you have to press the Retry button to step in the code where the assertion failed.
    Then you"ll be able to see/understand the reason of the failure as well as the place in your own code (using Call Stack) where the problem originally comes from.
    Victor Nijegorodov

  7. #7
    Join Date
    Dec 2013
    Posts
    14

    Re: Debug Assertion Failed! due to the use of ifstream

    Quote Originally Posted by 2kaud View Post
    What is the code in 'swap.h'? As the code stands as shown it shouldn't compile as cout is part of the std namespace and nornally you would use either std::cout or have a statement using namespace std after the STL includes. Also you are using a type Date which doesn't seem to be defined (from datecl.h? - but where is this header file included?) Also you are using vector without including the vector header file.

    Why are you using c-style strings for the file read? Why not use the c++ string class? Why read the data a line at a time and then convert the read line into the required variables? Why not just extract the data straight into those variables?

    I'm not sure what format your data file has, but you could do something like this (not tried)
    Code:
    string yrtype;
    double lib, drate;
    
         while (fin >> yr >> yrtype >> lib >> drate) {
              if (yrtype == "MO") yr /= 12.;
                 else if (yrtype == "WK") yr /= 52.;
                    else if (yrtype == "DY") yr /= 365.;
    
              mat.push_back(yr);
              libor[yr] = lib;
              discRate[yr] = drate;
         }
    I added the statement using namespace std but the result remains the same. I need to take the values from a file.

    The swap.h file is as below :

    Code:
    #ifndef _SWAP_H__
    #define _SWAP_H__
    
    #include "TNT\TNT.h"
    #include "datecl.h"
    #include <string>
    #include <vector>
    #include <map>
    #include <iostream>
    #define NUM_DATES        100
    #define THIRTY            30
    #define THREE_SIXTY      360
    #define THREE_SIXTY_FIVE 365
    #define NOTIONAL         1000000
    using namespace std;
    static std::vector<Date> payDates_;
    
    static double interpolate(double rate1, double rate2, double t1, double t2, double x) {
       double dy = rate2 - rate1;
       double dt = t2 - t1;
       double slope = dy/dt;
    
       return rate1 + slope*x; 
    }
    
    class FloatingLeg
    {
     public:
        FloatingLeg(std::map<double,double> floatLegRate, double floatLegBasis, int payFrequency) 
     		: floatLegRate_(floatLegRate),floatLegBasis_(floatLegBasis),payFrequency_(payFrequency) {}
        FloatingLeg() {}
        virtual ~FloatingLeg() {}
        inline double calcDuration() {  
          double duration = 0.0;
          duration = (double) (payDates_[0] - valuationDate_ + 1)/THREE_SIXTY_FIVE; //(maturityDate_ - valuationDate_ + 1);      
    	//(paysum/val;
          return duration;
        }
        inline void setEffectiveDate(Date date) { startDate_ = date; }
        inline void setValuationDate(Date date) { valuationDate_ = date; }
        inline void setNotional(double notional) { notional_ = notional; }
        inline Date getEffectiveDate() { return startDate_; }
        inline void setFrequency(int frequency) { payFrequency_ = frequency; }
        inline void setMaturityDate(Date mat) { maturityDate_ = mat; }
        inline std::vector<double> getPayFloat() { return floatRates; }
        inline void setFloatValue(double value) { value_ = value; }
        inline double getFloatValue() { 
    	return value_; 
        }
        inline void setFloatRate(std::map<double,double> rate) {
    	floatLegRate_ = rate;
        }
        inline double calcDV01() {
    
    	double duration = calcDuration();
    	double val = getFloatValue();
    	double DV = 0.0;
    
    	DV = -(duration*notional_)*((double)1/10000); 
    	cout << "float DV01 = " << DV << endl;
    
    	return -DV;
        }
        inline double calcModifiedDuration() {
    
    	double val = calcDV01();
    	double marketValue = getFloatValue();
    	double MD = (val/(notional_ + marketValue))*10000;
    
    	cout << "float modified duration = " << MD << endl;
    		
    	return MD;
         }
         inline void calcPayFloat() {
    		
    	std::vector<Date>::iterator iter;
    	double val = 0;
    	double diff = 0.0;
    	int diff1 = 0.0;
    	int d = 0.0;
    	int d1 = 0.0;
    	Date dateDiff;
    	int cnt = 0;
    	double floatVal = 0.0;
    		
    	for (iter = payDates_.begin(); iter != payDates_.end(); iter++)
    	{
    	     d = payDates_[cnt+1] - payDates_[0] + 1;
    	     d1 = payDates_[cnt+1] - payDates_[cnt] + 1;
    			
    	     diff = payDates_[cnt+1] - payDates_[0]+1;
    	     diff = (double) diff/THREE_SIXTY_FIVE;
    	     floatVal = interpolate(floatLegRate_[floor(diff)],floatLegRate_[ceil(diff)],floor(diff),ceil(diff),diff);	
    		
    	     if (payFrequency_ == 1)
    		val = notional_*(THIRTY/THREE_SIXTY)*floatVal;
    	     else 
    		val = notional_*((double)d1/THREE_SIXTY_FIVE)*floatVal;
    			
    	     floatRates.push_back(val);
    			
    	     cnt++;
    	 }	
         }
       private:
            double floatLegBasis_;
    	std::vector<double> floatRates;
    	std::map<double,double> floatLegRate_;
    	std::map<double,double> payfloatLeg_;
    	Date startDate_;
    	Date maturityDate_;
    	Date valuationDate_;
    	double value_;
    	double spread_;
    	double notional_;
    	double duration_;
    	double accrual_;
    	int payFrequency_;
    };
    
    class FixedLeg
    {
       public:
    	FixedLeg(double payfixedLeg, double fixedLegRate, double fixedLegBasis, int payFrequency) 
    		: payfixedLeg_(payfixedLeg), fixedLegRate_(fixedLegRate), fixedLegBasis_(fixedLegBasis),
    		payFrequency_(payFrequency) { }
    	FixedLeg() {}
    	virtual ~FixedLeg() {}
    	inline double calcDV01() {
    
    	  double duration = calcDuration();
    	  double val = getFixedValue(); 
    	  double DV = 0.0;
    
    	  DV = (duration*notional_)*((double)1/10000); 
    	  cout << "fixed DV01 = " << DV << endl;
    
    	  return DV;
    	}
    	inline void setEffectiveDate(Date date) { effectiveDate_ = date; }
    	inline double calcDuration() { 
    
    	  double sum = 0;
    	  double val = getFixedValue();
    	  double duration = 0.0;
    	  double dis = 0.0;
    		
    	  for (int i = 0; i < payfixedLeg_.size(); i++)
    	     sum = sum + payfixedLeg_[i]*((double)(payDates_[i+1] - valuationDate_ + 1)/
    		(maturityDate_ - valuationDate_ + 1));
    
    	     sum = sum + notional_;
    	     duration = sum/val;
    	     cout << "fixed duration = " << duration << endl;
    
    	     return duration;
    	}
    	inline double calcModifiedDuration() {
    
    	   double val = calcDV01();
    	   double marketValue = getFixedValue();
    	   double MD = (val/(notional_ + marketValue))*10000;
    
    	    //cout << "fixed modified duration = " << MD << endl;
    		
    	   return MD;
    	}
    	inline void setFixedValue(double val) { value_ = val; }
    	inline void setValuationDate(Date date) { valuationDate_ = date; }
    	inline double getFixedValue() {
    	   return value_; 
    	}
    	inline void setNotional(double notional) { notional_ = notional; }
    	inline void setFrequency(int frequency) { payFrequency_ = frequency; }
    	inline void setFixedRate(double rate) { fixedLegRate_ = rate; }
    	inline void setMaturityDate(Date mat) { maturityDate_ = mat; }
    	std::vector<double> getPayFixed() { return payfixedLeg_; }
    	inline void calcPayFixed() 
    	{	
    	   std::vector<Date>::iterator iter;
    	   double val = 0;
    	   int diff = 0;
    	   Date dateDiff;
    	   int cnt = 0;
    		
    	   for (iter = payDates_.begin(); iter != payDates_.end(); iter++)
    	   {
    	      if (payFrequency_ == 1)
    	      {
    		   if (cnt <= payDates_.size())
    		   {
    			if (cnt + payFrequency_ < payDates_.size())
    			   diff = payDates_[cnt+payFrequency_] - payDates_[cnt]+1;
    			else
    			   diff = 0;
    		
    			if ((cnt != 0) && (cnt % payFrequency_ == 0))
    			    val = notional_*(THIRTY/THREE_SIXTY)*fixedLegRate_;
    			else
    			    val = 0;
    	           }
    	           else
    		      val = notional_*(THIRTY/THREE_SIXTY)*fixedLegRate_;
    	      } 
    	      else 
    	      {
    		if (cnt <= payDates_.size()) 
    		{
    		   if (cnt + payFrequency_ <= payDates_.size())
    		   {
    		      // subtract five because there are 5 less days in a 360 day year
    			diff = (payDates_[cnt+payFrequency_] - payDates_[cnt] + 1) - 5;
    			//cout << "diff = " << diff << endl;
    	 	   }
    		   else
    		      diff = 0;
    		
    		   if ((cnt > 0) && ((cnt-1) % payFrequency_ == 0)) 
    		      val = notional_*((double)diff/THREE_SIXTY)*fixedLegRate_;
    		   else
    		      val = 0;
    		}
    		else
    		{
    		   //diff = (maturityDate_ - payDates_[cnt] + 1) - 5;
    		   //val = notional_*((double)diff/THREE_SIXTY)*fixedLegRate_;
    		   val = 0;
    		}
    		//cout << "fixed val = " << val << "cnt = " << cnt << endl;
    	       }
    	       cnt++;
    	       payfixedLeg_.push_back(val);
    	  } // for
    	}
       private:
    	std::vector<double> payfixedLeg_;
    	double fixedLegRate_;
    	double fixedLegBasis_;
    	double duration_;
    	double value_;
    	double accrual_;
    	double notional_;
    	double basis_;
            int payFrequency_;
    	Date effectiveDate_;
    	Date maturityDate_;
    	Date valuationDate_;
    };
    
    class Swap
    {
        public:
    	Swap() : notional_(NOTIONAL), maturity_("12/31/2010"), swapType(0) {}
    	Swap(double notional, Date maturity, Date effectiveDate, Date settlementDate, Date valuation, 
    	std::map<double,double> liborRate, std::map<double,double> disc, double fixedRate, int type) 
    	   : notional_(notional), maturity_(maturity), effectiveDate_(effectiveDate), settlementDate_(settlementDate), 		valuationDate_(valuation), floatRates_(liborRate), fixedRate_(fixedRate), swapType(type)
    	{
    	   getNotional();
    	   calcPayDates(effectiveDate_,maturity_,valuationDate_);
    	   fixedLeg.setNotional(notional_);
    	   fixedLeg.setValuationDate(valuationDate_);
    	   fixedLeg.setFrequency(2);
    	   fixedLeg.setFixedRate(fixedRate);
    	   fixedLeg.setMaturityDate(maturity);
    	   fixedLeg.calcPayFixed();
    	   fixedLeg.setEffectiveDate(effectiveDate_);
    	   floatLeg.setMaturityDate(maturity);
    	   floatLeg.setFrequency(4);
    	   floatLeg.setNotional(notional_);
    	   floatLeg.setFloatRate(liborRate);
    	   floatLeg.setValuationDate(valuationDate_);
    	   setDiscountRates(disc);
    	   floatLeg.calcPayFloat();
    	   netPayments();
    	   //fixedLeg.calcModifiedDuration();
    	   //floatLeg.calcModifiedDuration();
    	   calcDV01();			}
    	   virtual ~Swap() {}
    	   inline double getNotional() {
    		return notional_;
    	   }
    	   inline void setDiscountRates(std::map<double,double> rate) {
    	      discRates_ = rate;
    	   }
    	   inline double calcDV01() {  
    		
    	      double val = 0;
    			
    	      if (swapType == 0)
    		 val = fixedLeg.calcDV01() - floatLeg.calcDV01(); 
    	      else
    		val = floatLeg.calcDV01() - fixedLeg.calcDV01();
    
    	      cout << "Swap DV01 = " << val << endl << endl;
    
    	      return val;
    	   }
    	   inline void calcPayDates(Date tradeDate, Date endDate, Date valuation) 
    	   {		
    	      effectiveDate_ = tradeDate-1;
    			
    	      if (effectiveDate_ == Date::SATURDAY)
    		 effectiveDate_ = effectiveDate_ + 2;
    	      else if (effectiveDate_ == Date::SUNDAY)
    		 effectiveDate_ = effectiveDate_ + 1;
    			
    	      Date currDate = effectiveDate_;
    	      int cnt = 0;
    			
    	      while (currDate <= endDate)  
    	      {
    		   currDate.AddMonths(3);
    		   while (currDate.day > effectiveDate_.day)
    			currDate = currDate - 1;
    						
    		   if (currDate <= valuation)
    		   {
    			if (currDate.day_of_week == Date::SATURDAY)
    			   currDate = currDate + 2;
    			else if (currDate.day_of_week == Date::SUNDAY) 
    			   currDate = currDate + 1;
    			else if (currDate == currDate.ChristmasDay(currDate.year))
    			   currDate = currDate + 1;
    						
    			fixedAccruedDate_ = currDate;
    		    }
    		    if ((currDate <= endDate) && (currDate >= valuation))
    		    {
    			if (currDate.day_of_week == Date::SATURDAY)
    			   currDate = currDate + 2;
    			else if (currDate.day_of_week == Date::SUNDAY) 
    			   currDate = currDate + 1;
    			else if (currDate == currDate.ChristmasDay(currDate.year))
    			   currDate = currDate + 1;
    			
    			payDates_.push_back(currDate);
    
    			//cout << "payDate = " << payDates_[cnt] << endl;
    			cnt++;
    		     }
    		}
    	    }
    	    inline void netPayments() 
    	    {
    		double val = 0.0;
    		double y = 0.0;
    		std::vector<double> fixed = fixedLeg.getPayFixed();
    		std::vector<double> fl = floatLeg.getPayFloat();
    		double x = 0;
    		double sum = 0.0;
    		double sumfix = 0.0;
    		double sumfloat = 0.0;
    
    		if (swapType == 0)
    		{
    		   fixedAccrued_ =  notional_*fixedRate_*((valuationDate_ - fixedAccruedDate_))/THREE_SIXTY;
    		   floatAccrued_ = -notional_*floatRates_[0]*((valuationDate_ - fixedAccruedDate_))/THREE_SIXTY_FIVE;         
    		}
    		else
    		{
    		   fixedAccrued_ = -notional_*fixedRate_*((valuationDate_ - fixedAccruedDate_))/THREE_SIXTY;
    		   floatAccrued_ =  notional_*floatRates_[0]*((valuationDate_ - fixedAccruedDate_))/THREE_SIXTY_FIVE;         
    		}
    
    		int cnt = 0;
    		for (int i = 0; i < payDates_.size(); i++)
    		{
    		   x = (double) (payDates_[i+1] - payDates_[0] + 1)/THREE_SIXTY_FIVE;
    		   //if (x > 1)
    		   y = interpolate(discRates_[floor(x)],discRates_[ceil(x)],floor(x),ceil(x),x);
    		   //else 
    		   //	y = interpolate(discRates_[floor(x)],discRates_[ceil(x)-0.5],floor(x),ceil(x)-0.5,x);
    		   //cout << "y = " << y << endl;
    
    		   if (swapType == 0)
    		   {
    		      val = (fixed[i] - fl[i])*y;
    		      sumfix = sumfix + fixed[i]*y;
    		      sumfloat = sumfloat - fl[i]*y;
    		   }
    		   else
    		   {
    		      val = (fl[i] - fixed[i])*y;
    		      sumfix = sumfix - fixed[i]*y;
    		      sumfloat = sumfloat + fl[i]*y;
    		   }	
    		   sum = sum + val;
    		
    		   //cout << "payDates = " << payDates_[i] << " " << "net PV = " << val << endl;
    		}
    		fixedLeg.setFixedValue(sumfix);
    		floatLeg.setFloatValue(sumfloat);
    			
    		cout << "Fixed Accrued = " << fixedAccrued_ << endl;
    		cout << "Float Accrued = " << floatAccrued_ << endl;
    		cout << "Accrued = " << fixedAccrued_ + floatAccrued_ << endl;
    		cout << "Principal = " << sum << endl;
    		cout << "Market Value = " << sum + (fixedAccrued_ + floatAccrued_) << endl;
    	     }
    	private:
    	  int swapType;
    	  double notional_;
    	  double fixedAccrued_;
    	  double floatAccrued_;
    	  double fixedRate_;
    	  FloatingLeg floatLeg;
    	  FixedLeg fixedLeg;
    	  Date maturity_;
    	  Date fixedAccruedDate_;
    	  Date floatAccruedDate_;
    	  Date effectiveDate_;
    	  Date settlementDate_;
    	  Date valuationDate_;
    	  std::string index;
    	  std::map<double,double> discRates_;
    	  std::map<double,double> floatRates_;
    	  double value;
    };
    
    #endif _SWAP_H__
    Last edited by eBooster; March 12th, 2014 at 07:29 AM.

  8. #8
    Join Date
    Dec 2013
    Posts
    14

    Re: Debug Assertion Failed! due to the use of ifstream

    Quote Originally Posted by Philip Nicoletti View Post
    Where does the error occur ? The only vector you have is the variable: mat
    And you only do a push_back() with that variable. It is not used anywhere else.
    Does the "Swap" constructor end up using a vector somewhere ? It seems to
    me the error is somewhere in Swap. What happens if you comment out that line ?

    Also, have you verified that : mat , libor , and discRate have the correct values ?
    I believe the bug rizes from 'const char* file = "C:\\WAR ROOM\\FINANCE\\Cpp\\Test\\Test\\swapData.txt";' and the program does not go in the loop 'if (fin.good())' : this means that program is not able to read the file swapData.txt .

  9. #9
    Join Date
    Dec 2013
    Posts
    14

    Re: Debug Assertion Failed! due to the use of ifstream

    Quote Originally Posted by VictorN View Post
    Whenever you get the "Debug Assertion Failed!" message you have to press the Retry button to step in the code where the assertion failed.
    Then you"ll be able to see/understand the reason of the failure as well as the place in your own code (using Call Stack) where the problem originally comes from.
    I have done this and I am also redirected to the file at C:\Program Files\Microsoft Visual Studio 10.0\VC\include\vector and exactly at this part :

    #if _ITERATOR_DEBUG_LEVEL == 2
    if (size() <= _Pos)
    { // report error
    _DEBUG_ERROR("vector subscript out of range");
    _SCL_SECURE_OUT_OF_RANGE;
    }
    The CallStack indicates the following :

    Test.exe!std::vector<Date,std::allocator<Date> >:: operator[](unsigned int _Pos) Line 933 + 0x37 bytes C++
    Test.exe!FixedLeg::calcPayFixed() Line 212 + 0x23 bytes C++
    Test.exe!Swap::Swap(double notional, Date maturity, Date effectiveDate, Date settlementDate, Date valuation, std::map<double,double,std::less<double>,std::allocator<std:: pair<double const ,double> > > liborRate, std::map<double,double,std::less<double>,std::allocator<std:: pair<double const ,double> > > disc, double fixedRate, int type) Line 266 C++
    Test.exe!main() Line 84 + 0x11f bytes C++
    Test.exe!__tmainCRTStartup() Line 555 + 0x19 bytes C
    Test.exe!mainCRTStartup() Line 371 C
    kernel32.dll!75dfed5c()
    [Frames below may be incorrect and/or missing, no symbols loaded for kernel32.dll]
    ntdll.dll!778e37eb()
    ntdll.dll!778e37be()

  10. #10
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,556

    Re: Debug Assertion Failed! due to the use of ifstream

    Can you post a small sample of your data file please. If I have some data I'll have a look at it using my MSVC.

    NB It's not good practice to put a 'using' statement in an include file. You wouldn't normally put all the class function code into a header either. The class code would normally go into another cpp file (or hpp file for template code) with just the class declarations in the .h header file.
    Last edited by 2kaud; March 12th, 2014 at 08:02 AM.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  11. #11
    Join Date
    Dec 2013
    Posts
    14

    Re: Debug Assertion Failed! due to the use of ifstream

    Quote Originally Posted by 2kaud View Post
    Can you post a small sample of your data file please. If I have some data I'll have a look at it using my MSVC.

    NB It's not good practice to put a 'using' statement in an include file. You wouldn't normally put all the class function code into a header either. The class code would normally go into another cpp file (or hpp file for template code) with just the class declarations in the .h header file.
    I will now try to separate the file as described above.

    However, please find attached the files I am trying to run.
    codeguru_test1.zip

  12. #12
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,471

    Re: Debug Assertion Failed! due to the use of ifstream

    Quote Originally Posted by eBooster View Post
    The CallStack indicates the following :
    Test.exe!std::vector<Date,std::allocator<Date> >:: operator[](unsigned int _Pos) Line 933 + 0x37 bytes C++
    Test.exe!FixedLeg::calcPayFixed() Line 212 + 0x23 bytes C++
    Test.exe!Swap::Swap(double notional, Date maturity, Date effectiveDate, Date settlementDate, Date valuation, std::map<double,double,std::less<double>,std::allocator<std:: pair<double const ,double> > > liborRate, std::map<double,double,std::less<double>,std::allocator<std:: pair<double const ,double> > > disc, double fixedRate, int type) Line 266 C++
    Test.exe!main() Line 84 + 0x11f bytes C++
    Test.exe!__tmainCRTStartup() Line 555 + 0x19 bytes C
    Test.exe!mainCRTStartup() Line 371 C
    kernel32.dll!75dfed5c()
    [Frames below may be incorrect and/or missing, no symbols loaded for kernel32.dll]
    ntdll.dll!778e37eb()
    ntdll.dll!778e37be()
    Well, it is now clear that the problem is somewhere in the Swap() ctor which is called in the line 84 of your main() function.
    Then FixedLeg::calcPayFixed() is called and something wrong happens near the line 212...
    Victor Nijegorodov

  13. #13
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,556

    Re: Debug Assertion Failed! due to the use of ifstream

    In calcPayFixes, there are several lines of code like this
    Code:
    if (cnt <= payDates_.size())
    Are you sure this test should be <= and not < as the elements of the vector go from 0 to .size() -1?
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  14. #14
    Join Date
    Dec 2013
    Posts
    14

    Re: Debug Assertion Failed! due to the use of ifstream

    Quote Originally Posted by VictorN View Post
    Well, it is now clear that the problem is somewhere in the Swap() ctor which is called in the line 84 of your main() function.
    Then FixedLeg::calcPayFixed() is called and something wrong happens near the line 212...
    Yes, I discovered that based on your previous advice. Now I am trying to see why an index may be out of range.

  15. #15
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,556

    Re: Debug Assertion Failed! due to the use of ifstream

    Quote Originally Posted by eBooster View Post
    Yes, I discovered that based on your previous advice. Now I am trying to see why an index may be out of range.
    Possibly because of what I mentioned in my previous post #13.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center