C++

  • Thread starter Thread starter Cabdullahi
  • Start date Start date
  • Replies Replies 7
  • Views Views 2K

Cabdullahi

IB Legend
Messages
5,610
Reaction score
1,308
Gender
Male
Religion
Islam
Anybody here good at c++...ive designed a password function but its kinda useless....it doesnt compare it with the a set password....basically im kinda wack at this maybe some1 could help me :)

here's the code

// hndfh.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}

// 00000.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <windows.h>

using namespace std;

int main ()
{
cout << "Enter you password: (max 4 characters)" << endl;
// password (char array, doesn't work as string)
char password[25];

int i = 0; // used to index the current character being read
while (true) // infinite loop, broken by pressing return
{
password = getch(); // get the current character
cout << "*"; // output a star (can be replaced with a different character)

if (GetAsyncKeyState(VK_RETURN)) // if return has been pressed
break; // break from the loop

i++; // increment our index
}

// now fill the rest of the array with '\0' (NULL) character
for (i; i < strlen(password); i++)
password = '\0';

// output the password (just for testing)
cout << endl << "Welcome Ahmed Ali";

// end of the program
cout << endl << "Press any key to continue . . .";
getch ();
}
 
:sl:

You are not comparing it with a stored password. And you are not displaying the pwd where you say:
// output the password (just for testing)
cout << endl << "Welcome Ahmed Ali";
If you are testing it, then there should be a <<password also in that statement.

Another point that I noticed:
// now fill the rest of the array with '\0' (NULL) character
for (i; i < strlen(password); i++)
password = '\0';

There is no need for the loop here, since you are not changing the value of the index i after entering the password in the previous loop. And since it was counting the no. of chars entered, it will be pointing at the end of the password already. You can simply write password = '\0'; and it will close the string. Try it.

Don't you think this thread should be in the Educational Issues section?

Its late night here. If there are other queries, I will try to check tomorrow inshaAllah.

:w:
 
Anybody here good at c++...ive designed a password function but its kinda useless....it doesnt compare it with the a set password....basically im kinda wack at this maybe some1 could help me :)

here's the code

// hndfh.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}

// 00000.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <windows.h>

using namespace std;

int main ()
{
cout << "Enter you password: (max 4 characters)" << endl;
// password (char array, doesn't work as string)
char password[25];

int i = 0; // used to index the current character being read
while (true) // infinite loop, broken by pressing return
{
password = getch(); // get the current character
cout << "*"; // output a star (can be replaced with a different character)

if (GetAsyncKeyState(VK_RETURN)) // if return has been pressed
break; // break from the loop

i++; // increment our index
}

// now fill the rest of the array with '\0' (NULL) character
for (i; i < strlen(password); i++)
password = '\0';

// output the password (just for testing)
cout << endl << "Welcome Ahmed Ali";

// end of the program
cout << endl << "Press any key to continue . . .";
getch ();
}


assalam

Have you considered joining a usenet group like "comp.lang.c++" ? I think you will learn a lot there abot cpp.

wassalam
 
haha the starting thread was a big joke ...i fooled you guys this is my password function

// ff.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}

#include <string>
#include <iostream>
using std::string;
using std::cin;
using std::cout;

int main(){
string a;
cin>>a;
if(a == "1234"){

cout<<"Welcom Mr Ahmed Ali \n";
}
else if (a != "1234"){
cout << "incorrect password, please try again.";
}
system("PAUSE");
return 0;
}
 
:sl:

Both are two different methods of coding. In the first one, you are simply entering the pwd and then displaying it. If you want to check it with a stored pwd, then you will have to learn file handling as well.

In your second program, you have hardcoded the password, and you are not using asterisk to mask it while entering. Hardcoded pwds cannot be changed, therefore not user-friendly. Its better to use hashes, and store the pwds in a file, and use it for login.

what does the strlen function return?
strlen() returns the length of the string in int. You will have to include string.h header file for it.
 
^yes it takes a string, and returns the number of characters in it.
The declaration syntax in string.h is:
Code:
size_t strlen(const char *s);
 
I thought you needed help with grades because you got C+,but it's not about that. All this computer stuff is hard....
 

Similar Threads

Back
Top