0

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

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Joshua Cummings
  • 135
  • 2
  • 14
  • Is your intention to print the string in uppercase to the console/stdout, to return a `String` object that is a copy of `patientState` but all uppercase, or the change `patientState` in the current object? Also, please post the constructors for `String`. We'll need to see whether there's one that takes `char*` or not. – chwarr Nov 24 '13 at 23:23
  • What's this `String`? This is no standard type. – Johan Nov 24 '13 at 23:24
  • 2
    Can you post the definition for `String`, given it's not `std::string`? – Joe Z Nov 24 '13 at 23:44
  • Just did it in edit :D – Joshua Cummings Nov 24 '13 at 23:47
  • 2
    I still don't see the `String` class in the question. Are you sure you're not trying to use `std::string`? Even with a `using namespace std`, you'd still need to write `string` and not `String`. C++ is case-sensitive. (There are other ways to make `String` work, but instead of making us guess, show us.) – chwarr Nov 24 '13 at 23:49
  • Echoing what c45207 said: `class String` is not `std::string`, and is not defined by `#include `. If you are indeed using `std::string` (either through a `typedef` or `#define` or other mechanism), show it to us. – Joe Z Nov 25 '13 at 00:15
  • @JoeZ It's because I'm using a header file from my book for string. – Joshua Cummings Nov 25 '13 at 00:28
  • 2
    Can you provide us that header file? Without knowing how `class String` is _supposed_ to be manipulated, we can't help you manipulate the `class String`. This is the danger of having non-standard types for common things. No one can help you unless they are also familiar with the same non-standard thing. – Joe Z Nov 25 '13 at 00:29
  • @JoeZ Yeah I'm only using this header file because it is required for this project. I'd post the header file but it is extremely long. Is there a way to post a long section of code without having to indent each line? If not what could I look for in the header file that might help me? Thanks so much for your help thus far! – Joshua Cummings Nov 25 '13 at 00:47
  • Without knowing the interface, it's hard to say the best way to do this. Broadly, though: You do need to step through all of the characters of the original string, and call `toupper((unsigned char)c)` on them (where `c` is a variable holding the character), and insert those characters into a new string that you return from your `getPatientState` function. – Joe Z Nov 25 '13 at 01:01
  • 1
    To do better than that, we'd need to see the actual `String` interface and documentation, and honestly that's asking a bit much of StackOverflow. If this is coursework, then seek additional help from your school resources (instructors, TAs, fellow classmates). If this is not coursework, then whatever project this is should have documentation or other developers that can help you. – Joe Z Nov 25 '13 at 01:03
  • Okay, I understand. Thank you all for your help. I will try getting further information now that I understand why it isn't working with std::. – Joshua Cummings Nov 25 '13 at 01:56

3 Answers3

3

You don't need to convert your String to a char[]: just process each character on the fly. Since you don't spell out the exact type and String is sufficiently different from std::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 a char 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 value EOF. However, char may be signed, i.e., you shall use toupper() like so:

toupper(static_cast<unsigned char>(c))

where c is a char you obtained from somewhere.

Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
1

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

for ( char c : patientState ) putchar (toupper(c));
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 2
    You have all the chances to do it right! Why are you doing it wrong? The argument to `toupper()` has to be a positive value or `EOF`, i.e., you *have* to convert the `char` first to `unsigned char`! You could have just used `for ( unsigned char c: patientState ) putchar (toupper(c));`! (of course, using this code assumes that `String` has suitable `begin()` and `end()` members) – Dietmar Kühl Nov 24 '13 at 23:41
  • @Dietmar Kühl it is not wrong. The function depends on the current locale. From the C Standard: "If the argument is a character for which islower is true and there are one or more corresponding characters, as specified by the current locale, for which isupper is true, the toupper function returns one of the corresponding characters (always the same one for any giv en locale); otherwise, the argument is returned unchanged." – Vlad from Moscow Nov 25 '13 at 00:09
  • 2
    The problem is that a call to `toupper(c)` with a negative value, i.e., any character outside the ASCII range on platforms with 8 bit signed `char` has undefined behavior. Note that the described platforms are not exactly rare: Linux (at least on x86), SunOS, and Windows are among them. The relevant quote in the C standard (7.4 paragraph 1) is already in a comment to one of the other answers. – Dietmar Kühl Nov 25 '13 at 00:22
  • @ Dietmar Kühl Paragraph #1 says only that "In all cases the argument is an int, the value of which shall be representable as an unsigned char" Values of type char are representable in type unsigned char. So I did not see any problem. – Vlad from Moscow Nov 25 '13 at 04:41
  • 2
    The problem is that values like -1, -2, ... cannot be represented as an `unsigned char` because an `unsigned char` can't be negative. Thus, using `tolower(c)`, e.g., with `c == std::numeric_limits::min()` has undefined behavior on a system where `char` is signed. First converting `c` to `unsigned char` yields the correct result. – Dietmar Kühl Nov 25 '13 at 07:05
0

Look at Std::string::c_str this might be what you were looking for

Glenn Teitelbaum
  • 10,108
  • 3
  • 36
  • 80