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

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.

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

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? :?:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.