Debug assertion failure

I continue to have this error stating

Expression: string subscript out of range when trying to run a C++ program

I have tried to debug however, the error continues to come up each time. Can you help?
Last edited on
Where's the code?
// Nate Perkins
// Program 9
// April 24, 2019
// The strain and stress of programming!

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

double caclEnergy(double data[][50], int limit, double cutP = 100.0)
{
double x1 = data[0][0];
double x2 = data[1][0];
double y1 = data[0][1];
double y2 = data[1][1];
double energy = 0;
double cutPoint = cutP;
for (int i = 1; i < limit; i++)
{
// processing
if (x1 <= cutPoint)
{
//debug
// cout << "x1= " << x1 << " ";
// cout << "x2= " << x2 << " ";
// cout << "y1= " << y1 << " ";
// cout << "y2= " << y2 << " ";

energy += ((y1 + y2) / 2) * (x2 - x1);
// cout << "CEnergy= " << energy << endl;
}
// updates
x1 = x2;
x2 = data[i + 1][0];
y1 = y2;
y2 = data[i + 1][1];
}

return energy;
}
int main()
{
cout << " Nate Perkins" << endl;
cout << " Program 9" << endl;
cout << " April 24, 2019" << endl;
cout << " Strain and Stress of Programming" << endl << endl;

double data[50][50];
int dataSize = 0;
double cutPoint = 0;

// readin data
ifstream inputF("Prog9data.txt");
if (!inputF.is_open())
{
cout << "Error opening file." << endl;
}

// extract data
string buffer;
int counter = 0;
int indexer = 0;
while (getline(inputF, buffer))
{
if (counter == 0 || counter == 1 || counter == 2 || counter == 3)
{
// cout
}
else if (counter == 4)
{
// data set
dataSize = atoi(buffer.c_str());
}
else if (counter == 5)
{
// cut point
cutPoint = atof(buffer.c_str());
}
else
{
// data for the array (parse with a space)
string num1Raw;
string num2Raw;
bool num1Proc = true; // true if processing num1, num2otherwise
for (int i = 0, j = 0; i < buffer.size(); i++)
{
if (buffer[i] == ' ')
{
num1Proc = false;
continue;
}
// processing num1
if (num1Proc)
{
// cout << buffer[i];
num1Raw[i] = buffer[i];
}
else
{
num2Raw[j++] = buffer[i];
}
}
data[indexer][0] = atof(num1Raw.c_str());
data[indexer][1] = atof(num2Raw.c_str());
indexer++;
// cout << data[indexer][0] << " ";
// cout << data[indexer][1] << endl;
}
counter++;
}

// Debug
double Tenergy = caclEnergy(data, indexer);
double Eenergy = caclEnergy(data, indexer, cutPoint);
cout << setw(6) << left << " X ";
cout << setw(12) << " Y " << endl;
cout << setw(6) << "---";
cout << setw(12) << " ---" << endl;

// print all data
for (int i = 0; i < indexer; i++)
{
for (int j = 0; j < 2; j++)
{
if (j == 0)
{
cout << fixed << showpoint;
cout << setprecision(2);
}
else {
cout << noshowpoint;
cout << setprecision(0);
}
cout << setw(4);
cout << data[i][j] << " ";
}
cout << endl;
}

cout << fixed << showpoint;
cout << setprecision(2) << endl;
cout << "Cut Point= " << cutPoint << endl;
cout << noshowpoint;
cout << "Number of data pairs= " << dataSize << endl;
cout << "Total Energy= " << Tenergy << endl;
cout << "Elastic Energy= " << Eenergy << endl;

cout << endl;

system("pause");
return 0;
This is the output I receive:
'Program 9.exe' (Win32): Loaded 'C:\Windows\SysWOW64\CoreMessaging.dll'. Cannot find or open the PDB file.
'Program 9.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ntmarta.dll'. Cannot find or open the PDB file.
'Program 9.exe' (Win32): Loaded 'C:\Windows\SysWOW64\CoreMessaging.dll'. Cannot find or open the PDB file.
'Program 9.exe' (Win32): Unloaded 'C:\Windows\SysWOW64\CoreMessaging.dll'
'Program 9.exe' (Win32): Loaded 'C:\Windows\SysWOW64\WinTypes.dll'. Cannot find or open the PDB file.
'Program 9.exe' (Win32): Loaded 'C:\Windows\SysWOW64\WinTypes.dll'. Cannot find or open the PDB file.
'Program 9.exe' (Win32): Unloaded 'C:\Windows\SysWOW64\WinTypes.dll'
The problem is here:
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
// data for the array (parse with a space)
string num1Raw;
string num2Raw;
bool num1Proc = true; // true if processing num1, num2otherwise
for (int i = 0, j = 0; i < buffer.size(); i++)
{
	if (buffer[i] == ' ')
	{
		num1Proc = false;
		continue;
	}
	// processing num1
	if (num1Proc)
	{
		// cout << buffer[i];
		num1Raw[i] = buffer[i];
	}
	else
	{
		num2Raw[j++] = buffer[i];
	}
}
data[indexer][0] = atof(num1Raw.c_str());
data[indexer][1] = atof(num2Raw.c_str());
indexer++;
// cout << data[indexer][0] << " ";
// cout << data[indexer][1] << endl; 


It seems what you indended to do was this:
1
2
3
4
5
6
7
8
9
if (num1Proc)
{
	// cout << buffer[i];
	num1Raw.push_back(buffer[i]);
}
else
{
	num2Raw.push_back(buffer[i]);
}
Thank you so much, you are a life saver!
It works great now, except I cannot get the right numbers that the num2Raw variable you just fixed needs to bring in from the file. They are all set to zeros in the Y column.
Topic archived. No new replies allowed.