Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Work is below. Problem is I can enter the combination (which is 23, 25, 32) I\'v

ID: 3742541 • Letter: W

Question

Work is below. Problem is I can enter the combination (which is 23, 25, 32) I've set but it'll still tell me I've got the wrong combo. So, where did I go wrong?

lock.h

class lock

{

public:

static const int MAX_NUM = 39;

lock(int combo1, int combo2, int combo3);

void setCombo(int combo1, int combo2, int combo3);

void turn(int number, bool is_clockwise, int turns);

void close();

void try_to_open();

bool is_open() const;

int top() const;

private:

int combo1, combo2, combo3, answer,

recent_num, middle_num, old_num,

recent_spin, middle_spin, old_spin;

int ans;

bool open, recent_clockwisecheck;

int spinlen(int start, int stop, bool is_clockwise, int turns);

};

lock.cpp

#include "lock.h"

lock::lock(int combo1, int combo2, int combo3)

{

open = false;

recent_clockwisecheck = false;

recent_num = 0;

recent_num = middle_num = old_num;

recent_spin = 0;

recent_spin = middle_spin = old_spin;

}

void lock::setCombo(int combo1, int combo2, int combo3)

{

this->combo1 = combo1;

this->combo2 = combo2;

this->combo3 = combo3;

}

void lock::turn(int number, bool is_clockwise, int turns)

{

if (is_clockwise != recent_clockwisecheck)

{

old_spin = middle_spin;

middle_spin = recent_spin;

recent_spin = spinlen(recent_num, number, is_clockwise, turns);

old_num = middle_num;

middle_num = recent_num;

recent_num = number;

recent_clockwisecheck = is_clockwise;

}

else

{

recent_spin = recent_spin + spinlen(recent_num, number, is_clockwise, turns);

recent_num = number;

}

}

void lock::close()

{

open = false;

}

void lock::try_to_open()

{

if (recent_clockwisecheck)

{

if (old_spin >= 40 && middle_spin > 40 && middle_spin > 40 && middle_spin <= 80)

{

if (recent_spin > 0 && recent_spin <= 40)

{

if (old_num == combo1 && middle_num == combo2 && recent_num == combo3)

{

open = true;

}

}

}

}

}

bool lock::is_open() const

{

return open;

}

int lock::top() const

{

return recent_num;

}

int lock::spinlen(int start, int stop, bool is_clockwise, int turns)

{

if (is_clockwise)

{

if (stop >= start)

{

ans = ans + (stop - start);

}

else

{

ans = ans + 40 - (start - stop);

}

}

else

{

if (stop <= start)

{

ans = ans - (start - stop);

}

else

{

ans = ans + 40 - (start - stop);

}

}

return ans;

}

Demoing.cpp

#include "lock.h"

#include <iostream>

using namespace std;

int main()

{

int combo[3];

lock obj(18, 21, 6);

obj.setCombo(23, 25, 32);

cout << "Enter the three numbers of the combination (space between numbers): " << endl << endl;

cin >> combo[0] >> combo[1] >> combo[2];

obj.turn(combo[0], true, 1);

obj.turn(combo[1], false, 1);

obj.turn(combo[2], true, 0);

obj.try_to_open();

if (obj.is_open())

{

cout << "Unlocked";

}

else

{

cout << "Wrong Combination" << endl;

}

return 0;

}

Explanation / Answer

lockdemo.cpp

#include <iostream> // Provides cin and cout
#include <cstdlib>   // Provides EXIT_SUCCESS
#include "lock.h"    // Provides the lock class
using namespace std;
using namespace main_savitch_2;

int main( )
{
    lock my_bike_lock(14, 36, 10);
    int number[3];
  
    cout << "Guess the 3 numbers of the combination: "
   << endl;
    cin >> number[0] >> number[1] >> number[2];

    my_bike_lock.turn(number[0], true, 1);
    my_bike_lock.turn(number[1], false, 1);
    my_bike_lock.turn(number[2], true, 0);

    my_bike_lock.try_to_open( );

    if (my_bike_lock.is_open( ))
   cout << "Right!" << endl;
    else
   cout << "Wrong!" << endl;

    return EXIT_SUCCESS;
}


lock.h

#ifndef LOCK_H
#define LOCK_H

namespace main_savitch_2
{
    class lock
    {
    public:
   // STATIC MEMBER CONSTANT
   static const int SIZE = 40;
   static const int MAX_NUMBER = SIZE-1;
   // CONSTRUCTOR
   lock(int combo1, int combo2, int combo3);
   // MODIFICATION MEMBER FUNCTIONS
   void setcombo(int combo1, int combo2, int combo3);
   void turn(int number, bool is_clockwise, int turns);
   void close( );
   void try_to_open( );
   // CONST MEMBER FUNCTIONS
   bool is_open( ) const;
   int top( ) const;
    private:
   // PRIVATE MEMBER VARIABLES
   int combo1, combo2, combo3;
   int recent_number, middle_number, oldest_number;
   bool recent_is_clockwise;
   int recent_spinlen, middle_spinlen, oldest_spinlen;
   bool open;
   // PRIVATE HELPER FUNCTIONS
   int spinlen(int start, int stop, bool is_clockwise, int turns);
    };
}
#endif
  

lock.cpp

// File: lock.cxx

#include <cassert>
#include "lock.h"
using namespace std;

namespace main_savitch_2
{
    /////////////////////////////////////////////////////////////////////////
    // Static member constants
    const int lock::SIZE;
    const int lock::MAX_NUMBER;
    /////////////////////////////////////////////////////////////////////////


    /////////////////////////////////////////////////////////////////////////
    // CONSTRUCTOR for the lock
    // PRECONDITION: Each of the three parameters is from the range
    // [0..MAX_NUMBER].
    // POSTCONDITION: This lock has been initialized with the specified
    // combination. It's state is as if a person has just made four
    // spins: clockwise stopping at 0, counterclockwise stopping at 0,
    // clockwise stopping at 0, and finally counterclockwise stopping at 0.
    // This lock is closed.
    lock::lock(int combo1, int combo2, int combo3)
    {
   open = false;
   recent_number = middle_number = oldest_number = 0;
   recent_spinlen = middle_spinlen = oldest_spinlen = SIZE;
   recent_is_clockwise = false;
   setcombo(combo1, combo2, combo3);
    }
    /////////////////////////////////////////////////////////////////////////

  
    /////////////////////////////////////////////////////////////////////////
    // void lock::setcombo(int combo1, int combo2, int combo3)
    // PRECONDITION: Each of the three parameters is from the range
    // [0..MAX_NUMBER].
    // POSTCONDITION: This lock has had its combination changed to the
    // specified combination. Otherwise, it's state is unchanged.
    void lock::setcombo(int combo1, int combo2, int combo3)
    {
   assert(0 <= combo1 && combo1 <= MAX_NUMBER);  
   assert(0 <= combo2 && combo2 <= MAX_NUMBER);  
   assert(0 <= combo3 && combo3 <= MAX_NUMBER);  

   this->combo1 = combo1;  
   this->combo2 = combo2;  
   this->combo3 = combo3;
    }
    /////////////////////////////////////////////////////////////////////////

  
    /////////////////////////////////////////////////////////////////////////
    // void lock::turn(int number, bool is_clockwise, int turns)
    // PRECONDITION: number is in the range [0..MAX_NUMBER], and turns >= 0;
    // POSTCONDITION: The state of this lock has changed as if a person
    // spun this lock t full spins and then kept going until the next
    // time that number is at the top of the dial. If isClockwise is true,
    // then the direction of the spin is clockwise; otherwise, the direction
    // is counterclockwise.
    void lock::turn(int number, bool is_clockwise, int turns)
    {
   assert(0 <= number && number <= MAX_NUMBER);
        assert(turns >= 0);

   if (is_clockwise != recent_is_clockwise)
   {   // This spin is in a new direction
        oldest_spinlen = middle_spinlen;
        middle_spinlen = recent_spinlen;
        recent_spinlen
       = spinlen(recent_number, number, is_clockwise, turns);
        oldest_number = middle_number;
        middle_number = recent_number;
        recent_number = number;
        recent_is_clockwise = is_clockwise;
   }
   else
   {   // This spin and the most recent one are same direction
        recent_spinlen
       += spinlen(recent_number, number, is_clockwise, turns);
        recent_number = number;
   }
    }
    /////////////////////////////////////////////////////////////////////////

  
    /////////////////////////////////////////////////////////////////////////
    // void lock::close( )
    // POSTCONDITION: This lock is now closed.
    void lock::close( )
    {
   open = false;
    }
    /////////////////////////////////////////////////////////////////////////
  

    /////////////////////////////////////////////////////////////////////////
    // void lock::try_to_open( )
    // POSTCONDITION: If this lock was already open, then it is still open.
    // If it was closed and the correct sequence of actions to open the
    // lock has occured, then it is now open. Otherwise, it remains closed.
    void lock::try_to_open( )
    {
   if ((recent_is_clockwise)
        &&
        (oldest_spinlen >= SIZE)
        &&
        (middle_spinlen > SIZE)
        &&
        (middle_spinlen <= 2*SIZE)
        &&
        (recent_spinlen > 0)
        &&
        (recent_spinlen <= SIZE)
        &&
        (oldest_number == combo1)
        &&
        (middle_number == combo2)
        &&
        (recent_number == combo3)
       )
       open = true;
    }
    /////////////////////////////////////////////////////////////////////////
  

    /////////////////////////////////////////////////////////////////////////
    // bool lock::is_open( ) const
    // POSTCONDITION: The return value is true if and only if this lock is
    // currently open.
    bool lock::is_open( ) const
    {
   return open;
    }
    /////////////////////////////////////////////////////////////////////////
  

    /////////////////////////////////////////////////////////////////////////
    // int lock::top( ) const
    // POSTCONDITION: The return value is the number that's currently on
    // top of the dial.
    int lock::top( ) const
    {
   return recent_number;
    }
    /////////////////////////////////////////////////////////////////////////
  
    int lock::spinlen(int start, int stop, bool is_clockwise, int turns)
    {
   int answer = SIZE*turns;
  
   if (is_clockwise)
   {   // Adjust the answer for a clockwise spin
        if (stop >= start)
       answer += (stop - start);
        else
       answer += SIZE - (start - stop);
   }
        else
   {   // Adjust the answer for a counter-clockwise spin
        if (stop <= start)
       answer += -(stop - start);
        else
       answer += SIZE + (start - stop);
   }
   return answer;
    }
    /////////////////////////////////////////////////////////////////////////

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote