6

I'm trying to create an application in C++. In the application I have the default constructor and another constructor with 3 arguments. The user is providing from the keyboard an integer that it will be used to create an array of objects using the non default constructor. Unfortunately I haven't been able to finish it till now, since I'm having issues with the creation of the array of objects that they will use the non default constructor. Any suggestions or help?

#include<iostream>
#include<cstring>
#include<cstdlib>
#include <sstream>

using namespace std;

class Station{

public:
    Station();
    Station(int c, char *ad, float a[]);    
    ~Station(); 


    void setAddress(char * addr){

        char* a;
        a = (char *)(malloc(sizeof(addr+1)));
        strcpy(a,addr);
        this->address = a;
    }

    void setCode(int c){
        code=c; 
    }

    char getAddress(){
        return *address;
    }

    int  getCode(){
        return code;
    }


    float getTotalAmount(){
        float totalAmount=0;
        for(int i=0;i<4;i++){
            totalAmount+=amount[i];
        }
        return totalAmount;
    }

    void print(){

        cout<<"Code:"<<code<<endl;
        cout<<"Address:"<<address<<endl;
        cout<<"Total Amount:"<<getTotalAmount()<<endl;
        cout<<endl;
    }


private:
    int code;
    char *address;
    float amount[4];

};


Station::Station(){
    code= 1;
    setAddress("NO ADDRESS GIVEN");
    amount[0]= 0.0;
    amount[1]= 0.0;
    amount[2]= 0.0;
    amount[3]= 0.0;

}


Station::Station(int c, char *ad, float a[]){

    if( (c>=1&& c<=10 ) ){
        code=c;
        address=ad;

        for(int i=0;i<4;i++){
            amount[i]=a[i]; 
        }   

    }else{

        code= 1;

        setAddress("NO ADDRESS GIVEN");
        amount[0]= 0.0;
        amount[1]= 0.0;
        amount[2]= 0.0;
        amount[3]= 0.0;
    }   
}   


Station::~Station(){

}

int main(){

    int size,code;
    char *addrr;
    addrr = (char *)(malloc(sizeof(addrr+1)));
    float mes[4];

    do{ 
        cout<<"size of array:";
        cin>>size;

    }while(size<=0 || size>=11);

    //  Station *stations= new Station[size];
    //  Station** stations = new Station*[size];
    Station stations[size];

    for(int i=0;i<size;i++){

        cout<<"code:";
        cin>>code;

        cout<<"address:";
        cin>>addrr;

        double amo=0;

        for(int k=0;k<4;k++){
            cout<<"values"<<k+1<<":";
            cin>>mes[k]; 
        }
    }
    /*
    for(int q=0;q<size;q++){
        stations[q].print();
    }
    */

    return 0;
}

the values that I'll take from cin I want to assign them to the objects of the array!

6
  • 1
    mattgemmell.com/2008/12/08/what-have-you-tried Commented Nov 29, 2011 at 14:07
  • show me the creation of arrays, do you use new or malloc? Commented Nov 29, 2011 at 14:07
  • you can have a look in the code above! thank you Commented Nov 29, 2011 at 14:46
  • 1
    that code has lots of problems. like memory leaks and Undefined Behavior. as a first step that is likely to fix most of that, replace all use of char* with std::string. Commented Nov 29, 2011 at 15:19
  • im not allowed to use string! Commented Nov 29, 2011 at 17:30

3 Answers 3

11

You can either create the array default-initialized and then fill the array with the wanted object:

foo arr[10];
std::fill(arr, arr+10, foo(some, params));

Alternatively you could use std::vector and do just:

std::vector<foo> arr(10, foo(some, params));
Sign up to request clarification or add additional context in comments.

Comments

3

In C++0x, you can use braced-init-list in new expression, which means you can do this:

#include <iostream>

class A
{
public:
    A(int i, int j){std::cout<<i<<" "<<j<<'\n';}
};

int main(int argc, char ** argv)
{
    int *n = new int[3]{1,2,3};
    A *a = new A[3]{{1,2},{3,4},{5,6}};
    delete[] a;
    delete[] n;
    return 0;
}

Compiled under g++ 4.5.2, using g++ -Wall -std=c++0x -pedantic

Comments

0

Since you say you can't use std::string, this is going to be much more difficult. The line addrr = (char *)(malloc(sizeof(addrr+1))); is not doing what you think it is. Instead of using malloc to allocate on the heap and since there is no free (which will lead to a memory leak), it will be much easier if we allocate on the stack with a predetermined buffer size: char addrr[BUFFER_LENGTH]. With BUFFER_LENGTH defined before Station's declaration as const int BUFFER_LENGTH = 20; or some other appropriate length.

To use the non-default constructor, adding stations[i] = Station(c, addrr, mes); at the end of the for loop will do the trick.

for(int i=0;i<size;i++){
    cout<<"code:";
    cin>>code;

    cout<<"address:";
    cin>>addrr; // do not read in strings longer than 20 characters or increase BUFFER_LENGTH’s size

    double amo=0;

    for(int k=0;k<4;k++){
        cout<<"values"<<k+1<<":";
        cin>>mes[k];
    }

    stations[i] = Station(c, addrr, mes);
}

But, this is not going to work properly since the constructor is copying the addrr pointer, not the data. I would recommend also changing the data member char *address to char address[BUFFER_LENGTH]. Then, in the constructor you can replace the line address=ad; with strcpy(address, ad);.

Note: setAddress and getAddress will now need to be updated.

Another line that is troubling is Station stations[size];. This is non-standard since size is not a known at compile time. Either use Station *stations= new Station[size]; and remember to delete or if you can use a std::vector, use std::vector<Station> stations(size);

If you do go the std::vector route, using push_back will work nicely:

std::vector<Station> stations;

for(int i=0;i<size;i++){

    cout<<"code:";
    cin>>code;

    cout<<"address:";
    cin>>addrr;

    double amo=0;

    for(int k=0;k<4;k++){
        cout<<"values"<<k+1<<":";
        cin>>mes[k]; 
    }

    stations.push_back( Station(c, addrr, mes) );
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.