radiantshaw
radiantshaw

Reputation: 545

How to stop the user from entering more inputs in c++?

Suppose I created a character array say of 20 bytes and prompts the use to input their name provided their name should not be more than 20 characters... For Example:

char Name[20];
gets(Name);

Now suppose his name is abcdefghijklmnopqrstuvwxyz But as soon as he enters abcdefghijklmnopqrst, the Name should not take any further input... Neither the further input provided by the user should appear on the console screen... Only the cursor should blink until he presses enter. So how to do it??

More Information

Operating System: Windows 7

Compiler: Visual C++ 2010 Express

Upvotes: 0

Views: 366

Answers (2)

WildBeginner
WildBeginner

Reputation: 13

Here's silly answer: If you want for example the user to enter a name with maximum 20 characters Create a char array with. size[20] Ask the user to enter the size of the name and check the validity of the number if its less than or equal.20 then move on and add the name else ask the user to enter the size again. You can use a string instead of a char but that won't make sure that the user actually followed the size rule For example a user can enter a size 10 and the program will allow him to enter the name but once he's allowed if you are using the.string he can write as many as he wishes.

OR using a char array. for name with 20 or less.characters

For example the code would need a simple for loop Since we already have the size given which is less than or equal.20

For(int i =0; i<20; i++) Cin>>char[i] Then you can assign the characters into a string.

Upvotes: 0

Sebastian Mach
Sebastian Mach

Reputation: 39089

The manpage states:

Never use gets(). Because it is impossible to tell without knowing the data in advance how many characters gets() will read, and because gets() will continue to store characters past the end of the buffer, it is extremely dangerous to use. It has been used to break computer security. Use fgets() instead.

So you could use fgets:

char *fgets(char *s, int size, FILE *stream);

Yet the input sequence doesn't stop yet; however, you are no longer running into undefined behaviour and security holes, iff used correctly.

You should better use C++ facilities like std::string, std::getline, and the standard iostreams. They won't enable you stopping input, however, they prevent security holes when used in the canonical manner.

Summary of Standard Alternatives:

  • gets() can not be used correctly. People will may die when you use this (you wouldn't find a smile on my face as I am not kidding). There is provably no correct way to use it. Do not forget it, but throw it out of your toolbox now.
  • fgets() can be used correctly, but there's a lot of opportunity to misuse it.
  • iostreams and strings are automatically used correctly. Use them.

Unforunately, there is no builtin solution to read at max N characters from input without having to discard superfluous characters typed in. You would have to use a 3rd-party library or roll your own.

Upvotes: 3

Related Questions