Strange Strange - 4 years ago 101
C++ Question

Validations on C++ (Console)

Scenario 1 :

The input is taken in as a character array [20] and is saved onto a *.dat file.

I need to validate the size of the array, check the if there are any special characters,numeric and letter's case sensitivity.

since it's not a string cannot I use strlen to validate the length. If not is it possible to use

.length or size_t / sizeof
method

Scenario 2 :

Need to validate Age , Id and Phone number which are integers in a struct.(also saved onto a *.dat file)

For an Example:

struct patient{

int pId, age ,contactNo;
}

void input(){

patient obj;
if ((int)obj.age<=65){
//Some Code
}
}


The Above method did not work.

I need to validate the age with a set age limit like the above. pId should not be more than four digits and phone number will be 10 digits with out "-" in between and should only allow numeric input.

Is there any possible way of doing the above scenarios. If it possible please give me an example ...

Answer Source

It is unclear (at least) to me what you ask for in Scenario 1.

Scenario 2:

  • You need a comma after your struct definition
  • You should removed the typecast: (int)obj.age<=65 should be obj.age<=65, as it is already an int
  • With respect to pId, just check if it under 10000
  • For contactNo you need a uint64_t otherwise it cannot hold ten arbitrary digits.

It may also be a good idea to make your age and pId variables unsigned int instead of int, this way you remove the possibility of negative age and pId with a minus.

You can construct and object of type patient like this:

patient obj{1234, 47, 1234567};
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download