In my task I need to input some data about, let's say, depositor and then increase his amount of money by 15% but that`s not the point. The main problem is that it always "triggers a breakpoint" and transfers me to this page
Function where I allocate memory:
void allocateMemory(char*** szData, const int rows, const int words, const int max) {
for (int i = 0; i < rows; i++)
{
szData[i] = new char* [words]; //how many words in every row
for (int j = 0; j < words; j++)
{
szData[i][j] = new char[max]; //maximum quantity of symbols in every word
}
}
}
Function where I assign some values:
void assignFirst(char*** szData, const int rows, const int max) {
char* s1 = new char[max];
char* s2 = new char[max];
char* s3 = new char[max];
char* s4 = new char[max];
char* s5 = new char[max];
cin >> s1 >> s2 >> s3 >> s4 >> s5;
for (int i = 0; i < rows; i++)
{
szData[i][0] = s1;
szData[i][1] = s2;
szData[i][2] = s3;
szData[i][3] = s4;
szData[i][4] = s5;
}
}
Function where I delete memory:
void freeMemory(char*** szData, const int rows, const int words) {
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < words; j++)
{
delete[] szData[i][j];
}
}
for (int i = 0; i < rows; i++)
{
delete[] szData[i];
}
delete[] szData;
}
How it looks in the console and where it breaks
How can I solve it?
P.S. cannot do it using string
(must be an array of characters) or vector
as it is the condition of my task I was given. And yes, I know that new
is sort of outdated
Full code:
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <iomanip>
#include <conio.h>
using namespace std;
void allocateMemory(char*** szData, const int rows, const int words, const int max);
void assignFirst(char*** szData, const int rows, const int max);
void print(char*** szData, const int rows, const int words);
void freeMemory(char*** szData, const int rows, const int words);
void main() {
const int rowCount = 2;
const int wordCount = 5; //number of words in every row
const int maxWordLength = 10;
char*** szData = new char** [rowCount];
allocateMemory(szData, rowCount, wordCount, maxWordLength);
assignFirst(szData, rowCount, maxWordLength);
print(szData, rowCount, wordCount);
freeMemory(szData, rowCount, wordCount);
}
void allocateMemory(char*** szData, const int rows, const int words, const int max) {
for (int i = 0; i < rows; i++)
{
szData[i] = new char* [words]; //how many words in every row
for (int j = 0; j < words; j++)
{
szData[i][j] = new char[max]; //maximum quantity of symbols in every word
}
}
}
void assignFirst(char*** szData, const int rows, const int max) {
char* s1 = new char[max];
char* s2 = new char[max];
char* s3 = new char[max];
char* s4 = new char[max];
char* s5 = new char[max];
cin >> s1 >> s2 >> s3 >> s4 >> s5;
for (int i = 0; i < rows; i++)
{
szData[i][0] = s1;
szData[i][1] = s2;
szData[i][2] = s3;
szData[i][3] = s4;
szData[i][4] = s5;
}
}
void print(char*** szData, const int rows, const int words) {
for (int i = 0; i < rows - 1; i++)
{
for (int j = 0; j < words; j++)
{
if (j == 3) //change fourth element
{
double a = atoi(szData[i][3]); //convert fourth element into double
a = a * 1.15; //add 15% to it
cout << a << " ";
}
else
cout << szData[i][j] << " ";
}
cout << endl;
}
}
void freeMemory(char*** szData, const int rows, const int words) {
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < words; j++)
{
delete[] szData[i][j];
}
}
for (int i = 0; i < rows; i++)
{
delete[] szData[i];
}
delete[] szData;
}