andrew Patterson
andrew Patterson

Reputation: 579

Validate the length of a char * taken from std::cin

I have a pointer called char * panimal_name. This pointer should only be able to take in 20 characters and if the user enters more, it must ask the user to re-enter.

I've tried counting the characters in the stream and also using strlen(), however I'm still having problems.

cout << "Enter Animal Name: ";
cin.ignore();
cin.getline(panimal_name, 20);

Any help would be appreciated.

EDIT: Well I only want it to take at most 20 characters from the user. If that 20 is exceeded it should then ask the user to re-enter valid input. However in this setup, it now messes up the stream for my next inputs. The reason I'm using this, rather than a std::string, is that I'm learning pointers at the moment.

P.S. I know a string would probably be better in this situation for ease of use.

Upvotes: 0

Views: 1100

Answers (4)

user48678
user48678

Reputation: 2552

Use a larger buffer for user input and check for the last element of your buffer.

Upvotes: 0

Robᵩ
Robᵩ

Reputation: 168766

Consider the following program:

#include <iostream>
#include <string>
#include <limits>

// The easy way
std::string f1() {
  std::string result;
  do {
    std::cout << "Enter Animal Name: ";
    std::getline(std::cin, result);
  } while(result.size() == 0 || result.size() > 20);
  return result;
}

// The hard way
void f2(char *panimal_name) {
  while(1) {
    std::cout << "Enter Animal Name: ";
    std::cin.getline(panimal_name, 20);
    // getline can fail it is reaches EOF. Not much to do now but give up
    if(std::cin.eof())
      return;
    // If getline succeeds, then we can return
    if(std::cin)
      return;
    // Otherwise, getline found too many chars before '\n'. Try again,
    // but we have to clear the errors first.
    std::cin.clear();
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n' );
  }
}

int main () {
  std::cout << "The easy way\n";
  std::cout << f1() << "\n\n";

  std::cout << "The hard way\n";
  char animal_name[20];
  f2(animal_name);
  std::cout << animal_name << "\n";
}

Upvotes: 1

Grady Player
Grady Player

Reputation: 14549

You can use c++ methods..

std::string somestring;

std::cout << "Enter Animal Name: ";
std::cin >> somestring;

printf("someString = %s, and its length is %lu", somestring.c_str(), strlen(somestring.c_str()));

you can also use more c++ methods

std::string somestring;

std::cout << "Enter Animal Name: ";
std::cin >> somestring;

std::cout << "animal is: "<< somestring << "and is of length: " << somestring.length();

I guess you could do something with cin to a stringstream to get around the way that cin exctract works.

Upvotes: 1

Nick Shaw
Nick Shaw

Reputation: 2113

According to MSDN:

If the function extracts no elements or _Count - 1 elements, it calls setstate(failbit)...

You could check for that failbit to see if the user entered more data than the buffer allows?

Upvotes: 1

Related Questions