Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Problems with while and do in C++

[b][red]This message was edited by Garrett85 at 2003-7-9 18:36:28[/red][/b][hr]
Iv written a small no graphics DOS program because thats all I know how to do. The program holds my account names and passwords to various accounts and requires a password to access the information. The program works as far as not leating you in if you type in the wrong password but for some reason you must get the password wrong atleast once. If you type it in right the first time it says it's wrong and type it in again and it will let you in. But getting the password wrong doesnt let you in, that part works. Heres my code, someone care to take a look and tell me what the deal is?

#include
using namespace std;

int main()

{



int password;
int number;


number = 1, 2, 3;

password = 13245;

cout<< "Please enter your password.";
cout<< endl;
cin>> password;

do

{
cout<< "Sorry, the password you enterd is incorrect .";
cout<< endl;
cin>> password;
}

while(password != 13245);

if(password == 13245)

{
cout<< "Select your desierd information.";
cout<< endl;
cout<< "Press 1 for Hotmail, 2 for Programmers Heavem, and 3 for Bleepnet.";
cout<< endl;
}

cin>> number;

if(number == 1)

{

cout<< "Your Hotmaild Name and Password is Garrett85 - Nothing.";
cout<< endl;

}

if(number == 2)

{
cout<< "Your Name and password for Programmers Heaven is Garrett85 - Nothing.";
cout<< endl;

}

if(number == 3)

{
cout<< "Your Bleepnet name and password is Greg56 - Nothing.";
cout<< endl;

}

return 0;
}

Comments

  • swampdog2002swampdog2002 Member Posts: 11
    Garrett85,

    The problem seems to be in the following line of your code:

    [code]

    do

    {
    cout<< "Sorry, the password you enterd is incorrect .";
    cout<< endl;
    cin>> password;
    }

    while(password != 13245); [/code]

    with the [b]do[/b] statement, you are telling the program to enter the loop at least once, regardless if the password is correct or not. To correct this, simply place the following line before the [b]do[/b] statement in your program:

    [code]
    if(password != 13245)

    do

    {
    cout<< "Sorry, the password you enterd is incorrect .";
    cout<< endl;
    cin>> password;
    }

    while(password != 13245);
    [/code]

    I compiled it in Dev C++ and it worked fine for me. Hope this helps.


    : [b][red]This message was edited by Garrett85 at 2003-7-9 18:36:28[/red][/b][hr]
    : Iv written a small no graphics DOS program because thats all I know how to do. The program holds my account names and passwords to various accounts and requires a password to access the information. The program works as far as not leating you in if you type in the wrong password but for some reason you must get the password wrong atleast once. If you type it in right the first time it says it's wrong and type it in again and it will let you in. But getting the password wrong doesnt let you in, that part works. Heres my code, someone care to take a look and tell me what the deal is?
    :
    : #include
    : using namespace std;
    :
    : int main()
    :
    : {
    :
    :
    :
    : int password;
    : int number;
    :
    :
    : number = 1, 2, 3;
    :
    : password = 13245;
    :
    : cout<< "Please enter your password.";
    : cout<< endl;
    : cin>> password;
    :
    : do
    :
    : {
    : cout<< "Sorry, the password you enterd is incorrect .";
    : cout<< endl;
    : cin>> password;
    : }
    :
    : while(password != 13245);
    :
    : if(password == 13245)
    :
    : {
    : cout<< "Select your desierd information.";
    : cout<< endl;
    : cout<< "Press 1 for Hotmail, 2 for Programmers Heavem, and 3 for Bleepnet.";
    : cout<< endl;
    : }
    :
    : cin>> number;
    :
    : if(number == 1)
    :
    : {
    :
    : cout<< "Your Hotmaild Name and Password is Garrett85 - Nothing.";
    : cout<< endl;
    :
    : }
    :
    : if(number == 2)
    :
    : {
    : cout<< "Your Name and password for Programmers Heaven is Garrett85 - Nothing.";
    : cout<< endl;
    :
    : }
    :
    : if(number == 3)
    :
    : {
    : cout<< "Your Bleepnet name and password is Greg56 - Nothing.";
    : cout<< endl;
    :
    : }
    :
    : return 0;
    : }
    :
    :

  • slicer69slicer69 Member Posts: 272
    Your while loop is upside down.
    Prompt the user for thier password, then do the while loop.

    // ask for password
    // get password
    while (password is wrong)
    {
    // ask for password again
    // get password again
    }


    : [b][red]This message was edited by Garrett85 at 2003-7-9 18:36:28[/red][/b][hr]
    : Iv written a small no graphics DOS program because thats all I know how to do. The program holds my account names and passwords to various accounts and requires a password to access the information. The program works as far as not leating you in if you type in the wrong password but for some reason you must get the password wrong atleast once. If you type it in right the first time it says it's wrong and type it in again and it will let you in. But getting the password wrong doesnt let you in, that part works. Heres my code, someone care to take a look and tell me what the deal is?
    :
    : #include
    : using namespace std;
    :
    : int main()
    :
    : {
    :
    :
    :
    : int password;
    : int number;
    :
    :
    : number = 1, 2, 3;
    :
    : password = 13245;
    :
    : cout<< "Please enter your password.";
    : cout<< endl;
    : cin>> password;
    :
    : do
    :
    : {
    : cout<< "Sorry, the password you enterd is incorrect .";
    : cout<< endl;
    : cin>> password;
    : }
    :
    : while(password != 13245);
    :
    : if(password == 13245)
    :
    : {
    : cout<< "Select your desierd information.";
    : cout<< endl;
    : cout<< "Press 1 for Hotmail, 2 for Programmers Heavem, and 3 for Bleepnet.";
    : cout<< endl;
    : }
    :
    : cin>> number;
    :
    : if(number == 1)
    :
    : {
    :
    : cout<< "Your Hotmaild Name and Password is Garrett85 - Nothing.";
    : cout<< endl;
    :
    : }
    :
    : if(number == 2)
    :
    : {
    : cout<< "Your Name and password for Programmers Heaven is Garrett85 - Nothing.";
    : cout<< endl;
    :
    : }
    :
    : if(number == 3)
    :
    : {
    : cout<< "Your Bleepnet name and password is Greg56 - Nothing.";
    : cout<< endl;
    :
    : }
    :
    : return 0;
    : }
    :
    :

Sign In or Register to comment.