i get an error in the loops and the constructor when i run this program and it crashed for A_num>2..plz help me
#include <stdio.h>
#include <stdlib.h>
#include <iostream.h>
#include <string>
using namespace std;
class train {
string Sname,Rname ;
float chargeanakm, productvalue ;
float distance, ins_co , cost ;
public:
train::train();
float getcost();
void printdata(float k);
};
train::train(){
cout << "Eimaste stin metafora me traino\n\n" ;
cout << "Dose tin xreosi ana km\n" ;
cin >> chargeanakm ;
cout << "Dose tin aksia tou proiontos\n" ;
cin >> productvalue ;
cout << "Dose tin apostasi\n" ;
cin >> distance ;
cout << "Dose ton sintelesti ins_co\n" ;
cin >> ins_co ;
cout << "Dose onoma apostolea\n" ;
cin >> Sname ;
cout << "Dose onoma paralipti\n" ;
cin >> Rname ;
}
float train::getcost(){
float kostos;
kostos=(chargeanakm*distance)+(productvalue*distance*ins_co) ;
return kostos ;
}
void train::printdata(float k){
cout << "Onoma apostolea : " << Sname << "\n" ;
cout << "Onoma paralipti : " << Rname << "\n" ;
cout << "Kostos metaforas : " << k << "\n" ;
}
class trucks {
float chargeanakm1, percent ;
float distance1 ;
string Rname1, Sname1 ;
public:
trucks::trucks();
float getcost1();
void printdata1(float k1);
};
trucks::trucks(){
cout << "Eimaste stin metafora me fortigo\n\n" ;
cout << "Dose tin xreosi ana xiliometro\n" ;
cin >> chargeanakm1 ;
cout << "Dose to pososto epi tou kostous metaforas\n";
cin >> percent ;
cout << "Dose tin apostasi\n";
cin >> distance1 ;
cout << "Dose onoma apostolea\n" ;
cin >> Sname1 ;
cout << "Dose onoma parlipti\n" ;
cin >> Rname1 ;
}
float trucks::getcost1(){
float kostos1 ;
kostos1=(chargeanakm1*distance1)+(percent*chargeanakm1*distance1) ;
return kostos1;
}
void trucks::printdata1(float k1){
cout << "Onoma apostolea : " << Sname1 << "\n" ;
cout << "Onoma paralipti : " << Rname1 << "\n" ;
cout << "Kostos metaforas : " << k1 << "\n" ;
}
int main()
{
float total_cost(train x, trucks y,int k,int l);
train *A ;
trucks *B ;
int A_num, B_num, i ;
float temp1,temp2,sinolo;
cout << "Dose arithmo kivotion pou tha metaferthoun me traino\n" ;
cin >> A_num;
cout << "Dose arithmo kivotion pou tha metaferthoun me fortigo\n" ;
cin >> B_num;
cout << "Kalimera giorgos lazaridis\n" << A_num << "\n";
A=(train*)malloc(A_num*sizeof(train));
B=(trucks*)malloc(B_num*sizeof(trucks));
if(!A){
cout << "Allocation failure \n" ;
return 1;
}
if(!B) {
cout << "Allocation failure \n" ;
return 1;
}
cout << "Kalimera giorgos lazaridis\n" << A_num << "\n";
for(i=0; i<A_num; i++){
A[i]=train();
temp1=A[i].getcost();
A[i].printdata(temp1);
}
for(i=0; i<B_num; i++){
B[i]=trucks();
temp2=B[i].getcost1();
B[i].printdata1(temp2);
}
cout << "Sinoliko kostos" << total_cost(A[0],B[0],A_num, B_num);
getchar();
return 0;
}
float total_cost(train x, trucks y,int k, int l){
float total=0;
int i;
for(i=0; i<k; i++){
total+=x.getcost();
}
for(i=0; i<l; i++){
total+=y.getcost1();
}
return total;
}
Indent your code and use [code][/code] tags.