4 Replies - 688 Views - Last Post: 08 March 2010 - 08:26 PM Rate Topic: -----

#1 Birdo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 24-January 10

leaving a function without terminating the program

Posted 05 March 2010 - 10:01 PM

Using Borland's VCL I have developed a punchclock program that lets the user find his name,enter his password and click the Punch_in button. If he enters a name/password mismatch I want him to start over. I can't figure out how to force the user to re-enter a password/name combination if his first try was not a match. Here's the code of my Punch-In button:

//---------------------------------------------------------------------------
void __fastcall TTimeCard::punchINClick(TObject *Sender)
{

         string Searchstr;
    string Passstr;
    typedef map<string, string> mapType;
    mapType data;
    mapType::iterator iter = data.begin();
    // let's declare some initial values to this map
    data["Jay Jeffries"] = "jay";
    data["Walter Flores"] = "walter";
    data["James Guy"] = "james";
    data["Edwin Andrade"] = "edwin";
    data["Richard Cuellar"] = "richard";

    Searchstr = Employee->Text.c_str();
    Passstr = passwd->Text.c_str();


    iter = data.find(Searchstr);

                if (iter->second == Passstr)
                   ShowMessage("Good password");
                else
                   ShowMessage("Bad Password");

    ... more file processing code follows
}



This compiles and the corresponding messages display. But I want to stop when someone enters the wrong password and clear all the textboxes and start over.

Admin Edit: Please use code tags when posting your code. Code tags are used like so => :code:

Thanks,
PsychoCoder :)

Is This A Good Question/Topic? 0
  • +

Replies To: leaving a function without terminating the program

#2 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: leaving a function without terminating the program

Posted 05 March 2010 - 10:02 PM

In a void function, you can exit the function by using:
return;

Was This Post Helpful? 0
  • +
  • -

#3 nmeans73  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 59
  • Joined: 26-December 09

Re: leaving a function without terminating the program

Posted 05 March 2010 - 10:29 PM

Without more of the code my suggestions will be pretty vague. You could use recursion to simply call the function again if the input was bad. If you started with another function, the same process can be used. Along with calling show message, you can also call the function you wish to start with again inside your else statement. Right now you have:

else
                   ShowMessage("Bad Password");



If you simply change it to:

else
{
                   ShowMessage("Bad Password");

                   StartingFunction();
}




You can call both the function to display Bad Password and also call whatever function you want the user to go back to to start the whole process over again.
Was This Post Helpful? 1
  • +
  • -

#4 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 980
  • View blog
  • Posts: 3,397
  • Joined: 19-February 09

Re: leaving a function without terminating the program

Posted 06 March 2010 - 10:34 AM

View PostBirdo, on 06 March 2010 - 06:01 AM, said:

I can't figure out how to force the user to re-enter a password/name combination if his first try was not a match.

This compiles and the corresponding messages display. But I want to stop when someone enters the wrong password and clear all the textboxes and start over.

I don't know about forcing users to do things, perhaps you want an image which is green for good and red for bad.

The design I would change a little, the users use data quite a lot in the program. A more persistent version of the map would be better instead of a local instance.


I have created a little program, with some points explored.

Create a new Project/Form (the default is ok).
Add a ListBox control called Employee.
Add an Edit control called Password.
Add a Button control called PunchIn.

The name of the project and unit files is unimportant just now.
Here they are TC2, yours can be named whatever you like.

H File
Here is the full code for the header file, below that, are instructions to add the map.
TC2.h
//---------------------------------------------------------------------------

#ifndef TC2H
#define TC2H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------------------------------------------
#include <string>
#include <map>
using namespace std;
typedef map<string, string> mapType;
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:	// IDE-managed Components
        TEdit *Password;
        TListBox *Employee;
        TButton *PunchIn;
        void __fastcall PunchInClick(TObject *Sender);
private:	// User declarations
public:		// User declarations
        __fastcall TForm1(TComponent* Owner);
        mapType data;
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif



What has been added.
In the header file (TC2.h) add map typedef.
//---------------------------------------------------------------------------
#include <string>
#include <map>
using namespace std;
typedef map<string, string> mapType;



Add an instance of map called data.
        mapType data;



CPP File
In the cpp file you will find the form constructor function like thus
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}


Add the following code to the function to initialise the map.
    data["Jay Jeffries"] = "jay";
    data["Walter Flores"] = "walter";
    data["James Guy"] = "james";
    data["Edwin Andrade"] = "edwin";
    data["Richard Cuellar"] = "richard";

    mapType::reverse_iterator iter;
    AnsiString tmpStr;
    for(iter = data.rbegin(); iter != data.rend(); iter++) 
            Employee->Items->Add(tmpStr = (*iter).first.c_str());




Double click the Button PunchIn which will create an event function
void __fastcall TForm1::PunchInClick(TObject *Sender)
{
}


Add the following code to the PunchInClick() function.
  AnsiString temp;

  if (Employee->ItemIndex == -1) {
    ShowMessage("Please select name.");
    return;
   }

  temp = Employee->Items->Strings[Employee->ItemIndex];

  mapType::iterator iter;
  iter = data.find(temp.c_str());

  if( Password->Text == (*iter).second.c_str())
    ShowMessage("Found");
  else
    ShowMessage("Not Found");


  Password->Clear();
  Employee->SetFocus();
        



From Employee->ItemIndex we can tell if an ListBox item has been selected and which one.

Password->Clear() will erase text in Edit control although Password->Text = "" would probably work.

Employee->SetFocus() will set the focus to the ListBox, which is what you probably want. Focus generally means which control is ready for input or active.


Here is the full code for the CPP file
/---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop
    
#include "TC2.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
    data["Jay Jeffries"] = "jay";
    data["Walter Flores"] = "walter";
    data["James Guy"] = "james";
    data["Edwin Andrade"] = "edwin";
    data["Richard Cuellar"] = "richard";

    mapType::reverse_iterator iter;
    AnsiString tmpStr;
    for(iter = data.rbegin(); iter != data.rend(); iter++) 
            Employee->Items->Add(tmpStr = (*iter).first.c_str());
}
//---------------------------------------------------------------------------
void __fastcall TForm1::PunchInClick(TObject *Sender)
{
  AnsiString temp;

  if (Employee->ItemIndex == -1) {
    ShowMessage("Please select name.");
    return;
   }

  temp = Employee->Items->Strings[Employee->ItemIndex];

  mapType::iterator iter;
  iter = data.find(temp.c_str());

  if( Password->Text == (*iter).second.c_str())
    ShowMessage("Found");
  else
    ShowMessage("Not Found");


  Password->Text = "";
  Employee->SetFocus();
        
}
//---------------------------------------------------------------------------




Couple of other things.
On the Form select the Edit control (Password).
In the Object Inspector under the Properties tab find the property called PasswordChar, change it to an *.

Select the Form itself by clicking on any blank part of it.
In the Object Inspector under the Properties tab find the property called ActiveControl, choose Employee from the list. This will set the focus to Employee when the program starts, without selecting an item. An item could be selected if you wish.

You can now try compiling and running the finished example program.

You may be able to change the map to typedef map<AnsiString, AnsiString> mapType; which might or might not make things slightly easier, you probably would not need to use c_str(). Though I haven't tested this.
Was This Post Helpful? 0
  • +
  • -

#5 Birdo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 24-January 10

Re: leaving a function without terminating the program

Posted 08 March 2010 - 08:26 PM

I just wanted to thank you in advance. You have been more than helpful. I just haven't had a chance to get back to it. I will though - soon.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1