My code won't work correctly

View previous topic View next topic Go down

My code won't work correctly Empty My code won't work correctly

Post by CPSNAP April 19th 2015, 8:57 pm

At the end it doesn't work correctly.

#include <iostream>
#include <windows.h>
#include <string>
using namespace std;
//including valueable things ^

int main()
{
string pass;
string user;
int fails = 0;
char YN;
int pen;
string inpass;
string hint;
int done = 0;
int donee = false;
//Variables ^

cout << "If someone geths your password how long do you want them to wait?\n";
cout << "(1 secong = 1000) Seconds: ";
cin >> pen;
//Penilization ^

cout << "Make a username: ";
cin >> user;
Sleep(700);
while (done == 0){
cout << "Make a password: ";
cin >> pass;
Sleep(700);
Sleep(700);
cout << "Sure you want your password to be " << pass << "?\n YN: ";
cin >> YN;
if (YN == 'N'){
done = 0;
cout << "Redo! -->\n";
}
else if (YN == 'Y'){
done = 1;
cout << "Great!\n";
}
else{
cout << "What?\n";
done = 0;
}
}
//creation ^

cout << "Make a hint: ";
cin >> hint;
//The hint ^
Sleep(700);

while (donee == false){
cout << user << endl;
cout << "Password: ";
cin >> inpass;

if (inpass == pass){
cout << "Logging in\n";
donee = true;
}
else{
fails++;
}

if (fails == 3)
hint;
else if (fails == 6)
cout << "Penilizing\n";
Sleep('pen');

}
//Login ^

return 0;
}
CPSNAP
CPSNAP

Posts : 68
Thumbs Up : 9
Join date : 2015-01-18
Age : 23

Back to top Go down

My code won't work correctly Empty Re: My code won't work correctly

Post by lolslayer April 20th 2015, 1:25 am

I would like to help you, but I must to go to school now so maybe later ;D
lolslayer
lolslayer

Posts : 692
Thumbs Up : 29
Join date : 2014-11-10

Back to top Go down

My code won't work correctly Empty Re: My code won't work correctly

Post by bob1ngton April 22nd 2015, 9:03 pm

Well I was able to find the issues, but in the future if you could give some more information about your code such as: language used, and possible errors that were produced by the compiler then it would help those who are looking for bugs. It might not hurt to add more commenting as well as proper spacing between segments.

The first thing that I found was a discrepancy between your variable donee as it was assigned and as it was used. For example you assigned donee as an int when it should have been a boolean so it should have looked like: "bool donee = false;" I would also recommend using boolean for your "done" variable as well.

The next thing was the comparison of the inpass and pass strings. When you use "==" with strings you are only comparing the memory addresses and not the actual strings, so to compare the strings you will use a comparison function and that comparison would look like:  
"if( inpass.compare(pass) ==0)..."  (When comparison function returns 0 the strings are the same)

Here is the corrected code, I took the liberty of cleaning things up if you don't mind.
Code:

#include <iostream>
#include <windows.h>
#include <string>
using namespace std;
//including valueable things ^

int main()
{
    string pass;
    string user;
    int fails = 0;
    char YN;
    int pen;
    string inpass;
    string hint;
    bool done = false;
    bool donee = false;
    //Variables ^

    cout << "If someone gets your password how long do you want them to wait?\n";
    cout << "(1 second = 1000) Seconds: ";
    cin >> pen;
    //Penilization ^

    cout << "Make a username: ";
    cin >> user;
    Sleep(700);

    while (done == false){
        cout << "Make a password: ";
        cin >> pass;

        Sleep(1400);

        cout << "Sure you want your password to be " << pass << "?\n YN: ";
        cin >> YN;
        if (YN == 'N'){
            done = false;
            cout << "Redo! -->\n";
        }
        else if (YN == 'Y'){
            done = true;
            cout << "Great!\n";
        }
        else{
            cout << "What?\n";
            done = false;
        }
    }
    //creation ^



    cout << "Make a hint: ";
    cin >> hint;
    //The hint ^
    Sleep(700);


    while (donee == false){
        cout << user << endl;
        cout << "Password: ";
        cin >> inpass;

        if ( inpass.compare( pass) == 0){
            cout << "Logging in\n";
            donee = true;
        }
        else{
            fails++;
        }

        if (fails == 3){
            cout << "Hint: " << hint << "\n";
        } else if (fails == 6){
            cout << "Penilizing \n";
            Sleep( pen );
            fails = 0;
        }
    }
    //Login ^

    return 0;
}
bob1ngton
bob1ngton

