gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 08:48:02 24 4
gpt4 key购买 nike

#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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com