userHR
userHR

Reputation: 13

c++ method parameters giving unexpected output

Main class code:

#include <iostream>
#include "Chair.h"

using namespace std;

int main()
{
cout << "Hello world!" << endl;
Chair c1;
c1.chairType("Olivia",4,32,true);       // not working
Chair c2;
c1.chairType("Stephano",8,8,false);
return 0;
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Header class code:

#ifndef CHAIR_H_INCLUDED
#define CHAIR_H_INCLUDED
#include <iostream>


using namespace std;

class Chair
{
private:
    int legCount;
    int height;
    bool cushioned;
    string name;
public:
    void chairType(string newName, int newLegCount, int newHeight, bool cush);


};



#endif // CHAIR_H_INCLUDED

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Chair.cpp class:


#include <iostream>
#include "Chair.h"

using namespace std;

void Chair::chairType(string newName, int newLegCount, int newHeight, bool cush)
{
    name=newName;
    legCount=newLegCount;
    newHeight=newHeight;
    cushioned=cush;

    cout<<"I'm a chair, the following are my specs: "<<endl;
    cout<<"Model: "<<name<<endl;
    cout<<"Num of Legs: "<<legCount<<endl;
    cout<<"Height: "<<height<<endl;
    cout<<"Cushioned? : "<<cush<<endl;
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Output for the four variables fed into the method is as expected, apart from the third variable (second int) which is printing as being 2752192 regardless of what I feed to it, and for both objects c1 and c2.

I'm new to C++. I've been practising some object class questions trying to familiarise myself with the syntax. I vaguely understand pointers can cause reference addresses to print on occasion. However, this 7 digit number doesn't appear to be in address format. I've done some searches and can't see a similar question. If there is one, I would appreciate direction to it. I don't wish to break the terms of posting here on the site. Thank you in advance for your assistance.

Upvotes: 0

Views: 38

Answers (2)

Janos
Janos

Reputation: 720

Here is the mistake in your implementation Chair.cpp: newHeight=newHeight;

This is the correct: height = newHeight;

The long number you get is the uninitialized value of member variable height in your Chair object.

Upvotes: 0

Slava
Slava

Reputation: 44258

newHeight=newHeight;

should be replaced with

height=newHeight;

but better you should initialize object in constructor, rather than separate method:

class Chair
{
private:
    int legCount;
    int height;
    bool cushioned;
    string name;
public:
    Chair( const string &newName, int newLegCount, int newHeight, bool cush) :
        legCount( newLegCount ),
        height( newHeight ),
        cushioned( cush ),
        name( newName )
    {
    }
    ...
};

int main()
{
    cout << "Hello world!" << endl;
    Chair c1("Olivia",4,32,true);       // works now
    Chair c2("Stephano",8,8,false);
    return 0;
}

this way you cannot have instance of your class uninitialized and your mistake also would be detected by compiler.

Upvotes: 2

Related Questions