Howdy, Stranger!

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

Categories

IM now having to use cin>> twice When...

Thank swampdog2002 and slicer69, your advice helped and that part of my program now works. But now I IM having problems with selecting an invalid input for the choices. When you select something other than 1, 2, or 3 it works but you must type your desired valid input twice before it lets you in. I can see that to cin>> that are causing it but as far as I can tell I need them both. If anyone could help it would be greatly appreciated. My Code is bellow, Thanks.

#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;

if(password != 13245)

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;

}



if(number <= 1 || number >= 3)

do
{
cin>> number;

if(number == 1)

{

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

}

if(number == 2)

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

}

if(number == 3)

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

}

if(number < 1 || number > 3)

do
{
cout<< "sorry, invalid input, please choose 1, 2, or 3.
";
cin>> number;
}
while(number < 1 || number > 3);

}

while(number >= 1 || number <= 3);


}


Comments

  • slicer69slicer69 Member Posts: 272
    I think you forgot to read in your input, after requesting the user select a number. Also, when checking to make sure the user entered a number from 1 to 3, check the input right away, rather than getting them to re-enter later.
    Please see edited code below:

    : #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;
    :
    : if(password != 13245)
    :
    : 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;

    do
    {
    : cout<< "Press 1 for Hotmail, 2 for Programmers Heavem, and 3 for Bleepnet.";
    : cout<< endl;
    cin >> number; // ADD THIS LINE
    } while ( (number < 1) || (number > 3) );
    :
    : }
    :
    :
    :
    : // REMOVE THIS LINE if(number <= 1 || number >= 3)
    :
    : do
    : {
    : // REMOVE THIS LINE cin>> number;
    :
    : if(number == 1)
    :
    : {
    :
    : cout<< "Your Hotmaild Name and Password is Garrett85 - Nothing.
    ";
    :
    : }
    :
    : if(number == 2)
    :
    : {
    : cout<< "Your Name and password for Programmers Heaven is Garrett85 - Nothing.
    ";
    :
    : }
    :
    : if(number == 3)
    :
    : {
    : cout<< "Your Bleepnet name and password is Greg56 - Nothing.
    ";
    :
    : }
    :
    /* Take this code out

    : if(number < 1 || number > 3)
    :
    : do
    : {
    : cout<< "sorry, invalid input, please choose 1, 2, or 3.
    ";
    : cin>> number;
    : }
    : while(number < 1 || number > 3);
    :
    : }
    :
    : while(number >= 1 || number <= 3);
    */
    :
    : }
    :
    :
    :

  • o0obruceleeo0oo0obruceleeo0o Member Posts: 7
    Garret, your code is kind of a mess :D. Email me at o0obruceleeo0o@hotmail.com or contact me on AIM as o0obruceleeo0o and I will help ya out :).
Sign In or Register to comment.