Jump to content

[noob alert] C++ combination lock debugging

YouRezz

Hi!

So im learning my way around C++ and im having troube with making this code work..

Its a combination PIN lock for accessing the skynet (LOL)

and the lock thing works, and i can go through the pin changing but when goto command puts the code back up again and i access the log in screen, the old code is still valid and it says that the code i entered when i changed it is false.. 

 

 

 

I've written it in SciTe and Borland it compiles with exit code 0 and no warnings.

I've been trying to fix it for hours but it isnt working for me.

 

Thx for help guys :D

#include <iostream.h> int main(){  int pinv;//pin variable  int puk;  int x;//variable for the choice  int pin;  int st;//counter for failed attempts at entering pin  st = 0; //counters preset value  pinv = 1234; //preset pin value here:// goto mark  cout << "Press 1 for access to Skynet\n" << "Press 2 to change the Skynets PIN :" << endl; // intro screen  cin >> x;   while (1) {    if (x == 1) //first choice - Log in the skynet    {      do      {        cout << "Enter the PIN code :";//pin entering code        cin >> pin;        st++;      } while ((st != 3) && (pin != pinv)) ;//do-while loop for making sure pin is correct       if (pin == pinv)//success screen      {        cout << "You have successfully loged into Skynet" << endl;        cout << "ACCESS GRANTED!" << endl;        break;                                     //so the programme ends here if you come this path      }      else//fail screen      {        cout << "ACCESS DENIED!" << endl;        cout << "Authorities have been informed of an unauthorized intrusion in our system" << endl;        break;                                   //so the programme ends here if you come this path      }    }    else {      if (x == 2)//second choice - changing the PIN code      {        do        {          cout << "Enter the PUK code :"; //enter the PUK code screen          cin>>puk;                              //puk input        } while (puk != 123456789) ;         cout << "Enter the previous PIN :"; //enter the old pin        cin>>pin;                                   // old pin input if (pin == pinv)                          //checking if the pin you put in matches the old pin stored in the pinv variable        {          cout<< "Enter the new PIN :" << endl;          cin>>pin;                                         //new pin input           pin = pinv;                                        //setting the pin you just put as the new pin          cout<< "The PIN has been changed!" << endl; //success          goto here;                                         //in my strugle to make it work i had do use this command in order for the programme to reloop        }      }      else {        cout << "Enter a valid number!";            //Fail screen if you put it anything other than 1 or 2 at the begging      }    }  }  return 0;} 

RIG-Processor: Intel core i7 3770k @4.4GHz,Mobo: MSI Z77-G43,GPU:Gigabyte GTX 770, RAM:16 GB G-skill sniper f3,SSD: Corsair Force f3 240gb,HDD: Seagate baracuda 1TB,Cooler:CM Hyper 212 evo, Case: Sharkoon T28 Blue

Peripherals- Monitor: Samsung S24B300, Keyboard: Razer Blackwidow, Mouse: Razer Abyssus, Headphones: Razer Megalodon, Mousepad: Razer Goliathus Alpha, Webcam: Logitech C270,Pad:Logitech F710, Sp: Philips generic ones

#KILLEDMYWIFE #MAKEBOMBS

Link to comment
Share on other sites

Link to post
Share on other sites

WoahWoahWoah, stop, Do not use goto.

Please split your code into functions and call the functions when you want to move around the code. This is C++ not assembly.

Arch Linux on Samsung 840 EVO 120GB: Startup finished in 1.334s (kernel) + 224ms (userspace) = 1.559s | U mad windoze..?

Link to comment
Share on other sites

Link to post
Share on other sites

I've gone ahead and corrected some errors, your braces seemed to be all over the place

Added <stdlib.h> for exit();

Added namespace reference for std (I am guessing you are using VS though, some of these things may have to be removed again)

Added exit option

Changed setting the new pin to directly overwrite the old one.

Calling main() again from the edit screen will reset the required variables, yes, this is possible and useful.

Initiallised x to 0 so that nasty loops don't occur by going straight into one of the options

Reset x and puk in change pin screen

Standardised indentation

