what should i change ?

Mar 6, 2013 at 5:05pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
using namespace std;

string check(string y){
      if(y=="true"){
                           cout << "access granted" << endl;
                           return 0;
                           }
      else{
           cout << "fail" << endl;
           }}

int main (){
    string x;
    while(1){
             cout << "enter pw: ";
             cin >> x;
             check(x);
             }
    cout << "WELCOME" << endl;
    system("pause");
    return 0;
}


I want to make a check password program with password checking in another function. What should i change in line8 instead of return 0? I want the program to close the loop and resume int main when i enter the correct password.
Thanks
Mar 6, 2013 at 5:14pm
Hi,

Change than to break;

HTH,
Aceix
Last edited on Mar 6, 2013 at 5:15pm
Mar 6, 2013 at 5:18pm
I'd be tempted to make some sort of boolean function that returns whether the password is correct or not.

Here's a quick example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>

const std::string PASSWORD = "qwerty42";

bool ValidPassword( std::string pass )
{
  return pass == PASSWORD;
}

int main( int argc, char* argv[] )
{
  std::string input;
  std::cout << "Please enter password: ";
  std::getline( std::cin, input );

  std::cout << "Password is " << ( ValidPassword( input ) ? "correct\n" : "incorrect\n" );
}
Mar 6, 2013 at 5:25pm
A couple of problems:

Line 8: You shouldn't be specifying an integer (0) as the return value of a function whose return type is string.
Line 12: You don't return anything if the check fails. This really should be a bool function.
1
2
3
4
5
6
7
8
9
10
bool check(string y)
{   if (y=="true")
    {    cout << "access granted" << endl;
         return true;
    }
    else
   {    cout << "fail" << endl;
         return false;
   }
}


Line 19: You don't check whether check() succeeded, so you will loop forever. Your loop should be:
1
2
3
4
    do 
    {   cout << "enter pw: ";
         cin >> x;
    } until (check(x)); 








Mar 6, 2013 at 5:34pm
Yeah,

Your line 4 of the second snippet I think must be }while(check(x));

Thanks,
Aceix.
Mar 6, 2013 at 6:24pm
You're right. That's what happens when one jumps back and forth between languages frequently. :)
Mar 6, 2013 at 8:08pm
1
2
3
4
5
6
7
8
9
10
bool check(string y)
{   if (y=="true")
    {    cout << "access granted" << endl;
         return false;
    }
    else
   {    cout << "fail" << endl;
         return true;
   }
}


i need to invert return true and false, or the program will close when i fail and loop when i input the correct password

Mar 6, 2013 at 8:16pm
I'd leave the true and false alone in check. It's more confusing if the sense of check is reversed. Returning false if it passed is not logical.

I messed up with the "until" in my prior post. That snippet should have been
1
2
3
4
do 
    {   cout << "enter pw: ";
         cin >> x;
    } whiile (! check(x)); 

Note the logical not (!) reversing the sense of check. This will keep the meaning of check straight-forward. true=pass, false=fail. The loop will now continue as long as check returns false.
Topic archived. No new replies allowed.