Username & password check fail

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 6083 Views - Last Post: 02 January 2011 - 12:04 PM Rate Topic: -----

#1 busta5000  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: -7
  • View blog
  • Posts: 147
  • Joined: 08-March 08

Username & password check fail

Posted 01 January 2011 - 05:25 AM

Here starts the problem by checking the username & password the result it always gives me incorrect & sends me to main menu
               if((userreg == username) && (passreg == password))
                           {
                    cout << "Welcome\t"<<firstname<<"\n\n";
                    cout <<"Select your choice?\n\n";
                    cout << "1.Start the test.\n\n2.Instructions.\n\n";
                    cin >> n;
                    system("cls");
                     //going to the functions
                     switch(n)
                        {
                     case 1:
                     questions(grade);
                       break;
 
                        case 2:
 
cout <<"\t*******************Instructions*******************\n";
cout<<"________________________________________________________________________________\n";
cout<<"\n\n* This test will give you 10 questions that might randomly include addition, \nsubtraction, multiplication & devision.\n";
cout<<"\n\n* Every question is graded & then your total grade will be viewed if you failed \nor passed.\n\n";
cout<<"\n* You have 3 attempts.\n\n";
cout<<"____________________________\n";
cout<<"\n1.Menu.\n\n2.Start the test.\n\n3.Exit\n";
cout<<"____________________________\n\n";
cout<<"->";
cin>>n;
system("cls");
if (n==1)
{
goto start;
}
if (n==2)
{
questions(grade);
}
 
if (n==3)
{
return 0;
}
 
break;
case 3:
return 0;
break;
}
 
}
else if ((userreg != username && passreg != password))
{
cout << "Username/Password is Incorrect or might not exist.\n\n";
cout << "Please Restart The Program.";
   goto start;
 
    }





The Entire code
#include <iostream>
#include <cmath>
#include <cstdlib>
#include<ctime>
#include <string>
 
