So I have done this so far. The program works fine for any number of customers. I can change the number of customers by changing the variable NumOfCustomers. But I want the user to input the number of customers. When I try this by doing NumOfCustomers=Num, it gives an error that NumOfCustomers must have a constant value.
Is there a way to do this without using dynamic arrays? If there isn't, then please explain how dynamic array can be used in my case, as I find them hard to understand.
#include <iostream> void calculateCharges(int); int main(){ int Num; std::cout<<"Enter Number of customers: "; std::cin>>Num; calculateCharges(Num); } void calculateCharges(int Num){ const int NumOfCustomers = 3; double hrs[NumOfCustomers], charges[NumOfCustomers]; double Total_hrs=0; double Total_charges=0; for(int i=0;i<NumOfCustomers;i++){ std::cout<<"Enter hours parked: "; std::cin>>hrs[i]; } for(int i =0;i<NumOfCustomers;i++){ if(hrs[i]<=3) charges[i]=2; else if(hrs[i]>3) charges[i]=2+((hrs[i]-3)*.5); if(charges[i]>10) charges[i]=10; } std::cout<<"Car\tHours\tCharge\n"; for(int i=0;i<NumOfCustomers;i++){ std::cout<<i+1<<"\t"<<hrs[i]<<"\t"<<charges[i]<<"\n"; } for(int i=0;i<NumOfCustomers;i++){ Total_hrs += hrs[i]; Total_charges += charges[i]; } std::cout<<"Total\t"<<Total_hrs<<"\t"<<Total_charges<<"\n"; }
std::vector?