user3834119
user3834119

Reputation: 421

Array of user defined size in C++

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";
}

Upvotes: 2

Views: 6696

Answers (5)

Ashin369
Ashin369

Reputation: 1

A simple dynamic memory allocation can be done using c++.Let me show a small snippet regarding dynamic mem allocation.A pointer variable is allocated an integer array with new keyword.Then the memory allocate is deleted after we have completed the required process. NOTE:Dynamic memory allocation uses heap memory part.

#include <iostream>
using namespace std;

int main()
{

    int size;

    cout<<"enter ur array size "<<endl;

    cin >> size;
    int *ash;
    ash = new int[size];
    for (int i=0;i<size;i++){
        cin >> ash[i];

    }

    for (int j=0;j<size;j++){
        cout << ash[j]<<endl;
    }
    delete[] ash;
}

Upvotes: 0

Abhishek Mittal
Abhishek Mittal

Reputation: 366

You cannot have arrays of variable width. Array size should be known during during compilation. Either they need to be constant or some define parameter.

int a[10]

OR

`define A_SIZE 10
int a [A_SIZE]

std::vector is a STL container that is like an array but can change its size dynamically.

Reference on vectors.

http://www.cplusplus.com/reference/vector/vector/

Upvotes: 0

Jack
Jack

Reputation: 133577

You can use a std::vector, which is a class that enables efficient variable length arrays and relief you from manual allocation (you could still use double hrs = new double[NumOfCustomers]; delete [] hrs but this requires manual memory management and you don't want it.)

You can even use a single struct like:

struct CustomerData {
  double hrs;
  double charges;
}

std::vector<CustomerData> data(NumOfCustomers);
..

std::for_each(data.begin(), data.end(), [](CustomerData &item) {
  item.charges = std::min(10, item.hrs <= 3 ? 2 : 2 + ((item.hrs-3)*.5));
});

Upvotes: 1

Dr. Debasish Jana
Dr. Debasish Jana

Reputation: 7118

Here's the modified program. Hope you like!

#include <iostream>

void calculateCharges(int);

int main(){

int Num;
std::cout<<"Enter Number of customers: ";
std::cin>>Num;
calculateCharges(Num);
}

void calculateCharges(int Num){

int NumOfCustomers = Num;
double *hrs, *charges; // placeholder for dynamic array
hrs = new double[NumOfCustomers];
charges = new double[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";
delete [] hrs;
delete [] charges;

}

Upvotes: 1

M.M
M.M

Reputation: 141574

Change

double hrs[NumOfCustomers], charges[NumOfCustomers];

to

std::vector<double> hrs(NumOfCustomers), charges(NumOfCustomers);

Everything else can stay the same. You may need #include <vector>.

It would be a good idea to check std::cin>>hrs[i]; for failure so you don't get weird behaviour if the person doesn't type a number.

Upvotes: 3

Related Questions