Salve, sto scrivendo un programma che mandando in run si interrompe con il seguente errore "exit code -1073741819 (0xC0000005)". In particolare dopo aver inserito la dimensione della base della figura. Le stesse righe di codice inserite in un'altra finestra non danno problemi.Di seguito copio il main.cpp Monitor.cpp Monitor.h
#include <iostream>
#include "Monitor.h"
using namespace std;
int main() {
Monitor Monitor1;
int row_number=0,col_number=0,n=0,X=0,Y=0;
char c = '0';
do {
cout << "Inserire il numero di righe della matrice:";
cin >> row_number;
if ((row_number <= 0))
cerr << "Il numero inserito (" << row_number << ") deve essere > 0" << endl;
}
while ((row_number <= 0)||(row_number > 30));
Monitor1.setRows(row_number);
do {
cout << "Inserire il numero di colonne della matrice:";
cin >> col_number;
if ( (col_number <= 0))
cerr << "Il numero inserito (" << col_number << ") deve essere > 0" << endl;
}
while ((col_number <= 0)||(col_number > 30));
Monitor1.setCols(col_number);
Monitor1.print();
do{
cout << "\nInserisci una figura nel monitor q per quadrato t per triangolo e r per rombo, 0 per uscire:";
cin >> c;
cout << "\nInserisci dimensione della base della figura:";
cin >> n;
cout << "\nInserisci coordinate X e Y dove collocare la figura:";
cin >> X >> Y;
if (c=='q'){
Monitor1.addSquare(n, X , Y );
}
if (c=='r'){
Monitor1.addRhombus(n, X, Y);
}
if (c=='t'){
Monitor1.addTriangle(n, X, Y);
}
else if (c != '0')
cerr << "\nInserire un valore valido q,r,t o 0";
if(c == 't'||c =='r'||c =='q')
Monitor1.print();
}while(c!= '0');
return 0;
}
#include "Monitor.h"
#include <iostream>
#define OK 0
#define NO_MEM -1
using namespace std;
int **screen = nullptr;
Monitor::Monitor(){
_row_number=0 ; // number of row of the monitor
_col_number=0 ; // number of column of the monitor
_screen[ MAX_R ][ MAX_C ] = {};
_n=0; // figure dimension
_coordinateX=0; // X coordinate
_coordinateY=0 ; // Y coordinate
};
void Monitor::setRows( int row_number ){
_row_number = row_number;
screen = (int **) new (nothrow) int[_row_number];
if (screen == NULL) {
cerr << "Non è stato possibile allocare " << _row_number << " elementi" << endl;
exit(NO_MEM);
}
}
void Monitor::setCols( int col_number){
_col_number=col_number;
for (int i = 0; i < _row_number; i++) {
screen[i] = (int *) new (nothrow) int (_col_number);
if (screen[i] == NULL) {
cerr << "Non è stato possibile allocare " << _col_number << " elementi" << endl;
exit(NO_MEM);
}
}
}
void Monitor::print (){
cout << endl;
for (int i = 0; i < _row_number; i++) {
for (int j = 0; j < _col_number; j++) {
// i => riga, j => colonna
// 0, _col_number - 1 => riga 0
// _col_number, _col_number + _col_number-1 => riga 1
screen[i][j] =_screen[i][j];
cout << screen[i][j] << " ";
}
cout << endl;
}
/* for (int i = 0; i < _row_number; i++) {
for (int j = 0; j < _col_number; j++) {
cout << screen[i][j];
if (j < _col_number - 1) cout << " ";
else cout << endl;
}
}*/
}
Monitor::~Monitor() {
delete screen;
}
void Monitor::addRhombus(int n, int coordinateX, int coordinateY) {
_n = n;
_coordinateX = coordinateX;
_coordinateY = coordinateY;
for (int i = 0;_coordinateY+i < _coordinateY+_n;i++){
for(int j = 0;_coordinateX+j < _coordinateX+_n;j++){
_screen[_coordinateY+i][_coordinateX+j]=1;
}
}
}
void Monitor::addTriangle(int n, int coordinateX, int coordinateY) {
_n = n;
_coordinateX = coordinateX;
_coordinateY = coordinateY;
for (int i = 0;_coordinateY+i < _coordinateY+_n;i++){
for(int j = 0;_coordinateX+j < _coordinateX+_n;j++){
_screen[_coordinateY+i][_coordinateX+j]=1;
}
}
}
void Monitor::addSquare(int n, int coordinateX, int coordinateY) {
_n = n;
_coordinateX = coordinateX;
_coordinateY = coordinateY;
for (int i = 0;_coordinateY+i < _coordinateY+_n;i++){
for(int j = 0;_coordinateX+j < _coordinateX+_n;j++){
_screen[_coordinateY+i][_coordinateX+j]=1;
}
}
}
#define MAX_R 30
#define MAX_C 30
class Monitor {
public :
Monitor (); // Constructor
~ Monitor (); // Destructor
// set the number of row of the monitor
void setRows ( int _row_number );
// set the number of column of the monitor
void setCols ( int col_number );
// add a square
void addSquare ( int n, int coordinateX , int coordinateY );
// add a triangle
void addTriangle (int n, int coordinateX , int coordinateY );
// add a rhombus
void addRhombus ( int n, int coordinateX , int coordinateY );
void print (); // print the Monitor on the terminal
private :
int _screen [ MAX_R ][ MAX_C ]={}; // Pixel matrix of the monitor
int _row_number ; // number of row of the monitor
int _col_number ; // number of column of the monitor
int _n; // figure dimension
int _coordinateX ; // X coordinate
int _coordinateY ; // Y coordinate
};
# endif //ALGORITMI_MONITOR_H
Il terminale scrive questo:
C:\Users\Giuse\CLionProjects\Algoritmi\cmake-build-debug\Es3_03.exe
Inserire il numero di righe della matrice:15
Inserire il numero di colonne della matrice:20
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Inserisci una figura nel monitor q per quadrato t per triangolo e r per rombo, 0 per uscire:q
Inserisci dimensione della base della figura:4
Process finished with exit code -1073741819 (0xC0000005)
Vi ringrazio qualora riusciste a risolvermi il problema. Non so davvero cosa possa essere.