c++ - 将字符串转换为大写 C++

标签 c++

#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;      
}   

我运行此代码时遇到问题。我收到以下错误:

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]);
                                         ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~

基本上我想做的是将小写变量 - color 转换为大写。请记住,我是一个初学者,因此我的 C++ 知识非常有限,如果有人能建议一种更简单的方法来将字符串转换为大写,而无需运行字符串循环并逐个字符转换,我将不胜感激。任何帮助将不胜感激。

最佳答案

https://stackoverflow.com/a/735215/7175167此链接解释了如何操作。

#include <string>
#include <algorithm>

int main() {
    //simply replace the 'str' with 'colour'
    std::string str = "something";
    //this will make the string into upper case
    std::transform(str.begin(), str.end(), str.begin(), ::toupper);
    std::cout << str << std::endl;
    return 0;
}

关于c++ - 将字符串转换为大写 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60578429/

相关文章:

c++ - Qt metaObject->indexOfMethod 总是返回 -1

c++ - 如何将对象放置在堆以外的其他地方?

c++ - BITMAPFILEHEADER 的问题

c++ - Electron native 添加 : DLL initialization routine failed

c++ - 如何获取 UPS 的状态?

c++ - 提取十六进制数的 'parts'

c++ - 内存泄漏单元测试 C++

c++ - 构造函数失败后初始化 boost::asio 套接字

c++ - 在exe中编译静态库

c++ - 为什么 std::map::operator[] 如此违反直觉?