Converting strings to uppercase C++

Mike_hunt69 :
#include <iostream>
#include <string>
#include <cctype>

using namespace std;

int main()
{
    string colour;
    int iNum;
    int iLoop;
    string sTemp;

    sTemp = "";
    iNum = 0;
    iLoop = 0;


    cout << "Input a colour: ";
    cin >> colour;

    if ((colour != "green") && (colour != "yellow") && (colour != "orange") && (colour != "blue") && (colour != "purple") && (colour != "red"))
        {
            cout << "Colour not found" << endl;
        }
    else
        {
            cout << "Input a number";
            cin >> iNum;
        }   


    if ((colour == "blue") || (colour == "red") || (colour == "yellow")) 
        {
            switch (iNum)
                {
                    case 1: 

                        cout << "yellow,orange,red,purple,blue,green" << endl;
                        break;

                    case 2:

                        cout << colour << endl;
                        break;

                    case 3:

                        if (colour == "yellow")
                            {
                                cout << "red" << endl;
                            }        

                        if (colour == "red")
                            {
                                cout << "blue" << endl;
                            }    

                        if (colour == "blue")
                            {
                                cout << "yellow" << endl;
                            }    
                }
        }

    if ((colour == "orange") || (colour == "purple") || (colour == "green"))
        {
            switch (iNum)
                {
                    case 1:
                    {
                        cout << "green,red,purple" << endl;
                        break;
                    }
                    case 2:

                        for (iLoop = 0; iLoop < colour.length(); iLoop++)
                            {
                                sTemp = sTemp + toupper(colour[iLoop]);
                            }

                        cout << sTemp << endl;    
                        break;

                    case 3:

                        if (colour == "green")
                            {
                                cout << "orange" << endl;
                            }

                        if (colour == "orange")
                            {
                                cout << "purple" << endl;
                            }    

                        if (colour == "purple")
                            {
                                cout << "green" << endl;
                            }    

                }
        } 

        return 0;      
}   

I have a problem running this code. I receive the following error:

color.cpp: In function 'int main()':
color.cpp:79:47: error: no match for 'operator+' (operand types are 'std::__cxx11::string {aka std::__cxx11::basic_string<char>}' and 'int')
                                 sTemp = sTemp + toupper(colour[iLoop]);
                                         ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~

Basically what I am trying to do is convert the lower case variable - colour into uppercase. Please keep in mind that I am a beginner hence my C++ knowledge is extremely limited and I would greatly appreciate it if someone could suggest an easier method to use to convert strings into uppercase without having to run a loop through the string and converting character by character. Any assistance would be greatly appreciated.

anastaciu :

toupper(colour) returns an int you cannot add it (concatenate it) to a string.

For it to work you'll need to cast it to char

sTemp = sTemp + static_cast<char>(toupper(colour[iLoop]));

operator + concatenates two strings or a string and a char

https://en.cppreference.com/w/cpp/string/basic_string

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=33191&siteId=1