help with c++ code if else statement
I made a code to put in the user name id and password then create a new password; if the user name&password are wrong it should come up with a loop saying "invalid user id you can try ___ number of times. Please enter user id: " Then it starts over again so that part works in my code. But when making a new password it should not loop; it should only be 4 digits and can not be the same password of 1234; and this is where i am having a problem the 1234 works and says "Your password could not be changed." but when i put anything over or under 4 digits it doesnt do anything. it should say "Your password could not be changed." and exit but i'm not sure what i'm doing wrong can somebody please help me with this code and tell me how to fix it please!??
#include <iostream>
using namespace std;
int main()
{ //declaration
int pw;
int id;
int i=3;
do{
cout<<"enter six digit id: ";
cin>>id;
cout<<"enter four digit password: ";
cin>>pw;
if(123456==id && 1234==pw)
{
cout<<"Succesful login, please change password.”;
cout<<"Enter new 4-digit password: ";
cin>>pw;
if ((pw >= 1000 && pw <= 9999 && pw != 1234))
{
cout<<"password has been changed."<< endl;
break;
}
else if (pw == 1234 && pw > 1000 && pw < 10000)
cout<<"password could not be changed."<<endl;
break;
}
else
{
cout<<"Invalid user id/password you can try: "<<i<<" more times."<<endl;
if (0==i)
cout<<"Login failed\n";
i--;
}
}while(i>=0);
system("pause");
}
Re: help with c++ code if else statement
Hi wazup6678,
I'm not a professional programmer, but while I was reading through your code, everything looks okay. The only thing I noticed is that your main method is not returning anything. I'm not sure if that will fix it, but I was taught that main always needs a return type. At the bottom of your system("PAUSE"); try adding return 0;
Also, for this section. You are not subtracting i.
Code:
else
{
cout<<"Invalid user id/password you can try: "<<i<<" more times."<<endl;
if (0==i)
cout<<"Login failed\n";
Should be this:
Code:
else
{
cout<<"Invalid user id/password you can try: "<<i<<" more times."<<endl;
i--;
if (i == 0)
cout<<"Login failed\n";
I hope this somewhat helped you. If not, I apologize. I'm still learning myself.
Best of luck,
SubZeroGaming
Re: help with c++ code if else statement
Here you go. I tested it in Visual Studios. Fixed a few errors. Also, for future reference, please, please use conventions.
Works :)
NOTE:
This line here is not 100% how it should work, but the program does its job. This will give you something to fiddle with.
Code:
else if (pw == 1234 && pw > 1000 && pw < 10000)
{
cout<<"password could not be changed."<<endl;
break;
}
Enjoy.
Code:
#include <iostream>
using namespace std;
int main()
{ //declaration
int pw;
int id;
int i=3;
do{
cout<<"enter six digit id: ";
cin>>id;
cout<<"enter four digit password: ";
cin>>pw;
if(123456==id && 1234==pw)
{
cout << "Successful login, please change password.";
cout << "Enter new 4-digit password: ";
cin>>pw;
}
if ((pw >= 1000 && pw <= 9999 && pw != 1234))
{
cout<<"password has been changed."<< endl;
break;
}
else if (pw == 1234 && pw > 1000 && pw < 10000)
{
cout<<"password could not be changed."<<endl;
break;
}
else
{
cout<<"Invalid user id/password you can try: "<<i<<" more times."<<endl;
i--;
if (i == 0)
cout<<"Login failed\n";
}
}
while(i>=0);
system("pause");
return 0;
}
Best of luck,
SubZeroGaming
Re: help with c++ code if else statement
Thanks so much for your help ur awesome!! it worked great :)
Re: help with c++ code if else statement
Glad I could help.
Best of luck,
SubZeroGaming
Re: help with c++ code if else statement
Hmmm. As there is only one chance to change the password after a successful login, why put this code in the main loop? Also there is no checking for incorrect type of input. What if someone enters a Q for the id?
Try this
Code:
#include <iostream>
using namespace std;
const int CORR_ID = 123456;
const int CORR_PASS = 1234;
const int MAX_LOGIN = 3;
int main()
{
int pw,
id,
i = MAX_LOGIN;
bool correct = false,
bad = true;
do {
do {
bad = false;
cout << "Enter six digit id: ";
if (!(cin >> id)) {
cout << "Invalid id entered" << endl;
cin.clear();
cin.ignore(100, '\n');
bad = true;
}
} while (bad == true);
do {
bad = false;
cout << "Enter four digit password: ";
if (!(cin >> pw)) {
cout << "Invalid password entered" << endl;
cin.clear();
cin.ignore(100, '\n');
bad = true;
}
} while (bad == true);
if (CORR_ID != id || CORR_PASS != pw) {
cout << "Incorrect user id/password.";
if (--i > 0) {
cout << " You can try: " << i << " more time(s).";
}
cout << endl;
} else {
correct = true;
}
} while (i > 0 && correct == false);
if (i == 0) {
cout << "Login failed\n";
return 1;
}
cout << "Successful login, please change password." << endl;
do {
bad = false;
cout << "Enter new 4-digit password: ";
if (!(cin >> pw)) {
cout << "Invalid password entered" << endl;
cin.clear();
cin.ignore(100, '\n');
bad = true;
}
} while (bad == true);
if (pw >= 1000 && pw <= 9999 && pw != 1234) {
cout << "Password has been changed."<< endl;
} else {
cout << "Password could not be changed." << endl;
}
return 0;
}