Solved

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

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

Technology Partners: 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

Suggested Solutions

Title # Comments Views Activity
Task manager indicates my c++ program memory consumption is growing? 12 140
Best book to learn C++ 4 87
Outlook 13 84
Coding for the first time 9 70
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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

726 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