Validate input data from user in a Class

Im trying to make this work, main is going to be a menu with more options but to make it shorter i have cut that part, it seems its going through like it load and everything but if a enter a number the program keep letting me to continuous i need the system to ask me again until the input is valid, some ideas Thank you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
using namespace std;

class Patient
{
    private:
       
        string firstName;       
        
    public:
        
        bool setFirstName(string);
        string getFirstName();
        
};
        
    
    bool Patient::setFirstName(string fName)
    {
        bool validData = true;
        
        char ch;
        ch = cin.get();
        
        if(isalpha(ch))        
          FirstName = fName;     // It keep giving an error here.
        else
          validData = false;
        
        return validData;
    }
    
    string Patient::getFirstName()
    { return firstName; }
  
int main()
{
  //Main will be only a menu

	system("pause");
	return 0;
}

void create_an_account()
{
system("cls"); 

    Patient NewPatient;
    string first_name;

    cout << "Enter first name:\n\n";
    cin >> first_name;

cout << "\nPress the Enter key to return to the menu. ";
	cin.get();     
	cin.get();     
}
Last edited on
There's two problems with using getchar(). Firstly, it should not generally be used in C++ programs since we have the equivalent cin.get() (which has some nice signature variations, check 'em out: http://www.cplusplus.com/reference/istream/istream/get/).

However, you don't want to read a character from standard input here. You want to check the string that's been passed into the function. If all you need to test is the first letter, then you can just test name[0]. But I suspect that you are supposed to test all of the characters, in which case you will need a loop.

And obviously you would need to call NewPatient.setFirstName(first_name) with the string you've read in main.
Last edited on
Yes, actually i need to make the program a way the system will keep asking for the input until is valid. Do you have any idea how can i do that. Thank you. I change the getchar() for cin.get Thank you.
Re-read the second and third paragraphs in my post above. You should NOT use cin.get() (or getchar()) at all in that function. You just want to access the characters in the string fName, like fName[i] in a loop or something and see if they are all alphabetic. Then you'd have a loop in main and you loop unil NewPatient.setFirstName(first_name) returns true.
Can you show me a little bit more how i can do it please?
because i have used char before to see if it is a letter o not but it only check the first later i dont know how to do it with more letter. im not really following you what i need to do, im sorry.

Thank you.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <string>
#include <cctype>

struct patient
{
    // constructor: initialise first_name with the passed name
    explicit patient( std::string name ) : first_name(name) {}

    std::string get_first_name() const { return first_name ; } // note: const
    // see: https://isocpp.org/wiki/faq/const-correctness#const-member-fns

    private: std::string first_name ;
};

bool valid_name( std::string str ) // return true if str holds a valid name
{
    return str.size() > 1 && // valid if str has at least two characters
           std::isalpha( str.front() ) ; // and the first character is alpha
}

std::string get_name() // get valid first name from user. retry on input error.
{
    std::string name ;

    std::cout << "enter first name: " ;
    std::cin >> name ;
    if( valid_name(name) ) return name ; // valid name, return it

    std::cout << "invalid name. try again\n" ;
    return get_name() ; // try again
}

patient create_an_account() // create a new patient from user input and return it
{
    // initialise a patient object with a valid name entered by the user
    patient this_patient( get_name() ) ;
    return this_patient ; // and return it
}

int main()
{
    const patient p = create_an_account() ;

    std::cout << "patient{ name: " << p.get_first_name() << " } created.\n" ;
}
Thank you so much, but i have not be able to run it, it keep giving me this message no matching function for call to ` in this line std::isalpha( str.front() ) ;

Thank you again

std::basic_string::front was added by C++11. If you are using an old (pre-C++11) compiler, use operator[]

1
2
3
4
5
bool valid_name( std::string str ) // return true if str holds a valid name
{
    return str.size() > 1 && // valid if str has at least two characters
           std::isalpha( str[0] /* str.front() */ ) ; // and the first character is alpha
}
Thank you so much, i need it to validate numeric input but i just have to change the isalpha for isdigit and it works fine.
Topic archived. No new replies allowed.