#include <iostream>#include <stdlib.h>using namespace std;int main(){    int pinv;//pin variable    int puk;    int x=0;//variable for the choice    int pin;    int st;//counter for failed attempts at entering pin    st = 0; //counters preset value    pinv = 1234; //preset pin value    cout << "Press 1 for access to Skynet\n" << "Press 2 to change the Skynets PIN :" << endl << "Press 3 to quit" << endl; // intro screen    cin >> x;    while (1)    {        if (x == 1) //first choice - Log in the skynet        {            while ((st != 3) && (pin != pinv))            {                cout << "Enter the PIN code :";//pin entering code                cin >> pin;                st++;            }            if (pin == pinv)//success screen            {                cout << "You have successfully loged into Skynet" << endl;                cout << "ACCESS GRANTED!" << endl;                break;                                     //so the programme ends here if you come this path            }            else  //fail screen            {                cout << "ACCESS DENIED!" << endl;                cout << "Authorities have been informed of an unauthorized intrusion in our system" << endl;                break;                                   //so the programme ends here if you come this path            }        }         else if (x == 2)//second choice - changing the PIN code        {            while(puk != 123456789)            {                cout << "Enter the PUK code :"; //enter the PUK code screen                cin>>puk;                              //puk input            }                    cout << "Enter the previous PIN :"; //enter the old pin            cin>>pin;                                   // old pin input            if (pin == pinv)                          //checking if the pin you put in matches the old pin stored in the pinv variable            {                cout<< "Enter the new PIN :" << endl;                cin>>pinv;  //setting the pin you just put as the new                    cout<< "The PIN has been changed!" << endl; //success                puk = 0;                x=0;                st=0;                main();            }        }        else if(x==3)        {            exit(EXIT_SUCCESS);        }        else if(x != 0 || 1 || 2 || 3)        {            cout << "Enter a valid number!";            //Fail screen if you put it anything other than 1 or 2 at the beggin        }    }    return 0;}

Arch Linux on Samsung 840 EVO 120GB: Startup finished in 1.334s (kernel) + 224ms (userspace) = 1.559s | U mad windoze..?

Link to comment
Share on other sites

Link to post
Share on other sites

Thanks @lutzee 

 

I knew i wasnt supposed to use goto but i tried it and it did return to where i wanted it to go. while the infinite while loop didnt work for me... :D

 

I've gone ahead and compiled the code and this is what i get

 

>bcc32.exe  lock.cpp
Borland C++ 5.5.1 for Win32 Copyright © 1993, 2000 Borland
lock.cpp:
Warning W8013 lock.cpp 23: Possible use of 'pin' before definition in function main()
Warning W8013 lock.cpp 45: Possible use of 'puk' before definition in function main()
Error E2120 lock.cpp 62: Cannot call 'main' from within the program in function main()
*** 1 errors in Compile ***
>Exit code: 1
 
How do i fix this error? thx 
should i use another compiler, or devC++??

RIG-Processor: Intel core i7 3770k @4.4GHz,Mobo: MSI Z77-G43,GPU:Gigabyte GTX 770, RAM:16 GB G-skill sniper f3,SSD: Corsair Force f3 240gb,HDD: Seagate baracuda 1TB,Cooler:CM Hyper 212 evo, Case: Sharkoon T28 Blue

Peripherals- Monitor: Samsung S24B300, Keyboard: Razer Blackwidow, Mouse: Razer Abyssus, Headphones: Razer Megalodon, Mousepad: Razer Goliathus Alpha, Webcam: Logitech C270,Pad:Logitech F710, Sp: Philips generic ones

#KILLEDMYWIFE #MAKEBOMBS

Link to comment
Share on other sites

Link to post
Share on other sites

Thanks @lutzee 

 

I knew i wasnt supposed to use goto but i tried it and it did return to where i wanted it to go. while the infinite while loop didnt work for me... :D

 

I've gone ahead and compiled the code and this is what i get

 

>bcc32.exe  lock.cpp

Borland C++ 5.5.1 for Win32 Copyright © 1993, 2000 Borland

lock.cpp:

Warning W8013 lock.cpp 23: Possible use of 'pin' before definition in function main()

Warning W8013 lock.cpp 45: Possible use of 'puk' before definition in function main()

Error E2120 lock.cpp 62: Cannot call 'main' from within the program in function main()

*** 1 errors in Compile ***

>Exit code: 1

 

How do i fix this error? thx 

should i use another compiler, or devC++??

Use a different compiler, what I did was kind of hacky by calling main again. In reality you should really have the main loop test for a variable for exiting. Eg while(var!=true){code} then when you want Part of the code to exit the program set var=true (use a bool for var)

Arch Linux on Samsung 840 EVO 120GB: Startup finished in 1.334s (kernel) + 224ms (userspace) = 1.559s | U mad windoze..?

Link to comment
Share on other sites

Link to post
Share on other sites

Slight modification to lutzee's version:

