problem with break statemnt and function called from an other function

The program not beaking when the user not entered the correct user name or correct password. so for the break; statement i have put them in while loop. the break is working actully in the main() but in the username and password functions its not. So, I tried calling the password function from the username function, which could solve the problem but its not getting called either.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <conio.h>
#include <string>

using namespace std;

string username ();
string password ();


int main()
{
    string usernm;
    string passwrd;
    string uName = username();
    //string pWord = password();
    while (true)

    {
        cout<< "Enter Username: ";
        cin>>usernm;
        if (usernm==uName)
            {
                string pWord = password();
      [code]
cout<<"Enter the password: ";
cin>>passwrd;
if (passwrd==pWord)
{
cout<<"Access Granted";
}
else
cout<<"Wrong Password. Press any key to exit."<< endl;
break;

}
else
cout<<"Wrong Username. Press any key to exit.";
break;
}
_getch();

}

string username ()

{
string newun;
string uname;
cout<<"Enter a new username: ";
cin>>newun;
cout<<"Re Enter username to confirm: ";
cin>>uname;
while (true)
{
if(newun==uname)
{
cout<<"The new username has been created. \t"<< uname << " \t Congrats." << endl;
return (uname);
string password();
}
else
cout<<"Username mismatch. Try again. Press any key to exit"<< endl;
break;
}

}

string password()

{
string newpw;
string pword;
cout<<"Enter a new password: " ;
cin>>newpw;
cout<<"Re Enter password to confirm: ";
cin>>pword;
if(newpw==pword)
{
cout<<"The new password has been created. Congrats."<< endl;
return (pword);
}
else
cout<<"Password mismatch. Try again. Press any key to exit"<< endl;

}[/code]

error:
Program runs but the program not breaking if entered wrong username while creating.
Last edited on
Consider the following snippet of your code:
1
2
3
4
5
6
7
    if (passwrd==pWord)
    {   cout<<"Access Granted";
    }
    else
        cout<<"Wrong Password. Press any key to exit."<< endl;
			
    break;

Regardless of whether the password matches or not, you're going to exit the while loop. Is that really what you want to do there?

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
Last edited on
Topic archived. No new replies allowed.