954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

this should be an easy loop

Please help this loop won't step out. It keeps asking for input 3 times even when the inputs are correct.

//login with loop
#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>  // for gets

int main()
{
char charname[15],charpassword[15],chartryname[10],chartrypassword[15];
int inttrys;

    cout<<"Please enter a user name to setup ";
    gets(charname);

    cout<<"Please enter a password to setup ";
    gets(charpassword);
    system("cls");

for(inttrys=1; inttrys <=3; inttrys++)

{
    cout<<"You have 3 attempts"<<endl;
    cout<<"This is attempt number "<<inttrys<<endl;
    cout<<"Please enter your user name ";
    gets(chartryname);
    cout<<"Please enter your password ";
    gets(chartrypassword);
if(chartryname==charname && chartrypassword==charpassword)
{
    cout<<"\t The details you entered are correct "<<endl;
    inttrys = 4;
}
else
{
    cout<<"\t The details you entered are incorrect "<<endl;
}   //end of if statment
 }  //end of loop

      system("PAUSE");
      return 0;
} //end main
nickthedivil
Newbie Poster
8 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
 

As this is C++, why don't you use C++?

Also, never use gets().

The reason you are failing is that charname will never == chartryname, since both are pointers and both point to different locations. If you use the std::string class, the == operators are overloaded so that you can use == the way you think it ought to be working...

#include <iostream>
#include <string>

#include <cstdlib>  // for system(), alas.

using namespace std;

int main()
{
  string name, password, tryname, trypassword;
  int numtrys;

  cout << "Please enter a user name to setup ";
  getline( cin, name );

  cout << "Pleae enter a password to setup ";
  getline( cin, password );

  system( "cls" );  // there are better ways to do this...

  for (numtrys = 0; numtrys < 3; numtrys++)
  {
    cout<<"You have 3 attempts"<<endl;
    cout<<"This is attempt number "<<numtrys<<endl;

    cout<<"Please enter your user name ";
    getline( tryname );

    cout << "Please enter your password ";
    getline( trypassword );

    if ((tryname == name) and (trypassword == password))
    {
      cout<<"\t The details you entered are correct "<<endl;
      break;
    }
    else
      cout<<"\t The details you entered are incorrect "<<endl;
  }

  if (numtrys < 3) cout << "yeah!" << endl;

  cout << "Press ENTER to continue...";
  cin.ignore( numeric_limits<streamsize>::max(), '\n' );

  return EXIT_SUCCESS;
}


Hope this helps.

Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
 

thanks Duoas
This the first time I have ever used C. Im more at home with VB.net. Its not bad its got me hooked already. I promise never ever ever to use gets() again
Thanks again

nickthedivil
Newbie Poster
8 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
 
I promise never ever ever to use gets() again

Why? are you just going to believe him with no understanding of why he claims it's bad? :?:

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You