itoa() woes

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.

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//Patient.cpp
#include <stdlib.h>
#include "Patient.h"

//Constructors
Patient::Patient(){}

Patient::Patient(string first, string last, int id, string phyFirst, string phyLast){
    
    if(!isValidID(id)){
        cout << endl << "Invalid patient ID";
        return;
    }
    else
    {
        patientID = id;
        setName(first, last);
        physicianName.setName(phyFirst, phyLast);
    }
    
    Status="";
}

//Accessors
string Patient::getStatus(){
    
    return Status;
}

string Patient::getPhysicianFirstName(){
    
    return physicianName.getFirstName();
}

string Patient::getPhysicianLastName(){
    
    return physicianName.getLastName();
}

int Patient::getPatientID(){
    
    return patientID;
}

//Mutators
void Patient::setPhysicianName(string first, string last){
    
    physicianName.setName(first,last);
}

void Patient::setPatientID(int id){
		
    if(!isValidID(id)){
        cout << endl << "Invalid patient ID";
        return;
    }
    else
        patientID = id;
	}

void Patient::setStatus(string status){
    
    Status = status;
}

// ERROR HANDLING: To check the id number is valid
bool Patient::isValidID(int id){
    
    char ID[10];
    itoa(id,ID,10); //<--------This is my issue!
    
    if(strlen(ID)==5){
		for(int i=0;i<strlen(ID);i++)
        {
			if(!isdigit(ID[i]))
            {
				return false;
			}
		}
			return true;
	}
	else
	{
		return false;
	}
}

//Function
void Patient::displayPatient(){
    
	 cout << endl << getFirstName() << " " << getLastName() << " " << getPatientID() << " " << getPhysicianFirstName() << " " << getPhysicianLastName();
}
Last edited on
Why all the C standard headers?

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)
{
  const int max_id = 100 // or some other value
  const int min_id = 1; // or some other value
  if (id < min_id ¦¦ id > max_id)
     return false;

  return true;
}


Why all the C standard headers?


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.


Please use code tags when posting code.

Last edited on
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.

Screenshot:
https://postimg.org/image/7vtqjz5b3/
Last edited on
Make sure you #include <cstdlib>
http://www.cplusplus.com/reference/cstdlib/itoa/
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.
Did you find and study any documentation for this standard class?

http://www.cplusplus.com/reference/sstream/stringstream/

http://www.cplusplus.com/reference/sstream/stringstream/stringstream/

This documentation has many different examples, just click on the various methods for more information.

Remember a stringstream is a stream much like cin and cout are streams, all streams have the same basic interface.



Before:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// ERROR HANDLING: To check the id number is valid
bool Patient::isValidID(int id){
    
    char ID[10];
    itoa(id,ID,10); //<--------This is my issue!
    
    if(strlen(ID)==5){
		for(int i=0;i<strlen(ID);i++)
        {
			if(!isdigit(ID[i]))
            {
				return false;
			}
		}
			return true;
	}
	else
	{
		return false;
	}
}


After:
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
// 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]))
            {
				return false;
			}
		}
			return true;
	}
	else
	{
		return false;
	}
}


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).

What did I do incorrectly
What is the exact error message.

What is the exact error message.


Screenshot:
https://postimg.org/image/msd2xvyqh/
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.
Topic archived. No new replies allowed.