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.
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.
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.
#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-fnsprivate: 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() ) ;
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
}