简体   繁体   中英

C++ Array Class Returning Strange Value

I'll just go ahead and say that I'm not so good at C++, so it's quite possible that my problem could just be a syntax error that I missed.

Anyways, I've got a program that reads values from a text file and then makes an array of class objects with them. The problem is when I try to return the values to main.cpp I'm getting weird, completely wrong values.

I'm not sure where in the code it the problem is happening, so I'll try to post all the relevant parts.

main.cpp

void findRoom(int capacity, int numRoom);

int numRoom = 0;
std::string room;
int seats;
double len, wid;

#define MAX 100
ClassRoom ClassRoomOb[MAX]

int main()
{
    ifstream classes("classList.txt");

//Makes a count of how many rooms are in the list and builds an array of objects containing all room info
    while (classes >> room >> seats >> len >> wid) { /*Text reads as "ES2.410 110 50 60"*/
        numRoom++;
    ClassRoomOb[numRoom] = ClassRoom(room, seats, len, wid);
    cout << room << " " << seats << " " << len << " " << wid << endl;
}

//Tells the user how many rooms are in the list. For posterity's sake.
ClassRooms ClassRoomsOb(numRoom);
cout << "There are " << ClassRoomsOb.getnumRooms() << " rooms available." << endl;

//For now it's an infinite loop
    while (true) {
    cout << "Enter MAX capacity: ";
    int capacity;
    cin >> capacity;
    findRoom(capacity, numRoom);
}
}
//Find Room Function. To look up room based on user input. Right now just returns the area of the rooms until the value problem gets fixed.
void findRoom(int capacity, int numRoom){

for(int i=0; i < numRoom; i++)
{
    cout << i << ClassRoomOb[numRoom].getAreaPerSeat() << endl;
}

return;

ClassRoom.h

class ClassRoom
{
private:
    int numSeats;
    double length, width, area;
    string roomName;
public:
    ClassRoom(std::string room = "", int seats = 0, double len = 0, double wid = 0);
    int getSeats();
    double getAreaPerSeat();
    std::string getClassRoomName();
};

ClassRoom.cpp

ClassRoom::ClassRoom(string room, int seats, double len, double wid){
    int numSeats = seats;
    double length = len;
    double width = wid;
    string roomName = room;
    double area = width*length;
}
double ClassRoom::getAreaPerSeat(){
    return area;
}

std::string ClassRoom::getClassRoomName(){
    return roomName;
}

With this code cout << ClassRoomOb[numRoom].getAreaPerSeat() will get me things like '1.77793e+263' for each object.

Thank you for any help, even if it's just me being bad at coding and forgetting something simple.

The problem is your constructor ClassRoom::ClassRoom

you're creating new local variables, and assigning values to those, rather than assigning to the member variables.

ClassRoom::ClassRoom(string room, int seats, double len, double wid){
    numSeats = seats;
    length = len;
    width = wid;
    roomName = room;
    area = width*length;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM