Good Evening!
Having some trouble getting the folowing program to function properly. It compiles perfectly, but does not seem to be functioning. Essentially, the code is supposed to create a virtual combination lock. The lock combo can be reset, the lock can be locked or unlocked, and a new combo can be created.
When I call my lock function(which locks the virtual lock) and then call my isLocked function(which returns 1 if locked and 0 if not), nothing is returned to the console window.
Any idea why?
-----
Also a quick CString question.
If I've got a
char combination[6];
Can I not just assign a value to it like this:
combination = "testtt";
Here's the app:
Code:
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
using namespace std;
class ElectronicLock
{
protected:
char combination[6];
public:
void getCombination()
{
cout << "Enter the combination:";
cin >> combination;
}
void putCombination()
{
cout << "You entered the combination:"
<< combination;
}
virtual void reset() = 0;
virtual int isLocked() = 0;
virtual void openLock(char combo[]) = 0;
virtual void newCombination(char combo[]) = 0;
virtual void lock() = 0;
};
class LockSimulator : public ElectronicLock
{
private:
bool locked;
public:
void reset()
{
// combination = "reset";
}
int isLocked()
{
if(locked == true)
{
return 1;
}
else
{
return 0;
}
}
void openLock(char combo[])
{
if(stricmp(combo,combination) == 0)
{
locked = false;
}
}
void newCombination(char combo[])
{
/*
int intCombo = atoi(combo);
for(int i = 0; i < 6;i++)
{
if(isdigit(intCombo))
{
combination = combo;
}
*/
}
void lock()
{
locked = true;
}
};
int main()
{
bool done = false;
LockSimulator ls;
int choice;
char combo[6];
char newCombo[6];
while(!done)
{
cout << "Enter a selection:" << endl
<< " 1 - Lock" << endl
<< " 2 - Unlock" << endl
<< " 3 - Reset" << endl
<< " 4 - New Combination" << endl
<< " 5 - Is Locked?" << endl
<< " 6 - Exit" << endl;
cin >> choice;
switch(choice)
{
case 1:
ls.lock();
break;
case 2:
cout << "Enter a valid combination:" << endl;
cin >> combo;
ls.openLock(combo);
break;
case 3:
ls.reset();
break;
case 4:
cout << "Enter a new numeric combination:" << endl;
cin >> newCombo;
ls.newCombination(newCombo);
break;
case 5:
ls.isLocked();
break;
case 6:
done = true;
break;
}
}
return 0;
}