?
Solved

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

Posted on 2003-11-02
6
Medium Priority
?
420 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
[X]
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
  • 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 200 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

752 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