Jump to content

Help with C++ do...while loop

JustTooDank

So I decided to learn something new, and I decided on C++. Been messing around for a couple hours, and I've encountered a problem I can't understand.

I'm am trying to set up a username/password scenario where it loops on the wrong input, and continues on the right input. The username part works fine, but the password part always fails the first time, and succeeds the second time. I can't figure out why it's failing the first time. 

 

Code:

   const char CorrectUser[9]="TestUser";    string username;     do {     cout<<"Username:\n";    cin>> username;    cin.ignore();     if (username==CorrectUser){        cout<<"Password:\n";    }    else {        cout<<"\nIncorrect username. Please try again.\n\n";    }    } while (username!="TestUser");    cin.get();      const char CorrectPass[9]="TestPass";    string thepassword;     do {     cout<<"Password:\n";    cin>> thepassword;    cin.ignore();     if (thepassword==CorrectPass){        cout<<"\nLogging in...\nCorrect!\n";    }    else {        cout<<"Logging in...Incorrect password. Please try again.\n\n";    }    } while (thepassword!="TestPass");    cin.get(); //End of Login Process

CPU: AMD RYZEN 7 3700x CPU Cooler: AMD Wraith Prism Motherboard: MSI MPG X570 Gaming Plus Memory: Corsair Vengeance RGB Pro 16GB (2x8GB) SSD: Samsung 970 Plus 250GB NVME, WD Blue 2TB m.2, Crucial M500 240GB GPU: EVGA GTX 1080 FTW PSU: Seasonic G-Series 550W CASE: Corsair 220T RGB

Link to comment
Share on other sites

Link to post
Share on other sites

 

So I decided to learn something new, and I decided on C++. Been messing around for a couple hours, and I've encountered a problem I can't understand.

I'm am trying to set up a username/password scenario where it loops on the wrong input, and continues on the right input. The username part works fine, but the password part always fails the first time, and succeeds the second time. I can't figure out why it's failing the first time. 

 

Code:

   const char CorrectUser[9]="TestUser";    string username;     do {     cout<<"Username:\n";    cin>> username;    cin.ignore();     if (username==CorrectUser){        cout<<"Password:\n";    }    else {        cout<<"\nIncorrect username. Please try again.\n\n";    }    } while (username!="TestUser");    cin.get();      const char CorrectPass[9]="TestPass";    string thepassword;     do {     cout<<"Password:\n";    cin>> thepassword;    cin.ignore();     if (thepassword==CorrectPass){        cout<<"\nLogging in...\nCorrect!\n";    }    else {        cout<<"Logging in...Incorrect password. Please try again.\n\n";    }    } while (thepassword!="TestPass");    cin.get(); //End of Login Process
#include <iostream>#include <string>using namespace std;    int main()    {const char CorrectUser[9]="TestUser";    string username;    cout<<"Username:\n";    cin>> username;    cin.ignore();    while (username!="TestUser") {    cout<<"Logging in...Incorrect username. Please try again.\n";    cin >> username;    }    if (username==CorrectUser) {        cout<<"Password:\n";    }    const char CorrectPass[9]="TestPass";    string thepassword;    cin>> thepassword;    cin.ignore();    while (thepassword!="TestPass") {    cout<<"Logging in...Incorrect password. Please try again.\n";    cin >> thepassword;    }    if (thepassword==CorrectPass) {        cout<<"\nLogging in...\nCorrect!\n";    }//End of Login Process}

I used While loops instead of do loops. It works but sorry if it isn't perfect code, i'm learning c++ as well :P

Link to comment
Share on other sites

Link to post
Share on other sites

I used While loops instead of do loops. It works but sorry if it isn't perfect code, i'm learning c++ as well :P

Thanks a bunch dude.

CPU: AMD RYZEN 7 3700x CPU Cooler: AMD Wraith Prism Motherboard: MSI MPG X570 Gaming Plus Memory: Corsair Vengeance RGB Pro 16GB (2x8GB) SSD: Samsung 970 Plus 250GB NVME, WD Blue 2TB m.2, Crucial M500 240GB GPU: EVGA GTX 1080 FTW PSU: Seasonic G-Series 550W CASE: Corsair 220T RGB

Link to comment
Share on other sites

Link to post
Share on other sites

If you look at the order of instructions in your code, you'll realize you're prompting for the username, then doing cin.get, then cin >> thepassword resulting in strange behavior (if you echo the promted password at that point you'll see it's "estPass", the first character is missing.

 

Here's a better version of that code, using c++ variable assignment, and taking out the unnecessary steps, like repeating "TestUser". The point of variables is to use them everywhere, instead of hard coding what they contain!

#include <iostream>using namespace std;int main(){  const string userName("TestUser");  const string userPass("TestPass");  string inputName;  string inputPass; //Declare variables at the top, using C++ syntax for assignment  cout << "Username: \n"; //Prompt User name once  cin >> inputName;  while(inputName != userName) { //the Whole loop will be ignored if the User name is correct    cout << "Incorrect Username, try again \n Username: \n"; //If not prompt again but with error message    cin >> inputName;  }//Once the first looop is exited or ignored, repeat for the password  cout << "Password: \n";  cin >> inputPass;  while(inputPass != userPass) {    cout << "Incorrect Password, try again \n Username: \n";    cin >> inputPass;  }cout << "Login Succesful.";//End of Login Process}

CPU: Intel 3930K @ 4.6GHz || MOBO: Asus P9X79-E WS || GPU: 2x Nvidia GTX Titans || CASE: Fractal Design Define XL R2 || PSU: Corsair AX1200i


RAM: 32GB Dominator Platinum @ 2100 MHz || Storage: 240GB Samsung 840 Pro & 2TB Seagate Barracuda || Sound: Asus Xonar Essence STX


Peripherals: Samsung S27B970D - Asus PB23HQ - Logitech MX Revolution - Corsair K95 - Samson Meteor Mic - Logitech C920 - Bowers&Wilkins MM-1

Link to comment
Share on other sites

Link to post
Share on other sites

Well, considering he's got problems with do..whiles, which are perfect for user input with error detection/verification, why not use them

#include <iostream>using namespace std;int main(){  const string userName("TestUser");  const string userPass("TestPass");  string inputName = "";  string inputPass = ""; //another way of initializing strings  do{     if(inputName != "") //ignored on first pass      cout << "Incorrect Username, try again\n"; //If wrong prompt again but with error message    cout << "Username:\n";     cin >> inputName;  }while(inputName != userName);  do{    if(inputPass != "") //ignored on first pass      cout << "Incorrect Password, try again\n"; //If wrong prompt again but with error message    cout << "Username:\n";    cin >> inputPass;  }while(inputPass != userPass);cout << "Login Succesful.";//End of Login Process}
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

×