Function calls

This is a discussion on Function calls within the C++ Programming forums, part of the General Programming Boards category; Good Evening! Having some trouble getting the folowing program to function properly. It compiles perfectly, but does not seem to ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    9

    Function calls

    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;
    }

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    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";

    No, you cannot because the first thing that happens is the string literal "testtt" is stored in memory and a '\0' is tacked on the end, making the total length 7. The address is then assigned to combination, but combination is defined to be an array with a length of 6(remember array names act like pointers, so combination is a pointer to an array of length 6), so you will get an error that says:

    cannot convert from 'char [7]' to 'char [6]'

    The same thing would happen if you had declared combination to have a length of 100. The lengths have to be exact.

    You could do this:

    combination = "testt";

    (The only glitch in this explanation(I hope) is that the first dimension of an array is not supposed to be part of the type, so addresses of char arrays of any size can be assigned to a char pointer. Like this:

    char text[3] ="at";
    char word[5] ="rock";
    char* ptest =0;

    ptest = text;
    cout<<ptest<<endl;

    ptest = word;
    cout<<ptest<<endl;


    which might lead you to conclude that since the array name is a pointer it shouldn't matter what size array the address you assign to it is. However, an array name isn't exactly a pointer, but it does act like a pointer in many cases, so maybe that's the reason.)

    Here is your isLocked() problem:

    ls.isLocked();

    isLocked() returns a 0 or 1 right? Well, you don't assign the 0 or 1 to a variable so it disappears into thin air. Remember, your isLocked() function doesn't use "cout<<" so nothing is displayed to the screen within the function, and as you can plainly see, you don't use "cout<<" anwhere after the function returns.
    Last edited by 7stud; 04-09-2003 at 08:06 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accurately detecting function calls
    By Mole42 in forum C Programming
    Replies: 5
    Last Post: 05-17-2009, 05:01 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  5. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 06:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21