using namespace std;
int questions(int);
int main()
{
    int n = 0;
    int grade = 0;
    int test = 0;
 
    char username[25];         
    char password[25];        
    char userreg[25];        //username check
    char passreg[25];       //password check
    char firstname[25];
    char lastname[25];
    char email[25];
 system("color 2B");
 
    start:   //goto goes here Also this is the main menu
    system("cls");
    cout<<"===============================================================================\n";
    cout<<"\t\t\t\t Education Mathmatical Exam\n";
    cout<<"===============================================================================";
    cout<<"\n\nWelcome to educational mathmatics exam";
    cout<<"\n\n1.Log in.\n\n2.register.\n\n3.Credit.\n\n";
    cout<<"->";
    cin>>n;
    system("cls");
    if (n==3)
        {
        cout<<"\n\nThis project has been done by:\n\nHassan Abdulrahman Al Hussaini 005001490\n\nMohd Waheed Ali 007000698\n\nkhalifa hamad almanni 009001344\n\nMohammad Amer Schweinsteiger 008000180";
        cout<<"\n\n1.Menu.\n\n2.Exit\n\n";
        cin>>n;
        if (n==1)
        {
                goto start;
        }
        if (n==2)
        {
return 0;
        }
        }
        if (n == 2)
        {
            
                        //Registration screen
            
            system("cls");
            cout<<"*******************Registration*******************\n\n";
            cout<<"Enter Your First Name: ";
            cin>>firstname;
            cout<<"Enter Your Last Name: ";
            cin>>lastname;
            cout<<"Enter Your E-Mail Adress: ";
            cin>>email;
            cout<<"\nEnter Your Username: ";
            cin >> userreg;
            cout << "Enter Your Password: ";
            cin >> passreg;
            cout << "\nRegistration Succesful\n\n";
            cout << "Please Sign In.\n";
            system("cls");
            
                        
                        //member login
                        Login:
            system("cls");
            cout << "*******************Login*******************\n";
            cout << "\n";
            cout << "Username: ";
            cin >> username;
            cout << "Password: ";
            cin >> password;
            cout << "\n";
            system("cls");
            //verifies the user name & password by using string length function

               if((userreg == username) && (passreg == password))
			   {
                    cout << "Welcome\t"<<firstname<<"\n\n";
                    cout <<"Select your choice?\n\n";
                    cout << "1.Start the test.\n\n2.Instructions.\n\n";
                    cin >> n;
                    system("cls");
                     //going to the functions
                     switch(n)
                        {
                     case 1:
                     questions(grade);
                       break;
 
                        case 2:

cout <<"\t*******************Instructions*******************\n";
cout<<"________________________________________________________________________________\n";
cout<<"\n\n* This test will give you 10 questions that might randomly include addition, \nsubtraction, multiplication & devision.\n";
cout<<"\n\n* Every question is graded & then your total grade will be viewed if you failed \nor passed.\n\n";
cout<<"\n* You have 3 attempts.\n\n";
cout<<"____________________________\n";
cout<<"\n1.Menu.\n\n2.Start the test.\n\n3.Exit\n";
cout<<"____________________________\n\n";
cout<<"->";
cin>>n;
system("cls");
if (n==1)
{
goto start;
}
if (n==2)
{
questions(grade);
}
 
if (n==3)
{
return 0;
}
 
break;
case 3:
return 0;
break;
}
 
}
else if ((userreg != username && passreg != password))
{
cout << "Username/Password is Incorrect or might not exist.\n\n";
cout << "Please Restart The Program.";
   goto start;
 
    }
 cin>>n;
 
if (n==1) goto start;
 
 if(n==2)
 {
 return 0;
}
 
return 0;
} 
        if (n==1)
 {
         goto Login;
 }
}
int questions(int mark)
{
 
int x;
int y;
int test = 0;
int caserand = 0;
int ans;
int q=1;
cout<<"\n__________________________________\n";
 
 
for (test=0; test<10; test++)  //here it puts a limit to 10 questions
{
for (q=1;q<=10;q++){
cout<<"Question"<<q<<":";
srand((unsigned int)time(0));   //the reason I used unsigned int is because of the loss of data
caserand=(1+rand()%4);
//q represents question title example 1. 2. 3. etc...
 
// Here starts the questions
switch (caserand) {
 
case 1:
//adittion
srand((unsigned int)time(0));
x = (1+rand()%50);
y = (1+rand()%50);
cout<<"\n"<<x<<"+"<<y<<"=";
cin>>ans;
 
if (ans == x + y){
mark = mark + 1;
cout<<"\nCorrect\n"<<endl;
cout<<"\n__________________________________\n";
break;
}
else{
cout<<"\nwrong answer please try again (2 attempts left)\n"<<endl;
}
cin>>ans;
if (ans == x + y){
mark = mark + 1;
cout<<"\nCorrect\n"<<endl;
cout<<"\n__________________________________\n";
break;
}
else{
cout<<"\nWrong answer please try again (1 attempt left)\n"<<endl;
}
cout<<"->";
cin>>ans;
if (ans == x + y){
mark = mark + 1;
cout<<"\nCorrect\n"<<endl;
cout<<"\n__________________________________\n";
break;
}
else{
cout<<"\nanswer is incorrect\n"<<endl;
cout<<"\n__________________________________\n";
break;
}
cout<<"\n__________________________________\n";
 
case 2:
        //subtraction
srand((unsigned int)time(0));
x = (1+rand()%50);
y = (1+rand()%50);
cout<<"\n"<<x<<"-"<<y<<"=";
cin>>ans;
 
if ( ans == x - y){
mark = mark + 1;
cout<<"\nCorrect\n"<<endl;
cout<<"\n__________________________________\n";
break;
}
else{
cout<<"Wrong Answer please try again(2 attempts left)\n";
 
}
cin>>ans;
if ( ans == x - y){
mark = mark + 1;
cout<<"\nCorrect\n"<<endl;
cout<<"\n__________________________________\n";
break;
}
else{
cout<<"\nWrong Answer please try again(1 attempt left)\n"<<endl;
 
}
cout<<"->";
cin>>ans;
if ( ans == x - y){
mark = mark + 1;
cout<<"\nCorrect\n"<<endl;
cout<<"\n__________________________________\n";
}
else{
cout<<"\nanswer is incorrect\n"<<endl;
cout<<"\n__________________________________\n";
break;
}
cout<<"\n__________________________________\n";
 
case 3: 
//multiplication
srand((unsigned int)time(0));
x = (1+rand()%50);
y = (1+rand()%50);
cout<<"\n"<<x<<"*"<<y<<"=";
cin>>ans;
if(ans == x * y){
mark = mark + 1;
cout<<"\nCorrect\n"<<endl;
cout<<"\n__________________________________\n";
break;
}
else{
cout<<"\nWrong answer please try again (2 attempts left)\n=";
}
cout<<"->";
cin>>ans;
if(ans == x * y){
mark = mark + 1;
cout<<"\nCorrect\n"<<endl;
cout<<"\n__________________________________\n";
break;
}
else{
cout<<"\nWrong answer please try again(1 attempt left)\n=";
}
cout<<"->";
cin>>ans;
if(ans == x * y){
mark = mark + 1;
cout<<"\nCorrect\n"<<endl;
cout<<"\n__________________________________\n";
break;
}
else{
cout<<"\nanswer is incorrect\n"<<endl;
cout<<"\n__________________________________\n";
 break;
}
cout<<"\n__________________________________\n";
 
case 4:
//division
div:
srand((unsigned int)time(0));
x = (1+rand()%50);
y = (1+rand()%50);
if (y<x){
cout<<"\n"<<x<<"/"<<y<<"=";
cin>>ans;
}
if (y>x){
goto div;
}
 
if (ans == x / y){
mark = mark + 1;
cout<<"Correct\n"<<endl;
cout<<"\n__________________________________\n";
break;
}
else{
cout<<"Wrong answer please try again (2 attempts left)\n"<<endl;
cout<<"\n__________________________________\n";
}
cout<<"->";
cin>>ans;
if (ans == x / y){
mark = mark + 1;
cout<<"Correct\n"<<endl;
cout<<"\n__________________________________\n";
}
else{
cout<<"Wrong answer please try again (1 attempt left)\n"<<endl;
cout<<"\n__________________________________\n";
}
cout<<"->";
cin>>ans;
if (ans == x / y){
mark = mark + 1;
cout<<"Correct\n"<<endl;
cout<<"\n__________________________________\n";
}
else{
cout<<"answer is incorrect\n"<<endl;
cout<<"\n__________________________________\n";
break;
}
cout<<"\n__________________________________\n";
}
}
if (q>10){
goto grade;
}
 
}
 
//Gradeing Procces
grade:
system("cls");
cout<<"\n^^^/>^^^/>^^^/>^^^/>^^^/>^^^/>^^^/>^^^/>^^^/>^^^/>^^^/>^^^/>^^^/>^^^/>^^^/>^^^/>^^^/>^^^/>^^^/>^^^/>^^^/>^^^/>^^^/>^^^/>^^^/>^^^/>^^\n\n";
cout<<"your grade is:%"<<mark*10;
 
if (mark<60){
cout<<"\n\nToo bad, you Failed!\n\n";
cout<<"---------------------------------\n";
cout<<"|                               |\n";
cout<<"|        X              X       |\n";
cout<<"|                       o       |\n";
cout<<"|                       o       |\n";
cout<<"|           -----------         |\n";
cout<<"|         -             -       |\n";
cout<<"|      -                 -      |\n";
cout<<"|                               |\n";
cout<<"---------------------------------\n";
}
 
if (69<=mark){
cout<<"\n\nYou barely Passed with a D\n\n";
cout<<"---------------------------------\n";
cout<<"|                               |\n";
cout<<"|        @              @       |\n";
cout<<"|                       o       |\n";
cout<<"|                               |\n";
cout<<"|          ____________         |\n";
cout<<"|                               |\n";
cout<<"|                               |\n";
cout<<"|                               |\n";
cout<<"---------------------------------\n";
 
}
 
if (79<=mark){
cout<<"\\nnNot bad you got a C\n\n";
cout<<"---------------------------------\n";
cout<<"|                               |\n";
cout<<"|        @              @       |\n";
cout<<"|                               |\n";
cout<<"|                               |\n";
cout<<"|      -                 -      |\n";
cout<<"|        ---------------        |\n";
cout<<"|                               |\n";
cout<<"|                               |\n";
cout<<"---------------------------------\n";
}
 
if (89<=mark){
cout<<"\\nnvery Nice, you got a B\n\n";
cout<<"---------------------------------\n";
cout<<"|                               |\n";
cout<<"|        @              @       |\n";
cout<<"|                               |\n";
cout<<"|                               |\n";
cout<<"|   -                        -  |\n";
cout<<"|      -                    -   |\n";
cout<<"|         -               -     |\n";
cout<<"|          ---------------      |\n";
cout<<"---------------------------------\n";
}
 
if (90<=mark){
cout<<"\n\nPerfect, you got an A!!!!!\n\n";
cout<<"---------------------------------\n";
cout<<"|                               |\n";
cout<<"|        @              @       |\n";
cout<<"|                               |\n";
cout<<"|                               |\n";
cout<<"|    -                        - |\n";
cout<<"|      -                     -  |\n";
cout<<"|         -                -    |\n";
cout<<"|          ---------------      |\n";
cout<<"---------------------------------\n";
 
}
cout<<"\n^^^/>^^^/>^^^/>^^^/>^^^/>^^^/>^^^/>^^^/>^^^/>^^^/>^^^/>^^^/>^^^/>^^^/>^^^/>^^^/>^^^/>^^^/>^^^/>^^^/>^^^/>^^^/>^^^/>^^^/>^^^/>^^^/>^^";
cout<<"____________________________\n\n";
cout<<"\n\n1.Menu.\n\n2.Exit.\n";
cout<<"->";
return 1;
}   



