Command Line (cin) Hiding & Counter Using C++

Started by
16 comments, last by kuphryn 22 years, 4 months ago
quote:Original post by Oluseyi
  while(((c = getch()) != ''q'') && (c != ''Q'')) 

This line has the unfortunate effect of making the program wait for the user to enter a key before being prompted for each string, which gets tiresome.

[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!
Advertisement
Ahh. Thanks.

So there is not way to get getche() to work without namespace std right?

In your code, you have:

while(...c = getche()...)

and then

getline(cin, str)

What role does str play in the algorithm?

Kuphryn

quote:Original post by kuphryn
So there is not way to get getche() to work without namespace std right?

getch and getche are not part of the std namespace. The work with or without it. I included the std namespace because I''m lazy, and wont look up getline.

quote:What role does str play in the algorithm?

It is the destination of the string of input entered by the user. It is compared to see if it is "Q" or "q" (if it is, we exit) and displayed if it isn''t.

[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!
Your two questions are seperate and unrelated, right, kuphryn? All you want to do for the first one is to have someone enter a password but not show what is entered? If so, then here''s a simple way to do it:

  	char pw[256];    // Your string	char c;          // Stores an individual character	int i = 0;       // Stores the array index	for(;;)	{		c = getch();   //Wait for user to input a character                               //and set c equal to that character.		if(c == 13)			//If the user hit ENTER (13 = ENTER)		{			pw[i] = NULL;    //End the string with a NULL character			break;           //Exit loop		}		else		{			pw[i] = c;       //Add character to string                        i++;      		}	}  
quote:Original post by TerranFury
    			pw[i] = NULL;    //End the string with a NULL character    

  pw[i] = ''\0'';  

I do not believe the two are equivalent.

[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!
TerranFury,

Kick ass!!! Very elegant. It works great.

Here is a section of the code where I incorporated your technique with what I am trying to accomplish:

------------------------------------------------------{	bool veri = false;		string uKeyA, uKeyB, strOpt;	do	{		int tmpCh = 0;		uKsize = 0;				cout << "\nEnter a password (32 chars. max.): ";				while ((tmpCh = getch()) != 13)			uKeyA += tmpCh;			cin.clear();				uKsize = uKeyA.size();		if (uKsize > maxKeySIZE)		{			veri = false;uKeyA.erase(uKeyA.begin(), uKeyA.end());}					cout << "\n\nEnter that password one more time: ";				while ((tmpCh = getch()) != 13)			uKeyB += tmpCh;		cin.clear();				if (uKeyA == uKeyB)		{			veri = true;						for (int i = 0; i < uKsize; ++i)				key = uKeyA;		<br>		}<br><br>		else<br>		{<br>			veri = false;<br>			uKeyA.erase(uKeyA.begin(), uKeyA.end());<br>			uKeyB.erase(uKeyB.begin(), uKeyB.end());<br><br>			cout << "\n\nPassword does not match!  Try again."<br>————————————————————-<br> </pre>  <br><br>Note: I left some part of it out.<br><br>How did you determine integer 13 represents Enter or "\n?" Did you use:<br><br>cout << int("\n");<br><br>I am also trying to implement something like astrisks for each character like in windows.  I am confident I can get that done.  The code you post is simple, yet effectively.<br>—————-<br><br>Oluseyi:<br><br>Interesting.  I think he used NULL to end the string, not to indicate a new line.<br>—————-<br><br>Lastly, here is a simple character array:<br><br>char temp[5] = {'a','b','c','d');<br><br>It is a rule that the last space has to be NULL?  I think it is true if the program sends temp to cout.  However, what if temp is used only as a storage variable?  Is:<br><br>temp[4] = NULL;<br><br>required?<br><br>Kuphryn<br>   </i> <br><br>Edited by - kuphryn on December 16, 2001 2:21:06 PM    
Using ''NULL'' is awkward. It''s not defined as part of C++, so a compiler or header file that provides it could call it whatever it likes. I think MSVC defines it as 0L (zero as a long int) but I''ve seen it defined as (void*)0 before (zero as a pointer to void). Anyway, a string of characters should really be terminated with the zero character : ''\0''. Don''t rely on the compiler to make a conversion for you when you can do it explicitly - saves you on debugging time later.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost ]
Message noted.

Deitel and Deitel C++ How to Program highlighted that it is recommend to use "0" or "''\0''" instead of NULL. I will convert from NULL to "0" or "''\0''" where applicable.

Thanks,
Kuphryn

This topic is closed to new replies.

Advertisement