Help - class - bank

All the problems are in the line 67 of the main, help me please, I can't find the error.

Here there are the three errors that gives the compiler:

1st)
error C2466: cannot allocate an array of constant size 0


2nd)
error C2075: 'c2' : array initialization needs curly braces


3rd)
error C2057: expected constant expression


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//Credito.h
#include <iostream>
using namespace std;
#include "math.h"

class Credito
{
	double intAnual;
	int nMeses;
	double Capital;

public:
	double CalculaCuota(Credito c);
	Credito(double a,int b, double c);
	Credito()
	{
		intAnual = Capital = 0;
		nMeses = 0;
	};
	~Credito(void);
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//Credito.cpp
#include "Credito.h"

Credito::Credito(double a,int b, double c)
{
	intAnual = a;
	nMeses = b;
	Capital = c;
}

Credito::~Credito(void)
{
	//delete c;
}

double Credito::CalculaCuota(Credito c)
{
	double A = 0;
	double Cuotamensual = 0;
	A = c.intAnual/(100*12);//interes mensual
	Cuotamensual = ((c.Capital * A)/(1-pow((1/(1+A)),c.nMeses)));
	return (Cuotamensual);
}


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
80
81
82
83
84
//main.cpp
#include "Credito.h"

void main()
{
	int opcion = 0;
	Credito *c2;//creación vector de objetos (1/2)

	while(1)
	{
		cout<<"\n\n\t***MENU***"<<endl;
		cout<<endl;
		cout<<"\t1) Calcula Cuota"<<endl;
		cout<<"\t2) Siulacion"<<endl;
		cout<<"\t3) Terminar"<<endl;
		cout<<"OPCION ==> ";
		cin>> opcion;
		cout <<endl;

		if (opcion == 1)
		{
			double a = 0;
			int b = 0;
			double c = 0;
			double cuota = 0;
			cout<<"Monto del capital a prestar = ";
			cin >> c;
			cout<<"\nInteres Anual";
			cin >> a;
			cout<<"\nNumero de meses";
			cin >> b;

			Credito c1(a,b,c);

			cuota = c1.CalculaCuota(c1);

			cout<<" Cuota = " << cuota<<endl;
		}
		else if (opcion == 2)
		{
			double a = 1;
			int b1 = 1;
			int b2 = 1;
			double c = 1;
			int longitud = 1;

			cout<<"Monto del capital a prestar = ";
			cin >> c;
			cout<<"\nInteres Anual = ";
			cin >> a;
			cout<<"\nMes de inicio de la simulacion = ";
			cin >> b1;
			cout << "\nMes final de la simulacion = ";
			cin >> b2;
			longitud = b2-b1;
			double *v = new double[longitud];
			c2 = new Credito[longitud];//creación vector de objetos (2/2)
			/*escrito de otra forma
			Credito c* = new Credito[longitud];
			pero lo hemos dividido en dos partes para no olvidar que se
			puede haceer así por si tenemos que hacer un menú o algo raro
			de ese tipo que pida dividirla en dos*/
			cout<<"MES\tCUOTA"<<endl;

			for(int i = b1; i <= b2 ; i++)
			{
				Credito c2[i-b1](a,i,c);
				cout<<"\t"<<i<<"\t"<< c2[i-b1].CalculaCuota(c2[i-b1]);		
			}
		}
		else if (opcion == 3)
		{
			system("cls");
			cout<<"THE END"<<endl;
			cout<<endl;
			exit(0);
		}
		else 
		{
			system("cls");
			cout<<"No se contempla esa opcion";
		}
	}
}
Credito c2[i-b1](a,i,c);
should be
c2[i-b1](a,i,c);
Thanks Yay295, that was one important thing, but the problem in the line 67 persists and I don't know what to do:

term does not evaluate to a function taking 3 arguments


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//Credito.h
#include <iostream>
using namespace std;
#include "math.h"

class Credito
{
	double intAnual;
	int nMeses;
	double Capital;

public:
	double CalculaCuota(Credito c);
	Credito(double a,int b, double c);
	Credito()
	{
		intAnual = Capital = 0;
		nMeses = 0;
	};
	~Credito(void);
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//Credito.cpp
#include "Credito.h"

Credito::Credito(double a,int b, double c)
{
	intAnual = a;
	nMeses = b;
	Capital = c;
}

Credito::~Credito(void)
{
	//delete c;
}

double Credito::CalculaCuota(Credito c)
{
	double A = 0;
	double Cuotamensual = 0;
	A = c.intAnual/(100*12);//interes mensual
	Cuotamensual = ((c.Capital * A)/(1-pow((1/(1+A)),c.nMeses)));
	return (Cuotamensual);
}


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
80
81
82
83
84
85
86
//main.cpp
#include "Credito.h"

void main()
{
	int opcion = 0;
	Credito *c2;//creación vector de objetos (1/2)

	while(1)
	{
		cout<<"\n\n\t***MENU***"<<endl;
		cout<<endl;
		cout<<"\t1) Calcula Cuota"<<endl;
		cout<<"\t2) Siulacion"<<endl;
		cout<<"\t3) Terminar"<<endl;
		cout<<"OPCION ==> ";
		cin>> opcion;
		cout <<endl;

		if (opcion == 1)
		{
			double a = 0;
			int b = 0;
			double c = 0;
			double cuota = 0;
			cout<<"Monto del capital a prestar = ";
			cin >> c;
			cout<<"\nInteres Anual";
			cin >> a;
			cout<<"\nNumero de meses";
			cin >> b;

			Credito c1(a,b,c);

			cuota = c1.CalculaCuota(c1);

			cout<<" Cuota = " << cuota<<endl;
		}
		else if (opcion == 2)
		{
			double a = 1;
			int b1 = 1;
			int b2 = 1;
			double c = 1;
			int longitud = 1;

			cout<<"Monto del capital a prestar = ";
			cin >> c;
			cout<<"\nInteres Anual = ";
			cin >> a;
			cout<<"\nMes de inicio de la simulacion = ";
			cin >> b1;
			cout << "\nMes final de la simulacion = ";
			cin >> b2;
			longitud = b2-b1;
			double *v = new double[longitud];
			c2 = new Credito[longitud];//creación vector de objetos (2/2)
			/*escrito de otra forma
			Credito c* = new Credito[longitud];
			pero lo hemos dividido en dos partes para no olvidar que se
			puede haceer así por si tenemos que hacer un menú o algo raro
			de ese tipo que pida dividirla en dos*/
			cout<<"MES\tCUOTA"<<endl;

			for(int i = b1; i <= b2 ; i++)
			{
				c2[i-b1](a,i,c);
				cout<<"\t"<<i<<"\t"<< c2[i-b1].CalculaCuota(c2[i-b1]);		
			}
			delete [] v;
			delete [] c2;
		}
		else if (opcion == 3)
		{
			system("cls");
			cout<<"THE END"<<endl;
			cout<<endl;
			exit(0);
		}
		else 
		{
			system("cls");
			cout<<"No se contempla esa opcion";
		}
	}
}
Last edited on
I think the trouble you're running into is that the default constructor is called when you create the array of Credito objects with longitud length at line 57.

Then on line 67 you're trying to call another constructor on the existing objects.

I might suggest creating set functions to update intAnual, nMeses, and Capital when you have an existing Credito object. Maybe someone else has another solution?
Sorry, but I can't understand you, can you exlane me more expecifically? Tanks
Change line 67 to c2[i-b1] = Credito(a,i,c);

Also change line 65 to for(int i = b1; i < b2 ; i++)
Now I've onle one question more, where I've to delete the two pointers????
Thaks "Yay295" really, but the line 65 is ok, the line 67 you are rigt


//main.cpp
#include "Credito.h"

void main()
{
	int opcion = 0;
	Credito *c2;//creación vector de objetos (1/2)

	while(1)
	{
		cout<<"\n\n\t***MENU***"<<endl;
		cout<<endl;
		cout<<"\t1) Calcula Cuota"<<endl;
		cout<<"\t2) Simulacion"<<endl;
		cout<<"\t3) Terminar"<<endl;
		cout<<"OPCION ==> ";
		cin>> opcion;
		cout <<endl;

		if (opcion == 1)
		{
			double a = 0;
			int b = 0;
			double c = 0;
			double cuota = 0;
			cout<<"Monto del capital a prestar = ";
			cin >> c;
			cout<<"\nInteres Anual";
			cin >> a;
			cout<<"\nNumero de meses";
			cin >> b;

			Credito c1(a,b,c);

			cuota = c1.CalculaCuota(c1);

			cout<<" Cuota = " << cuota<<endl;
		}
		else if (opcion == 2)
		{
			double a = 1;
			int b1 = 1;
			int b2 = 1;
			double c = 1;
			int longitud = 1;

			cout<<"Monto del capital a prestar = ";
			cin >> c;
			cout<<"\nInteres Anual = ";
			cin >> a;
			cout<<"\nMes de inicio de la simulacion = ";
			cin >> b1;
			cout << "\nMes final de la simulacion = ";
			cin >> b2;
			longitud = b2-b1;
			double *v = new double[longitud];
			c2 = new Credito[longitud];//creación vector de objetos (2/2)
			/*escrito de otra forma
			Credito c* = new Credito[longitud];
			pero lo hemos dividido en dos partes para no olvidar que se
			puede haceer así por si tenemos que hacer un menú o algo raro
			de ese tipo que pida dividirla en dos*/
			cout<<"MES\tCUOTA"<<endl;

			for(int i = b1; i <= b2 ; i++)
			{
				//c2[i-b1](a,i,c); // ESTO ESTÁ MALLLLLLLLLLLLLLLLLLLLLLLLLLLLL
				c2[i-b1] = Credito(a,i,c); //BIENNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
				v[i-b1]= c2[i-b1].CalculaCuota(c2[i-b1]);	
				cout<<"\t"<<i<<"\t"<< v[i-b1]<<endl;		
			}
			delete [] v;
			delete [] c2;

		}
		else if (opcion == 3)
		{
			system("cls");
			cout<<"THE END"<<endl;
			cout<<endl;			
			exit(0);
		}
		else 
		{
			system("cls");
			cout<<"No se contempla esa opcion";
		}
	}

}
Topic archived. No new replies allowed.