Solved

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

Posted on 2003-11-02
6
411 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

728 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now