-4

Im talking about the string validation in void enter() , what im trying to do is create a system , where if someone accidentally puts a number in Enter name field , they need to write it again

im just not being able to solve this string validation at all.

I tried something like after cout<<"Enter name";

 for(int i=0;i<strlen(name);i++)
 {
  gets(name);
  if(isalpha(name[i]))
  cout<<"There is a number in the input , try again";
 }


include<fstream.h>
include<stdio.h>
include<conio.h>
include<string.h>
include<stdlib.h>

class Directory
 {
 char name[20];
 char num[30];
 char address[50];
  public:
  void enter()
 {
  cout<<"Enter name: "<<endl;
  gets(name);
  cout<<"Enter number: "<<endl;
  gets(num);
  cout<<"Enter address: "<<endl;
  gets(address);
 }

it did work , but if I enter a name "mar3", now i have to enter Mark four times for it to take the record.

Curse
  • 7
  • Firstly, no reasonable validation is possible with `gets`. You need `std::getline`. – HolyBlackCat Jan 10 '19 at 14:45
  • Please, do not use `gets`. It has issues and was deprecated in C++11 and removed in C++14. C++ has better ways to do what you want and you can learn about those with a [good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – NathanOliver Jan 10 '19 at 14:47
  • That is not what #include directives look like. Which book are you using? – Lightness Races in Orbit Jan 10 '19 at 14:48
  • 1
    Some jurisdictions allow numeric digits in names (ones written out in Roman numerals are common in the US), so don't be unnecessarily restrictive. – Bathsheba Jan 10 '19 at 14:49
  • 1
    Please read about [mcve] and provide one. The code you show here does not compile, hence it does not match the problem you describe " but if I enter a name "mar3", now i have to enter Mark four times for it to take the record." - We need some code to reproduce exactly that, only then we can tell you how to fix it in a meaningful way. Btw I second batsheba, why is `mar3` not a valid name? – 463035818_is_not_an_ai Jan 10 '19 at 14:52
  • Use `std::string` for text because character arrays can overflow. You don't restrict the length of the input. – Thomas Matthews Jan 10 '19 at 14:55
  • 2
    If you want to get serious about validation, see [Falsehoods Programmers Believe About Names](https://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about-names/). Less ambitiously, what about O'Malley or Harvey-Jones? – molbdnilo Jan 10 '19 at 14:55
  • 1
    On a logical side note, a character's not being alphabetical does not imply that it's a digit. – molbdnilo Jan 10 '19 at 15:03
  • @user463035818 my teacher says there cant be any numbers in a name – Curse Jan 10 '19 at 15:19
  • @Bathsheba Which roman numerals consist of numeric digits? – Lightness Races in Orbit Jan 10 '19 at 15:24
  • @Curse: Your teacher needs to read [this](https://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about-names/) (and [this](https://softwareengineering.stackexchange.com/a/330518/17853)) then make all his/her/its students read it too. But you'll have to ignore that for now then. – Lightness Races in Orbit Jan 10 '19 at 15:26
  • @LightnessRacesinOrbit: "ones" is referring to "names" not "numeric digits in names", but I know you know that really. Do I need to ask mother if my writing is appropriate? – Bathsheba Jan 10 '19 at 15:41
  • @Bathsheba You said "some jurisdictions allow numeric digits in names" then seemed to give roman numerals as an example, which I just thought was weird because roman numerals do not have any numeric digits. Not sure what your mother has to do with it either. Oh well, you seem to be getting personal about it, so let's just leave it. – Lightness Races in Orbit Jan 10 '19 at 15:43
  • @LightnessRacesInOrbit Because Roman numerals do not have numeric digits, there is no ambiguity in my sentence. I thought you knew that my mother is a professor of English Literature at Oxford. – Bathsheba Jan 10 '19 at 15:45
  • @LightnessRacesinOrbit: No personal here, I'm rather excited in fact: just sped up some code by 3 orders of magnitude ;-) – Bathsheba Jan 10 '19 at 15:46
  • @Bathsheba Then why did you mention roman numerals at all? And why would I know where your mother works? I'm so confused! Confused by three orders of magnitude in fact! Shrug. – Lightness Races in Orbit Jan 10 '19 at 15:47
  • @LightnessRacesinOrbit: Cuz names with "phonetic" numbers in them are common (King Henry VIII) but ones with digits are less so, but are still allowed in some places. Now who was I having that conversation with then? Oh well, back to the abstract syntax tree! Kitty out. – Bathsheba Jan 10 '19 at 15:49

1 Answers1

1

I think this is what you want to do. You want to reenter the name untill it meets your requrement.

bool validate(char name[])
{
    for(int i=0;i<strlen(name);i++)
        if(isalpha((unsigned char)name[i]))
        {
            cout<<"There is a number in the input , try again";
            return 0;
        }
    return 1;
}

and while reading name do this.

gets(name);
while(!validate(name))
{
    gets(name);
}

UPD : use string and getline to read.

#include<fstream>
#include<cstdio>
#include<cstring>
#include<stdlib.h>
#include<stdio.h>
#include<iostream>
using namespace std;
bool validate(string name)
{
    for(int i=0;i<name.length();i++)
        if(isalpha(name[i]))
        {
            cout<<"There is a number in the input , try again\n";
            return 0;
        }
    return 1;
}
class Directory
{
    string name,num,address;
    public:
    void enter()
    {
        cout<<"Enter name: "<<endl;
        getline(cin,name);
        while(!validate(name))
        {
            getline(cin,name);
        }
        cout<<"Enter number: "<<endl;
        getline(cin,num);
        cout<<"Enter address: "<<endl;
        getline(cin,address);
    }
};

here is you want to also limit the length of the name or number you can make another validator for that

Poojan
  • 3,366
  • 2
  • 17
  • 33
  • Thanks for a good explanation , but now it shows me errors. first is type name expected and second is declaration missing. – Curse Jan 10 '19 at 15:13
  • @AlgirdasPreidžius Fixed mb – Poojan Jan 10 '19 at 15:14
  • If you are using c++ dont use gets. And use strings its easier to wrok around. I have edited my answer to with strings and using getline. – Poojan Jan 10 '19 at 15:32
  • @Curse why you shouldn't use gets https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used. – Poojan Jan 10 '19 at 15:40
  • You may want to assign `strlen(name)` to a const temporary variable. This will prevent the compiler from calling `strlen` each iteration. – Thomas Matthews Jan 10 '19 at 15:40
  • `isalpha(name[i])` gives UB on negative character codes. It should be `isalpha((unsigned char)name[i])`. – HolyBlackCat Jan 10 '19 at 15:51