I'm trying to convert a String to uppercase in my header file function. However, when I try to do this I get an error saying "Cannot convert from 'class String' to 'char'.
Here's my code -
#ifndef PATIENT_DEMO_CLASS
#define PATIENT_DEMO_CLASS
// system defined preprocessor statement for cin/cout operations
#include <iostream.h>
// header file from book
#include "tstring.h"
#include <algorithm>
#include <string>
class PatientDemographicInformation
{
private:
// patient's state
String patientState;
public:
// constructor
PatientDemographicInformation(String state);
// returns the patient's state in all capital letters
String getPatientState( );
};
// assign values to constructor
PatientDemographicInformation::PatientDemographicInformation(String state)
{
patientState = state;
}
String PatientDemographicInformation::getPatientState( )
{
int i=0;
// ------The line below this is where my error occurs--------
char str[] = {patientState};
char c;
while (str[i])
{
c=str[i];
putchar (toupper(c));
i++;
}
return patientState;
}
This is just the function section of code from the header file. 'patientState' is defined in the constructor as a String. Let me know if I need to post more code. Please help in anyway you can.
Thanks - Josh
There is no such type as String in C++. If you mean C++/CLI then I think the return type should be declared as String ^. If it is simply a typo and you mean class std::string then it would be simpler to write
You don't need to convert your
String
to achar[]
: just process each character on the fly. Since you don't spell out the exact type andString
is sufficiently different fromstd::string
that it may be something different (in particular, the uppercase first character) it is unclear which operation can be used to access the characters within, however. In no case will you able to initialize achar str[]
, however: as a variable, this is a statically sized array with the size derived from the initialization.However, one thing you need to do is to make sure you only pass valid arguments to
toupper()
: this function consumes only positive values and the special valueEOF
. However,char
may be signed, i.e., you shall usetoupper()
like so:where
c
is achar
you obtained from somewhere.Look at Std::string::c_str this might be what you were looking for