Home > Mobile >  trouble using Function with data type struct in c
trouble using Function with data type struct in c

Time:02-16

My assignment has given me a function prototype, Apartment get_apartment_info();, that I need to use, which asks me to do this:

function should prompt the user for all the information about an apartment, store it an Apartment variable and return it.

I don't know how I would go about accomplishing this, but what I have tried returns this error after I input all the information:

free pointer(): invalid pointer signal: aborted (core dumped)

#include <iostream>
#include <string>
using namespace std;

struct Apartment {

  Apartment get_apartment_info() {
    cout << "enter building number: ";
    cin >> building_number;
    cout << "enter apartment number: ";
    cin >> apartment_number;
    cout << "enter apartment type: ";
    cin >> apartment_type;
    cout << "enter view: ";
    cin >> view;
    cout << "enter your name: ";
    cin >> renter_name;
  };

  int building_number;
  int apartment_number;
  string apartment_type;
  string view;
  string renter_name;
  double amount_due;
};

int main() {
  Apartment complex;
  complex.get_apartment_info();
}

Since this isn't the way to achieve what I am trying to, how would I accomplish this?

CodePudding user response:

With How do I ask and answer homework questions? in mind, my answer is: You are actually almost there. The last ingredient you need is how to return a struct as a value. I will show some simplified example:

#include <iostream>

struct Point{
    int x;
    int y;
};

Point make_point(int x, int y){
    return Point{x,y};
}

int main(){
    using namespace std;
    Point MyPoint = make_point(3,4);
    cout << "My point is:" << MyPoint.x <<", " << MyPoint.y << endl;
}

return Point{x,y}; is the point I want to emphasize. You need the function which returns an Apartment, right? Modify this syntax to make appropriate return Apartment class.

CodePudding user response:

The problem is that you didn't follow the directions you were given:

function should prompt the user for all the information about an apartment, store it an Apartment variable and return it.

Your function promises to return an Apartment object, but it doesn't return anything at all, which is undefined behavior. So the code crashes when it tries to destruct a non-existant object.

Try this instead:

#include <iostream>
#include <string>
using namespace std;

struct Apartment {

  Apartment get_apartment_info() {
    Apartment result;
    cout << "enter building number: ";
    cin >> result.building_number;
    cout << "enter apartment number: ";
    cin >> result.apartment_number;
    cout << "enter apartment type: ";
    cin >> result.apartment_type;
    cout << "enter view: ";
    cin >> result.view;
    cout << "enter your name: ";
    cin >> result.renter_name;
    return result;
  };

  int building_number;
  int apartment_number;
  string apartment_type;
  string view;
  string renter_name;
  double amount_due;
};

int main() {
  Apartment complex;
  complex.get_apartment_info();
}

That being said, the function, as defined, doesn't make sense as a member of the struct. Chances are, the assignment meant for it to be a free-standing function instead, eg:

#include <iostream>
#include <string>
using namespace std;

struct Apartment {
  int building_number;
  int apartment_number;
  string apartment_type;
  string view;
  string renter_name;
  double amount_due;
};

Apartment get_apartment_info() {
  Apartment result;
  cout << "enter building number: ";
  cin >> result.building_number;
  cout << "enter apartment number: ";
  cin >> result.apartment_number;
  cout << "enter apartment type: ";
  cin >> result.apartment_type;
  cout << "enter view: ";
  cin >> result.view;
  cout << "enter your name: ";
  cin >> result.renter_name;
  return result;
};

int main() {
  Apartment complex = get_apartment_info();
}
  • Related