Is This A Good Question/Topic? 0
  • +

Replies To: Username & password check fail

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5960
  • View blog
  • Posts: 23,235
  • Joined: 23-August 08

Re: Username & password check fail

Posted 01 January 2011 - 05:54 AM

First, your repeated use of goto is enough to make me want to vomit. Who the hell is teaching you this???

Second, you cannot compare C-strings -- which you are using instead of the C++ strings you should be using -- like this:
if((userreg == username) && (passreg == password))


You MUST use strcmp!
Was This Post Helpful? 0
  • +
  • -

#3 busta5000  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: -7
  • View blog
  • Posts: 147
  • Joined: 08-March 08

Re: Username & password check fail

Posted 01 January 2011 - 06:16 AM

View PostJackOfAllTrades, on 01 January 2011 - 04:54 AM, said:

First, your repeated use of goto is enough to make me want to vomit. Who the hell is teaching you this???

Second, you cannot compare C-strings -- which you are using instead of the C++ strings you should be using -- like this:
if((userreg == username) && (passreg == password))


You MUST use strcmp!


I'm sorry if I used goto many times but it would be more sick if I repeated multiple lines many times!!! like login then I type login again in an if statement etc.


regarding strcmp I did try it even before posting to dream in code trying to fix it but it always gives me this error


