I have been trying to tweak this program to get rid of the itoa() function. I am having a little trouble properly implementing a to_string() or a snprint() replacement. I left my attempts commented out. Maybe one of the C++ experts here can assist me. This is my first time posting please forgive me if I don't do this correctly. I'm not sure how to format the code tags.
Why are you using C-strings instead of C++ std::string? You've included the C++ string header so it appears you know about this class, why not use it, then you can get rid of all those horrible C-strings and use the std::string functions to convert numbers to strings, or strings to numbers.
Why do you complicate things so much?
There is no need to convert the int into a string to check if it is valid. What are the valid ranges of an id? You could easily do it like that.
1 2 3 4 5 6 7 8 9
bool Patient::isValidID(int id)
{
constint max_id = 100 // or some other value
constint min_id = 1; // or some other value
if (id < min_id ¦¦ id > max_id)
returnfalse;
returntrue;
}
This was to show that I had the appropriate Libraries loaded for the snprintf() and to_string() functions.
Why do you complicate things so much?
There is no need to convert the int into a string to check if it is valid. What are the valid ranges of an id? You could easily do it like that.
I know it may be overly complicated, but this is my second class in C++. Therefore, I just don't know any better, sorry.
The constraints for "id" is that it must be a 5 digit number.
This was to show that I had the appropriate Libraries loaded for the snprintf() and to_string() functions.
But that doesn't explain the use of the C standard headers. You should be using the C++ standard headers that provide the same functionality ie: <cstdlib> and <cstdio>. Also note that the <string> header is the C++ header for the C++ std::string class, not the header for the various C-string functions like strlen(), which are contained in the <cstring> header.
By the way is there a reason you even need to treat the ID as a number? I would normally just use a std::string and never convert it to a number, especially if the ID number can have either leading zeros or non-digit characters. When you convert a string that has leading zeros to a number you loose those leading zeros.
If all you need to do is verify that there are 5 digits you can just test the length of the string.
Ok I am understanding what you are saying now. I was copying the incorrect headers when looking up which libraries snprintf() and to_string() were under.
When creating a patient, the program asks the user to enter in a 5 digit number. This data is received in an int.
I am trying to cast this int into a string so I can verify that the length is in fact 5 digits.
This is really the only compile error I am getting. If I can just figure out how to convert this int to a string not using the itoa() function, I think it will compile correctly.
When creating a patient, the program asks the user to enter in a 5 digit number. This data is received in an int.
You really should be using a string to retrieve this value, especially if leading zeros are allowed. A value like "00343" would be a 5 digit "number" if retrieved as a string but would only be a 3 digit number if retrieved as an int.
If I can just figure out how to convert this int to a string not using the itoa() function, I think it will compile correctly.
Well since itoa() is a non-standard function you may be out of luck. Besides you should be using std::strings instead of C-strings and then if you're compiler is recent enough you should be able to use std::string.to_string(), or the good old fashioned stringstream method to convert the values to and from std::strings.
if you're compiler is recent enough you should be able to use std::string.to_string(), or the good old fashioned stringstream method to convert the values to and from std::strings.
Could you possibly show me how to implement the stringstream method? This is something we didn't cover in this class, but sounds like a useful tool to have for the future.
// ERROR HANDLING: To check the id number is valid
bool Patient::isValidID(int id){
//char ID[10];
//itoa(id,ID,10);
string strID;
stringstream out;
out << id;
strID = out.str();
if(strID.length()==5){
for(int i=0; i < strID.length(); i++)
{
if(!isdigit(strID[i]))
{
returnfalse;
}
}
returntrue;
}
else
{
returnfalse;
}
}
So I did the research and understand stringstreams (so much easier than that garbage I was trying before). But now I am getting a "Linker command failed with exit code 1 (use -v to see invocation).
How about cutting and pasting the complete error messages, all of them, exactly as they appear in your development environment. The "picture" you posted just shows the last message, you need to start at the beginning and work your way down. That message is also talking about 27 duplicate somethings, without the complete error listing there is no way to know what or where the problem actually occurs.
Your function is not having problem probably you are doing some mistake in other part of code.
Stringstream is best option to convert int to string as suggested.
your function parameter is int so you can't have number like '00123'.
my suggestion is to convert id as string type.In long run it will help in cater alpha numeric id as well.