int main(){    enum loopExitCond    {        FAIL_LOOP = 0,	PASS_LOOP = 1,	EXIT_LOOP = 2    };    loopExitCond loopConditional = PASS_LOOP;    int pinv;//pin variable    int puk;    int x=0;//variable for the choice    int pin;    int st;//counter for failed attempts at entering pin    st = 0; //counters preset value    pinv = 1234; //preset pin value     while ( loopConditional == PASS_LOOP )    {	if (x == 0)	{	  cout << "Press 1 for access to Skynet\n" << "Press 2 to change the Skynets PIN :" << endl << "Press 3 to quit" << endl; // intro screen          cin >> x;	  if( (x < 0) || (x>3) )	  {	      cout << "Invalid Option, try again.\n";	      x = 0;	  }	}        else if (x == 1) //first choice - Log in the skynet        {            while ((st != 3) && (pin != pinv))            {                cout << "Enter the PIN code :";//pin entering code                cin >> pin;                st++;            }            if (pin == pinv)//success screen            {                cout << "You have successfully loged into Skynet" << endl;                cout << "ACCESS GRANTED!" << endl;                loopConditional = EXIT_LOOP;                break;                                     //so the programme ends here if you come this path            }            else  //fail screen            {                cout << "ACCESS DENIED!" << endl;                cout << "Authorities have been informed of an unauthorized intrusion in our system" << endl;                loopConditional = FAIL_LOOP;                break;                                   //so the programme ends here if you come this path            }        }         else if (x == 2)//second choice - changing the PIN code        {            while(puk != 123456789)            {                cout << "Enter the PUK code :"; //enter the PUK code screen                cin>>puk;                              //puk input            }                    cout << "Enter the previous PIN :"; //enter the old pin            cin>>pin;                                   // old pin input            if (pin == pinv)                          //checking if the pin you put in matches the old pin stored in the pinv variable            {                cout<< "Enter the new PIN :" << endl;                cin>>pinv;  //setting the pin you just put as the new                    cout<< "The PIN has been changed!" << endl; //success                puk = 0;                x=0;                st=0;            }        }        else if(x==3)        {            exit(EXIT_SUCCESS);        }        else if(x != 0 || 1 || 2 || 3)        {            cout << "Enter a valid number!";            //Fail screen if you put it anything other than 1 or 2 at the beggin        }    }    return 0;}

You could also add something after the While loop to take advantage of the multiple exit statements, depends on your needs.

 

I also feel the need to say try using more descriptive variables; but I'm glad to see you use lots of comments!

Link to comment
Share on other sites

Link to post
Share on other sites

Thx guys, gonna compile it tommorow as right now im on my phone..

What compiler should i use?

RIG-Processor: Intel core i7 3770k @4.4GHz,Mobo: MSI Z77-G43,GPU:Gigabyte GTX 770, RAM:16 GB G-skill sniper f3,SSD: Corsair Force f3 240gb,HDD: Seagate baracuda 1TB,Cooler:CM Hyper 212 evo, Case: Sharkoon T28 Blue

Peripherals- Monitor: Samsung S24B300, Keyboard: Razer Blackwidow, Mouse: Razer Abyssus, Headphones: Razer Megalodon, Mousepad: Razer Goliathus Alpha, Webcam: Logitech C270,Pad:Logitech F710, Sp: Philips generic ones

#KILLEDMYWIFE #MAKEBOMBS

Link to comment
Share on other sites

Link to post
Share on other sites

Thx guys, gonna compile it tommorow as right now im on my phone..

What compiler should i use?

mingw would probably be the most correct compiler for windows. Though I have no experience in using it, as I am a linux user.

Arch Linux on Samsung 840 EVO 120GB: Startup finished in 1.334s (kernel) + 224ms (userspace) = 1.559s | U mad windoze..?

Link to comment
Share on other sites

Link to post
Share on other sites

mingw would probably be the most correct compiler for windows. Though I have no experience in using it, as I am a linux user.

Holy crap, this mingw is a pain to set up.. setting it up right now :D

RIG-Processor: Intel core i7 3770k @4.4GHz,Mobo: MSI Z77-G43,GPU:Gigabyte GTX 770, RAM:16 GB G-skill sniper f3,SSD: Corsair Force f3 240gb,HDD: Seagate baracuda 1TB,Cooler:CM Hyper 212 evo, Case: Sharkoon T28 Blue

Peripherals- Monitor: Samsung S24B300, Keyboard: Razer Blackwidow, Mouse: Razer Abyssus, Headphones: Razer Megalodon, Mousepad: Razer Goliathus Alpha, Webcam: Logitech C270,Pad:Logitech F710, Sp: Philips generic ones

#KILLEDMYWIFE #MAKEBOMBS

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×