Hi

I finally upgraded a code for password i hope you like it.

#include <conio.h>
#include <iostream>
using namespace std;
int main()
{
	char *tempPassword = new char;
	cout << "enter your password: ";
	char ch = _getch();
	int j = 0;
	while (ch != '\r') 
	{
		if (ch == '\b')
		{
			if (j == 0)
				ch = _getch();
			else
			{
				cout << '\b';
				cout << " ";
				cout << '\b';
				j--;
				tempPassword--;
				ch = _getch();
			}
		}
		else
		{
			cout << "*";
			*tempPassword = ch;
			ch = _getch();
			tempPassword++;
			j++;
		}
	}
	*tempPassword = '\0';
	for (; j>0; j--)
		tempPassword--;
	cout << endl;
	return 0;
}

thanks for Ancient Dragon for giving me the code that i upgraded

Recommended Answers

All 5 Replies

> char *tempPassword = new char;
How many characters can you store here before you overflow the amount of memory you've allocated.

Also, you need to free this memory at some point.

I don't know i think new char; alone without will make it unlimited?

and thanks for reminding me about freeing memory i will fix it

What makes you think such a thing?
The fact that it doesn't immediately crash?

new char; is exactly what it means, ONE (just the one, no more) character.

There's no such thing as an infinitely long array in either C or C++.

C++ containers like vector and string can expand automatically, but they're still bound by the limits of the physical machine.

I am a novice programmer and new to C++

i want to ask why we use an underscore here in ch=_getch();

>i want to ask why we use an underscore here in ch=_getch();
Because that's how the compiler defines it, probably.

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.