Link to home
Start Free TrialLog in
Avatar of bpatia
bpatia

asked on

how do I call a flat text file to my program?

This is my program which is not running properly!


//Project #1


#include <iostream.h>
#include <fstream.h>
#include <string.h>


void access ();
int main ()

{

      access ();

      cout <<"Access succeful. \n ";

      return 0;

}





void access ()

{
      char userid[15],password [15],pass[15],user[15];

      char ans,close;

      ifstream fin;

      cout << " Please enter your user ID \n";

      cin  >> userid;

      cout << "Now please enter a password: \n";

      cin  >>password;

         
      
      
             fin.open("c:security.txt");

              while (fin>>user>>pass);

            {
                if (!strcmp(userid,user)&& !strcmp(password,pass));


          else if (userid!=user);
                 cout <<" wrong userid\n";
             
                    cout <<"denied";

   }
            
      fin.close ();


}



It supposseted to verify this passwords and it's not doing it.
Avatar of bpatia
bpatia

ASKER

bpitt            bp1111
sconnery      sc2222
mphiffer      mp3333
mmonrow    mm4444
jbeal           jb55555
try this one:

//Project #1
#include <iostream.h>
#include <fstream.h>
#include <string.h>
int access ();
int main ()
{
     if (access ()==0)
     cout <<"Access succeful. \n ";
     return 0;
}
int access ()
{
     char userid[15],password [15],pass[15],user[15];
     char ans,close;
     ifstream fin;
     cout << " Please enter your user ID \n";
     cin  >> userid;
     cout << "Now please enter a password: \n";
     cin  >>password;
            fin.open("c:security.txt");
            while (fin>>user>>pass);
                      {
                         if (!strcmp(userid,user)&& !strcmp(password,pass)){
                              fin.close ();
                              return 0;
                              }
                         else if (userid!=user);
                         cout <<" wrong userid\n";
                         cout <<"denied";
                         }
     return -1;
}

Regards
Kar10s
Sorry , I forgot  fin.close ();

//Project #1
#include <iostream.h>
#include <fstream.h>
#include <string.h>
int access ();
int main ()
{
     if (access ()==0)
     cout <<"Access succeful. \n ";
     return 0;
}
int access ()
{
     char userid[15],password [15],pass[15],user[15];
     char ans,close;
     ifstream fin;
     cout << " Please enter your user ID \n";
     cin  >> userid;
     cout << "Now please enter a password: \n";
     cin  >>password;
            fin.open("c:security.txt");
            while (fin>>user>>pass);
                      {
                         if (!strcmp(userid,user)&& !strcmp(password,pass)){
                              fin.close ();
                              return 0;
                              }
                         else if (userid!=user);
                         cout <<" wrong userid\n";
                         cout <<"denied";
                         }
 fin.close ();
 return -1;
}



Avatar of Infinity08
Or a better working variant :

#include <iostream.h>
#include <fstream.h>
#include <string.h>

int access();

int main() {
  switch (access()) {
    case 1 : cout << "Access successfull." << endl; break;
    case 0 : cout << "Access denied : no such username." << endl; break;
    case -1 : cout << "Access denied : password incorrect." << endl; break;
    default : cout << "Access denied." << endl;
  }
  return 0;
}

int access() {
  char userid[15],password [15],pass[15],user[15];
  char ans,close;

  ifstream fin;

  cout << "Please enter your user ID :" << endl;
  cin  >> userid;
  cout << "Now please enter a password :" << endl;
  cin  >> password;

  fin.open("c:security.txt");

  while (fin>>user>>pass) {
    if (!strcmp(userid,user)) {
      if (!strcmp(password,pass)) {
        fin.close();
        return 1;
      }
      else {
        fin.close();
        return -1;
      }
    }
  }
  fin.close();
  return 0;
}
//this is the correct code for access

int access ()
{
     char userid[15],password [15],pass[15],user[15];
     ifstream fin;
     cout << " Please enter your user ID \n";
     cin  >> userid;
     cout << "Now please enter a password: \n";
     cin  >>password;
            fin.open("d:\\security.txt");
            while (fin>>user>>pass)
                    {
                         if (!strcmp(userid,user)&& !strcmp(password,pass)){
                              fin.close ();
                              return 0;
       }
       else if (strcmp(userid,user)) {
                         cout <<" wrong userid\n";
                         cout <<"denied";
      }
       else if (strcmp(password,pass)) {
                         cout <<" wrong password\n";
                         cout <<"denied";
      }
         }
     return -1;
}

//you also the problem that you would always print wrong userid even if it was the wrong
//password but the right userid. You cannot compare userid and user the way you were
//because you were comparing thier addresses effectively, which they will always be
//different :)
the problem with the code was also that several unwanted couts were done (like "wrong userid" while you entered a correct one). That is why i changed the code the way i did.
ASKER CERTIFIED SOLUTION
Avatar of kar10s
kar10s

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
euhm, kar10s that's almost a literal copy of my suggestion :) only difference is the location of the cout's ...

btw, bpatia, does any of this answer your question ?
Sorry Infinity08. I did not read your code.
I answered again due to I thought that bpatia followed having problems with this question.
take a look to this
https://www.experts-exchange.com/questions/20886447/How-do-I-make-my-program-run-properly.html

Regards
Kar10s
int iCheckCred(char userid[15], char password[15])
{
     char user[15],pass[15];
    ifstream fin;
    fin.open("c:\\security.txt");
    while (fin>>user>>pass)
    {
            if (!strcmp(userid,user)&& !strcmp(password,pass)){
                fin.close ();
                return 0;
               }
     }                
     fin.close ();                    
    return -1;
}

int main()
{
     char userid[15],password [15];
     cout << " Please enter your user ID \n";
     cin  >> userid;
     cout << "Now please enter a password: \n";
     cin  >>password;
     if (fCheckCred(userid,password)==0){
          cout <<"Access succeful. \n ";
     }else{
          cout <<" wrong userid\n";
          cout <<"denied";
     }
     cin.get();
     return 0;
}
> Sorry Infinity08. I did not read your code.
Wasn't meant to accuse you of that :) Just more of a witty comment to say that our code must be OK since we both came up with the same :)
Avatar of bpatia

ASKER

Thank you guys. You helped me a lot with my assigment.
It really answer my Quetions. Specially to  Infinity08 and kar10s. :)
Avatar of bpatia

ASKER

why the return -1, and the return -2, and not 0?????
Just gonna answer this (although it's about kar10s's code) :)

the return statement in a function ends execution of that function and gives back the indicated value.

So, the following if statement in main() :

  if (access()==0)

checks whether 0 was returned or another value (in this case, the possible values are 0, -1 and -2)
Avatar of bpatia

ASKER

infinity08
Thank You so much for your help!
:)