D_S_B
D_S_B

Reputation: 208

C++ Member Variables not Updating - Noob here

I've lurked and browsed for a while around here already, but this is my first time posting. Hope I've got the rules and format down pat.

I'm been coding for several months now, so still pretty new at this. Right now, I'm trying to implement a very basic parkingmeter class, with only 2 functions. One to insert quarters, and one to check remaining time. Member variables are maxTime, rate, and time.

I got some functionality up and running, but it kept giving me strange results with my checkTime function. I tested at almost every line, and I realize that after I exit the constructor, the values I entered disappear and are replaced with a really long number. I can't figure out why this is happening. It's not my first time using classes, constructors, instantiating objects, and such, and I don't see what I've done differently this time.

Could any of you experts let me know where I've messed up?

Here's my header file:

#ifndef PARKINGMETER_H_INCLUDED
#define PARKINGMETER_H_INCLUDED

#ifndef PARKINGMETER_H_INCLUDED
#define PARKINGMETER_H_INCLUDED

class ParkingMeter {
private:
    int maxTime;
    double time, rate;

public:
    ParkingMeter();
    ParkingMeter(int, double);
    ~ParkingMeter();
    void insertQtrs(int);
    double checkTime(double);
    double getTime();
    void setTime(double);

};

And here's my implementation:

#include <iostream>
#include <stdexcept>
#include <ctime>
#include "ParkingMeter.h"

using namespace std;

ParkingMeter::ParkingMeter() {       //default constructer
    maxTime = 120;
    rate = .25;
    time = 0;
}

ParkingMeter::ParkingMeter (int maxTime, double rate) {       //constructor
    maxTime = maxTime;
    rate = rate;
    cout<<"maxTime is "<<maxTime<<endl;
    cout<<"rate is "<<rate<<endl;
}

ParkingMeter::~ParkingMeter(){      //destructor
}

void ParkingMeter:: insertQtrs(int quarters){
    ParkingMeter test(this->maxTime, this->rate);
    cout<<"maxTime is "<<test.maxTime<<endl;
    cout<<"rate is "<<test.rate<<endl;
    cout<<"You have inserted: "<<quarters<<" quarters."<<endl;
    double time = quarters * (rate * 60);
    if ( time > 120)
        time = 120;
    this ->setTime(time);

}

double ParkingMeter:: checkTime (double startTime){
    ParkingMeter test(this->maxTime, this->rate);
    double elapsed = clock() - startTime;
 //   test.maxTime = this->maxTime;
    cout<<"test: "<<test.maxTime<<endl;
    cout<<"elapsed time: "<<elapsed<<endl;
    cout<<"meter time: "<<time<<endl;
    cout <<"Your remaining time is: "<< (time - (elapsed / ((double)CLOCKS_PER_SEC)));
}

/*double ParkingMeter:: getTime (){
    int time = this-> maxTime;
    cout<<"time: "<<time<<endl;
    return time;
}*/

void ParkingMeter:: setTime (double time){
    this ->time = time;
}

int main () {
    double maxTime, rate;
    int quarters;
    char y;

    cout<<"Please enter the max parking time and rate, separated by a space: "<<endl;
    cin>>maxTime>>rate;
    ParkingMeter meter(maxTime, rate);
    cout<<"Please enter the amount of quarters you wish to enter: "<<endl;
    cin>>quarters;
    clock_t start = clock();
    meter.insertQtrs(quarters);
    cout<<"Please enter Y to check remaining time: "<<endl;
    cin>>y;

    if (y == 'y'){
       double startTime = start;
        cout<<"starttime: "<<startTime<<endl;
        meter.checkTime (startTime);
    }



}

Upvotes: 0

Views: 5841

Answers (1)

Daniel Frey
Daniel Frey

Reputation: 56863

Here's your problem:

ParkingMeter::ParkingMeter (int maxTime, double rate) {       //constructor
   this->maxTime = maxTime;
   this->rate = rate;
// ^^^^^^ note this!

you might want to use an initializer list, in which case you can (and must) drop the this->. To avoid problems, I suggest you use different names for member variables, e.g., add _:

ParkingMeter::ParkingMeter (int maxTime, double rate)
    : maxTime_( maxTime ), rate_( rate )
{
    cout<<"maxTime is "<<maxTime_<<endl;
    cout<<"rate is "<<rate_<<endl;
}

Upvotes: 1

Related Questions