Posts : 3
Thumbs Up : 0
Join date : 2015-04-22

Back to top Go down

My code won't work correctly Empty Re: My code won't work correctly

Post by HatchetHaro April 23rd 2015, 2:56 pm

@bob1ngton
bro...
HatchetHaro
HatchetHaro

Posts : 68
Thumbs Up : 19
Join date : 2013-08-17
Age : 26

Back to top Go down

My code won't work correctly Empty Re: My code won't work correctly

Post by bob1ngton April 23rd 2015, 4:30 pm

If I did something wrong please correct me otherwise I'm not sure what to say. I am just correcting out of information that I have learned over the past few semesters at college.
bob1ngton
bob1ngton

Posts : 3
Thumbs Up : 0
Join date : 2015-04-22

Back to top Go down

My code won't work correctly Empty Re: My code won't work correctly

Post by HatchetHaro April 23rd 2015, 7:31 pm

bob1ngton wrote:If I did something wrong please correct me otherwise I'm not sure what to say. I am just correcting out of information that I have learned over the past few semesters at college.
I mean, like, "bro, you loco"
HatchetHaro
HatchetHaro

Posts : 68
Thumbs Up : 19
Join date : 2013-08-17
Age : 26

Back to top Go down

My code won't work correctly Empty Re: My code won't work correctly

Post by DatGuyYouKnow April 23rd 2015, 10:33 pm

bob1ngton wrote:If I did something wrong please correct me otherwise I'm not sure what to say. I am just correcting out of information that I have learned over the past few semesters at college.
Show me more magic twicks plz
DatGuyYouKnow
DatGuyYouKnow

Posts : 85
Thumbs Up : 20
Join date : 2015-04-12
Age : 26

Back to top Go down

My code won't work correctly Empty Re: My code won't work correctly

Post by lolslayer April 24th 2015, 2:02 am

why is everybody like holy shit here xD

I mean, it's very cool and I can't do better, but a normal student in the last grade can do this xDDD
lolslayer
lolslayer

Posts : 692
Thumbs Up : 29
Join date : 2014-11-10

Back to top Go down

My code won't work correctly Empty Re: My code won't work correctly

Post by Mootjuh April 24th 2015, 3:30 am

I think this is more basic tban last grade.
Mootjuh
Mootjuh
Developer

Posts : 597
Thumbs Up : 132
Join date : 2014-10-30
Age : 28

Back to top Go down

My code won't work correctly Empty Re: My code won't work correctly

Post by bob1ngton April 24th 2015, 3:43 am

I totally agree, what I did to the code was just basic formatting and logic. You don't really start to get into coding until you deal with object oriented design in addition to advanced data structures.
bob1ngton
bob1ngton

Posts : 3
Thumbs Up : 0
Join date : 2015-04-22

Back to top Go down

My code won't work correctly Empty Re: My code won't work correctly

Post by HatchetHaro April 24th 2015, 11:23 am

I know, I could probably do the same thing but 1. I don't know the language, and 2. I'm lazy.
HatchetHaro
HatchetHaro

Posts : 68
Thumbs Up : 19
Join date : 2013-08-17
Age : 26

Back to top Go down

My code won't work correctly Empty Re: My code won't work correctly

Post by Sponsored content


Sponsored content


Back to top Go down

View previous topic View next topic Back to top

- Similar topics

Permissions in this forum:
You cannot reply to topics in this forum