3 Replies - 1063 Views - Last Post: 22 February 2017 - 07:46 PM Rate Topic: -----

#1 amandaorhevba   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 22-February 17

HOW CAN YOU RERUN A PROGRAM IN C++ WITHOUT CLOSING IT

Posted 22 February 2017 - 02:13 PM

im writing a code in C++ to collect username and password from a user how do i design the program to prompt the user to re input values if the first try was unsuccesful and for the program to accept the new input and run the original function again
Is This A Good Question/Topic? 0
  • +

Replies To: HOW CAN YOU RERUN A PROGRAM IN C++ WITHOUT CLOSING IT

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14175
  • View blog
  • Posts: 56,789
  • Joined: 12-June 08

Re: HOW CAN YOU RERUN A PROGRAM IN C++ WITHOUT CLOSING IT

Posted 22 February 2017 - 02:27 PM

Typically a while loop would work.
Was This Post Helpful? 0
  • +
  • -

#3 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11096
  • View blog
  • Posts: 18,982
  • Joined: 19-March 11

Re: HOW CAN YOU RERUN A PROGRAM IN C++ WITHOUT CLOSING IT

Posted 22 February 2017 - 02:31 PM

Use a loop:

while (not_done) {
  do_the_thing();
  maybe_update_done();
}

Was This Post Helpful? 0
  • +
  • -

#4 UG Cyber   User is offline

  • D.I.C Addict

Reputation: 36
  • View blog
  • Posts: 611
  • Joined: 24-July 08

Re: HOW CAN YOU RERUN A PROGRAM IN C++ WITHOUT CLOSING IT

Posted 22 February 2017 - 07:46 PM

while(checkLogin() != SUCCESS){
  askForLogin();
}



This is a part of a login code that i use on my commercial development

/*...some function..*/
 while(true){
        int ret = chkLogin();
        if(ret == 0){
            loadLogin();
        }else if(ret == 1){
            break;
        }else if(ret == 2){
            closeWindow = true;
            break;
        }
    }

bool    MainWindow::    loadLogin(){
    ifstream fin("password.file", ios::binary);
    string postPacket;
    if(!fin){
        return false;
    }
    string file;
    getline(fin,file);
    fin.close();
    StringList pFrags = splitString(file, ':');
    if(pFrags.size() < 4){
        return false;
    }
    string user = decryption(pFrags[0][0], pFrags[1]);
    string pass = decryption(pFrags[0][0], pFrags[2]);
    if(!pFrags[3].compare("false")){
        SYSTEMTIME ctime;
        GetSystemTime(&ctime);

        char dummy[10];
        memset(dummy, 0, 10);
        string date(itoa(ctime.wMonth,dummy, 10));
        memset(dummy, 0, 10);
        itoa(ctime.wDay, dummy, 10);
        date.append(".");date.append(dummy);
        if(date.compare(pFrags[4])){
            return false;
        }
    }
    //Attempt Login
    USER = user;
    PASS = pass;

    return true;
}
int     MainWindow::    chkLogin(){
    QString msg = sendPacket("com=clientLogin");
    if(!strcmp(msg.toStdString().c_str(), "Login Accepted")){
        /*do tasks*/
        return 1;
    }else{
        loginDialog ld;
        ld.setMessage(msg);
        int ret = ld.exec();
        if(ret == -1){
            return 2;
        }
        return 0;
    }
}



May not be the most secure in the world, but it works just fine for my tasks.

This post has been edited by UG Cyber: 22 February 2017 - 07:50 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1