Well I got the password code right, I think.
I have a few problems
1) it wont close properly
2) Can someone point me in the right direction to find a tutorial that teach me how to only let the user have 34 tries before it close and also to encrypt the password to a file then decrypt it and compare it to another file that contain the correct password( I think that it use the fstream but I dont know the decrpytion, encryption, compare part )?
Thank you

#include <iostream>
#include <string>
#include <conio.h>
using namespace std;

int takingPass();
int i;
int pass1;
char destination[13];
int main()
{
	 
	cout << "I'm going to kill your computer if you dont enter the right password!!!"<< endl;
	
	for (i=0; i<13; i++)          //user can input max 13 character
       {
		   destination[i] = getch();
		   cout << "*";
		   if (destination[i]==13)
		   {
			   takingPass();
		   }
       }
}

int takingPass()
{
    destination[i] = 0;
    pass1 = stricmp(destination,"lovebug");   //compare with name "lovebug"
    if (pass1 == 0)			    // case insensitive
    {
    cout <<"\nPASSWORD IS CORRECT \n";
    }
    else                                    //if password is not correct
    {
    cin.clear();				// clear cin

    cout <<"\nPASSWORD WAS INCORRECT\n";
    main();                          //go to main to do until success
    }
    		return 0;	     // exit program

}

Recommended Answers

All 2 Replies

>"I'm going to kill your computer if you dont enter the right password!!!"
Uh huh.

>main(); //go to main to do until success
It's illegal to call main recursively in C++.

>teach me how to only let the user have 34 tries before it close

// Pseudo-C++
int n = 0;

while ( n < 34 ) {
  if ( is_right_password() )
    break;
  ++n;
}

if ( n == 34 )
  exit ( EXIT_FAILURE );

// Do stuff for correct password

>encrypt the password to a file then decrypt it and compare it to
>another file that contain the correct password
Do you know how to read from a file? If not then it would be best to consult your C++ book (you do have one, right?). The encrypting and decrypting part is a little more difficult depending on your needs, but we'll get to that once you have a program that works with plain text. Those are easier to test for correctness. ;)

>"I'm going to kill your computer if you dont enter the right password!!!"
--- well it made me laugh a little. I change some of the wording.

Okay I think I few some of the bugs but I dont know if the program is good.
I got it to save the password a file call out.

#include <iostream>
#include <fstream>
#include <string>
#include <conio.h>
#include <stdio.h>
using namespace std;

int takingPass();
int o = 0;
int i;
int pass1;
char ch[13];
int wrong();
int main()
{


	ofstream out;
	out.open("c:\\out.txt");

	cout << "Please enter the correct password."<< endl;
	
	for (i=0; i<13; i++)          //user can input max 13 character
       {

		   ch[i] = getch();
		   cout << "*";
		   out << ch[i];
		   


		   if (ch[i]==13)
		   {
			   takingPass();
			   cout << o;
		   }
		  
       }
	   
	  out.close();
}

int takingPass()
{

    ch[i] = 0;
	o++;
	pass1 = stricmp(ch,"lovebug");
			
		 if (pass1 == 0)			    
			{
				cout <<"\nPASSWORD IS CORRECT \n";
				exit(1);				// exit program if successful
			}
				else 
				{
				system("cls");
			    cout <<"PASSWORD WAS INCORRECT\nPLEASE RE-ENTER PASSWORD"<<endl;
				cout << "Number of re-tries: "<<o;  // show o increasing
				cout<< endl;
				}
					if (o == 3)
					{
						cout << "YOU ARE A UNAUTHORIZE USER\n";
						exit(1);    // exit program
					}
						main();
						return 0;
	
}
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.