Quote

error C2661: 'strcmp' : no overloaded function takes 1 arguments


I did type the code this way

if(strcmp(userreg == username) && (strcmp(passreg == password)))



but I don't know how to declare strcmp I did try

int strcmp(char username[25]);         
int strcmp(char password[25]);        
int strcmp(char userreg[25]);        //username check
int strcmp(char passreg[25]);       //password check
int strcmp(char firstname[25]);
int strcmp(char lastname[25]);
int strcmp(char email[25]);



BTW I did not study strcmp I'm learning it online our instructor said use any type of code that we did not study to do education with registration etc.
Was This Post Helpful? 0
  • +
  • -

#4 Hezekiah  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 207
  • View blog
  • Posts: 550
  • Joined: 12-July 09

Re: Username & password check fail

Posted 01 January 2011 - 06:28 AM

Use strcmp() like this:
if(strcmp(userreg, username) && strcmp(passreg, password))

Edit: Oops, got the return value wrong - see Huzi94's post.

This post has been edited by Hezekiah: 01 January 2011 - 06:40 AM

Was This Post Helpful? 1
  • +
  • -

#5 Huzi94  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 119
  • Joined: 14-November 09

Re: Username & password check fail

Posted 01 January 2011 - 06:32 AM

This is how you use strcmp()

#include <iostream>
#include <cstring>

using namespace std;

int main() {
    char username[25] = "blahblah";
    char userreg[25] = "blahblahd";
    if (strcmp(userreg, username) == 0) {
        cout << "Success" << endl;
    }
    else {
        cout << "Failure" << endl;
    }
    cin.get();
}



The strcmp() functions starts comparing the first string(userreg) with the second string(username).
A zero value returned, indicates that both the strings (userreg and username) are equal. Hope this solves your problem.

For more info http://www.cplusplus...cstring/strcmp/

This post has been edited by Huzi94: 01 January 2011 - 06:36 AM

Was This Post Helpful? 4
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5960
  • View blog
  • Posts: 23,235
  • Joined: 23-August 08

Re: Username & password check fail

Posted 01 January 2011 - 06:35 AM

*
POPULAR

Read about functions in my signature and use them -- and loops -- instead of goto. There is next to NO APPROPRIATE USE for goto except laziness in determining how to split your program up into the appropriate data and control structures.
Was This Post Helpful? 5
  • +
  • -

#7 busta5000  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: -7
  • View blog
  • Posts: 147
  • Joined: 08-March 08

