Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

C++ HOW TO LIMIT THE NUMBER OF CHARACTERS A USER CAN TYPE

Posted on 2003-11-02
6
413 Views
Last Modified: 2013-12-03
hey all,
I am coding up a project, and I need the user to enter his 4 digit password.  The password is always 4 digits.  I want to make it so that when the user types the code in, it will be impossible to type more than the 4 digits.  How can I do this?  Is there a way to put a limit on the number of characters in cin << ?  If you are not clear on what I'm saying please let me know.  Thanks.
0
Comment
Question by:chipahoy82
  • 3
6 Comments
 

Expert Comment

by:jbuote1
ID: 9672732
There are lots of string manipulation routines, as well as console iput routines.
Here is an example that doesn't use cin, but does the job you need....
Do you have to use cin?

This uses _getche() which get one charater from the console, and echos it to the user.
_getch() does the same thing, but doesn't echo to the console.

Hope this helps....

////////////
#include <iostream.h>
#include <conio.h>
#include <tchar.h>

int main() {

      int            i= 0;
      TCHAR      temp[100];

       cout << _T("Enter Some Text: ") << endl;

      for (i=0;i<4;i++) {
            temp[i] = getche();
      }
      strcpy(&temp[4], _T("\0"));

       cout << endl << _T("You Typed: ") << temp << endl;

      return 0;

}
/////////////
0
 

Author Comment

by:chipahoy82
ID: 9672803
Thanks ,it works but I would like the user to hit enter when done, not just automatically take the input.  I want to give the user the opportunity to go back and delete their number.  Any sugestions?  Thanks.
0
 

Accepted Solution

by:
jbuote1 earned 50 total points
ID: 9673031
Well,
This one will get as many characters (up to 100) that the user enters.
It will only put up to 100 into temp so as not to overflow the buffer and trample memory..
It's then up to you to detemine if it's correct...
As with passwords, this code allows for 3 retries...

Will this do?

//////////////////////
#include <iostream.h>
#include <conio.h>
#include <tchar.h>

int main() {

      int            i= 0;
      int j = 0;
      TCHAR      temp[100];

       cout << _T("Enter Some Text: ") << endl;

      cin.getline(&temp[0], 100, _T('\n'));

      while((strlen((TCHAR*)temp) > 4) && (j< 2)) {
            cout << _T("You entered too many characters. The limit is 4. Try again") << endl;
            cout << _T("Enter Some Text: ") << endl;
            strcpy((TCHAR*)temp, _T(""));
            cin.getline(&temp[0], 100, _T('\n'));
            j += 1;
      }

      if(strlen((TCHAR*)temp) > 4) {
            cout << endl << _T("Invalid text... Access Denied") << endl;
      }
      else {
            cout << endl << _T("You Typed: ") << temp << endl;
      }

      return 0;

}
0
 

Expert Comment

by:jbuote1
ID: 9673047
I should also add:
Do you understand what's happening there?
If not, let me know and I'll try to explain it better...

0
 
LVL 9

Expert Comment

by:tinchos
ID: 10242444
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: jbuote1 {http:#9673031}

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question