Debug Assertion Failed

Hello all,

When i tried to run my program it says that Debug Assertion failed with an error message string subscript out of range.
Here is my program.
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// trajectory 1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


#include<fstream>
#include<iostream>
#include <cstdlib>
#include <algorithm>
#include<list>
#include <math.h>
#include<conio.h>
#include<string>
using namespace std;
ifstream indata;
ofstream outfile;
int main(){
   int count=0;
   string str;
   string str2;
   indata.open("12.txt");
   outfile.open("output.txt");
   if(!indata) { // file couldn't be opened
      cerr << "Error: file could not be opened" << endl;
      exit(1);
   }    
   while(!indata.eof()){
	   count=count+1;
       getline(indata,str);	 
	   unsigned found=str.find("Rotation matrix");
	   int cycle_number=1;
	   int loop_number=0;
	   double sum_of_diagonal=0.00;
	   if(found!=string::npos){		  
		   while( cycle_number<=3){
               int i=0;	
			   while (str[i]){
				  char c=str[i];			   
				  if(c==' '){
					  loop_number=loop_number+1;
					  if( cycle_number==1 && loop_number==5){
						  sum_of_diagonal=sum_of_diagonal+atof(str2.c_str());
					  }else{
						  if( cycle_number==2 && loop_number==4){
							  sum_of_diagonal=sum_of_diagonal+atof(str2.c_str());
						  }

					  }
					 // cout<<"str:"<<str2<<endl;
					  str2="";
					  i++;				
				  }else{				
					 str2+=c;
					 i++;
				  }				
					
			   }
           if( cycle_number==3){
			   sum_of_diagonal=sum_of_diagonal+atof(str2.c_str());
			  /* if( sum_of_diagonal<0){
				   cout<<"stop"<<endl;
			   }*/
			   outfile<<sum_of_diagonal<<'\n';
		   }
		   if( cycle_number<=2){
			  getline(indata,str);	
			  loop_number=0;
		   }
		   str2="";
		   cycle_number=cycle_number+1;		   
		   }
	   }	

   } 
   
   return 0;
}
This condition in while loop

while (str[i]){

is invalid because it is not necessary that objects of type std::string contain the terminating zero.
How should i change that ? please could you help me ?
I do not know what you are trying to do.
converting to string and printing out
What are you going convvert to sttring if str is already defined as string?
Topic archived. No new replies allowed.