Re: Username & password check fail

Posted 01 January 2011 - 06:52 AM

Thank you Huzi94 for explaining how comparison is suppose to work I thought I should use == because most sites compare using ==! It did work exactly how I planned



JackOfAllTrades thank you for suggesting that I use other methods other than goto I will use other methods from your references. But I have an advise for you please be less aggressive next time.

edit:sited -> sites

This post has been edited by busta5000: 01 January 2011 - 06:54 AM

Was This Post Helpful? 0
  • +
  • -

#8 Huzi94  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 119
  • Joined: 14-November 09

Re: Username & password check fail

Posted 01 January 2011 - 07:09 AM

View Postbusta5000, on 01 January 2011 - 03:52 PM, said:

Thank you Huzi94 for explaining how comparison is suppose to work I thought I should use == because most sites compare using ==! It did work exactly how I planned


You are welcome.
Was This Post Helpful? 0
  • +
  • -

#9 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 988
  • View blog
  • Posts: 5,135
  • Joined: 28-September 06

Re: Username & password check fail

Posted 01 January 2011 - 07:13 AM

*
POPULAR

Two important issues here.

1 - Don't use strcmp().
Instead of using strcmp to compare C-strings make the step forward and use C++ strings so you don't need to use strcmp().
Using strcmp is solving the wrong problem.
The problem here, as stated right at the beginning of this thread is that you should not be using C-strings at all. You should be using C++ strings.
If you use C++ strings then you can simply use "==" to compare one string to another.
The right solution is to drop C-strings and drop strcmp() and use C++ strings instead.
Read more here:
http://www.cplusplus...ference/string/
and here
http://www.cplusplus...rial/variables/
(scroll down to "Introduction to strings")

2 - Don't use goto.
Let's be very clear. Unless you are a lot older than I guess you are then goto has been a deprecated idea longer than you have been alive
"The 1960s and 1970s saw computer scientists move away from GOTO statements in favor of the "structured programming" paradigm."
"Probably the most famous criticism of GOTO is a 1968 letter by Edsger Dijkstra called Go To Statement Considered Harmful.[4] "
"Some programmers, such as Linux Kernel designer and coder Linus Torvalds or software engineer and book author Steve McConnell also object to Dijkstra's point of view, stating that GOTOs can be a useful language feature, improving program speed, size and code clearness, but only when used in a sensible way by a comparably sensible programmer."
http://en.wikipedia....ism_and_decline

Now read this
http://en.wikipedia....red_programming

The key work is this one
O.-J. Dahl, E. W. Dijkstra, C. A. R. Hoare Structured Programming, Academic Press, London, 1972 ISBN 0-12-200550-3
this volume includes an expanded version of the Notes on Structured Programming, above, including an extended example of using the structured approach to develop a backtracking algorithm to solve the 8 Queens problem.
a pdf version is in the ACM Classic Books Series
Note that the third chapter of this book, by Dahl, describes an approach which is easily recognized as Object Oriented Programming. It can be seen as another way to "usefully structure" a program to aid in showing that it is correct.

This book is so old and its contents so accepted as just "the way to program" that you will be hard pressed to find a copy unless it is some scanned PDF. It is not in current print because no-one needs to be told the things that it says, except, apparently, whoever is teaching you to program as though 1972 is in the future instead nearly 40 years in the past.

I have provided all these links not for you but for you to pass to whoever is teaching you (I would suggest doing so anonymously to avoid a confrontation with someone who is obviously a fool).

It is negligence of the first order to be be teaching goto in C/C++ the 21st century. When and if you are able to mount a comprehensive and compelling argument against Dijkstra's work you can do so. That's a PhD level thesis. No undergraduate or lower should be taught to use goto and any assignment using it should receive an automatic fail. When you have shown you have complete mastery of structured programming then if you feel compelled to use goto and have the standing of a Torvalds or a McConnell you can give it a try. Do expect to be challenged at every turn even then. Until you have a global standing in the programming community structured programming is your only reasonable path forward.

This post has been edited by janotte: 01 January 2011 - 07:16 AM

Was This Post Helpful? 6
  • +
  • -

#10 Huzi94  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 119
  • Joined: 14-November 09

Re: Username & password check fail

Posted 01 January 2011 - 07:26 AM

@janotte

Thanks for the useful piece information. Atleast got to learn something new.
Was This Post Helpful? 0
  • +
  • -

