I wrote this small program that enters a password from a user and displays '*' instead of the letters so I could add this into my project.
I compiled it through Dev C++ using Windows 7 and I am getting errors, please help me out.
The program :-

#include<iostream.h>
#include<conio.h>
#include<string.h>
char enterPass();
void passFunc();
char enterPass()
{
	char numPass[11];
	char ch;
	int i=0;
	while((ch!='\r')||(ch!='\n')&&(i!=10))
	{
		cin>>ch;
		cout<<'*';
		numPass[i]=ch;
		i++;
	}
	return numPass[11];
}
void passFunc()
{
    char pass[11];
	cout<<"Enter password :- ";
	pass=enterPass();
	if(strcmp(pass,"myworld")==0)
	{
		cout<<"Correct Password";
		getch();
	}
	else
	{
		cout<<"Wrong Password";
		exit(0);
	}
}
int main()
{
	passFunc();
	getch();
	return 0;
}

Errors I am getting are :-

24 C:\Users\Dhruv\Desktop\pass.cpp incompatible types in assignment of `char' to `char[15]' 

24 C:\Users\Dhruv\Desktop\pass.cpp At global scope:

Your function is designated to return a single char and "pass" in passFunc is declared as a char array. There's a mismatch there.

These errors don't include that you are trying to return a single character (perhaps thinking you are returning the char array) from your enterPass function that is one past the end of the array (arr[11] has elements 0 thru 10). I would say the best thing to do would be to have your function return a std::string. Makes things nice and easy.

You're going to have some trouble with displaying the stars because all of the standard library functions will echo your input. There's a platform dependent way to do it with getch I believe but I don't remember exactly how.

Also, a bit off topic, but it seems like you have ditched your VC++ 2010 for the Turbo again -- how come?

Edited 6 Years Ago by jonsca: n/a

Your function is designated to return a single char and "pass" in passFunc is declared as a char array. There's a mismatch there.

These errors don't include that you are trying to return a single character (perhaps thinking you are returning the char array) from your enterPass function that is one past the end of the array (arr[11] has elements 0 thru 10). I would say the best thing to do would be to have your function return a std::string. Makes things nice and easy.

You're going to have some trouble with displaying the stars because all of the standard library functions will echo your input. There's a platform dependent way to do it with getch I believe but I don't remember exactly how.

Also, a bit off topic, but it seems like you have ditched your VC++ 2010 for the Turbo again -- how come?

So how do I use std::string thingy. I don't have knowledge about that. I am trying to apply what I have learnt at school. Can you provide some links?

I am still using VC++ 2010, I keep alternating between those compilers. Sometimes VC++, sometimes Turbo. Just for time pass. Hehe

This is a good overall reference: http://www.cplusplus.com/reference/string/

Your function would look just like this (omit the std:: if you have a using directive):

std::string enterPass()
{
    std::string pass;
    cin >> pass; //provided no spaces, otherwise use getline
    return pass;
}
in your other function

void Passfunc()
{
   std::string password;
   password = enterPass();

  //etc.

}

You can write a std::string right to another one without using strcpy and you can compare them to each other with != == etc. instead of strcmp.

I remember my first test masking program - the good 'ol days with TC(yuck!) & non-standard C++. Yes, jonsca is right & getch() had the key role in it. My code was somewhat like this:

char pass[50]; 
int i=0;
while(1)
{
  char ch = getch();
  if(ch == '\r')
  {
     pass[i++]='\0';
     break;
  }
  else if(ch=='\b')
  {
     cout<<"\b \b";
     i--;
  }
  else
    pass[i++]=ch;
}

This should mask out characters while storing them at the same time in pass[].

This article has been dead for over six months. Start a new discussion instead.