Solved

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

Posted on 2003-11-02
6
417 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 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
  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 …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

707 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