#11 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 988
  • View blog
  • Posts: 5,135
  • Joined: 28-September 06

Re: Username & password check fail

Posted 01 January 2011 - 08:03 AM

View PostHuzi94, on 01 January 2011 - 11:26 PM, said:

At least got to learn something new.

Don't get me wrong in any way Huzi94 your explanation of strcmp() was first rate and to be highly commended. It was useful to the OP, directly answered the OP's question and will be a great resource for those who read this in the future.

If the OP was writing in C it would be the perfect answer. In no way was I wanting to suggest your contribution was not excellent.

It's just that since the OP is writing in C++ there is an even better option available.
Was This Post Helpful? 0
  • +
  • -

#12 busta5000  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: -7
  • View blog
  • Posts: 147
  • Joined: 08-March 08

Re: Username & password check fail

Posted 01 January 2011 - 08:20 AM

View Postjanotte, on 01 January 2011 - 06:13 AM, said:

Two important issues here.

1 - Don't use strcmp().


2 - Don't use goto.


Sorry it took time for me to reply but I was actually reading how to do c++string & try to use other methods other than goto.

I as a simple student respect you & I will not use goto. I did use c++string & it worked like charm

string username;         
string password;        
string userreg;        //username check
string passreg;       //password check

if ((userreg == username) && (passreg== password))



I will explain myself why I did use goto to be honest as I said before instructor said make the code work by any means necessary even using codes we did not study wet.

The instructor only taught us return 0; or return 1; or return value; I'm taking about the codes that make you return to a specific line. I searched & found goto so I used it & it worked great for me didn't know it was offensive to PHd professors!

The only method I can think of to return to a specific line & run that line other than goto is switch case or if statement. The problem is I need to copy paste registration & login codes more than one time in every switch case or if statement that requires it to go there!

I think my instructor will decrease my grade for repeating login & register multiple times.

So isn't there another method to return to a specific line similar to goto but can be used by a simple student like me?

If there is another method other than switch case or if statement I am ready to learn.
Was This Post Helpful? 0
  • +
  • -

#13 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1251
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Username & password check fail

Posted 01 January 2011 - 08:25 AM

View Postbusta5000, on 01 January 2011 - 08:52 AM, said:

Thank you Huzi94 for explaining how comparison is suppose to work I thought I should use == because most sites compare using ==! It did work exactly how I planned



JackOfAllTrades thank you for suggesting that I use other methods other than goto I will use other methods from your references. But I have an advise for you please be less aggressive next time.

edit:sited -> sites


I will slap you with both hands if I catch you using Goto next time. I'll be following your posts. :lol:
Was This Post Helpful? 0
  • +
  • -

#14 jjl  Icon User is online

  • Engineer
  • member icon

Reputation: 1049
  • View blog
  • Posts: 4,458
  • Joined: 09-June 09

Re: Username & password check fail

Posted 01 January 2011 - 11:53 AM

here's a quick example of how to loop the log in process
#include <iostream>
#include <string>

#define LOGIN_SUCCESS 0
#define LOGIN_FAILED -1

int login(std::string username, std::string password)
{
	// if the username and password are correct return LOGIN_SUCCESS (0), if not return LOGIN_FAILED (-1)
	return (username == "USERNAME" && password == "PASSWORD")? LOGIN_SUCCESS : LOGIN_FAILED;
}


int main()
{
	std::string userName, userPassword;

	for(;;)/>
	{
		std::cout<<"USER NAME : ";
		std::cin>>userName;
		std::cout<<"USER PASSWORD : ";
		std::cin>>userPassword;
		if(login(userName, userPassword) == LOGIN_FAILED) //if login in failed, display error message and keep looping
			std::cerr<<"Login Failed .... Try again"<<std::endl;
		else
		{
			std::endl::cout<<"Login in Successful!!!!"<<std::endl;
			break; //break out of infinite loop, you were successfull
	}
	//now do operations that you are successfully logged in

	std::cin.ignore();
	std::cin.get();
	return 0;

}


hope that makes sense :)

This post has been edited by ImaSexy: 01 January 2011 - 11:53 AM

Was This Post Helpful? 2
  • +
  • -

#15 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Re: Username & password check fail

Posted 01 January 2011 - 05:25 PM

Very nice, never thought about doing something like the above code. Learn something new everyday, you get a "This post was helpful" hit from me :D

This post has been edited by JackOfAllTrades: 02 January 2011 - 05:58 AM
Reason for edit:: Removed unnecessary quote

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2