Reputation: 35
#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.
Upvotes: 2
Views: 9945
Reputation: 23802
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
Upvotes: 0
Reputation: 616
https://stackoverflow.com/a/735215/7175167 This link explains how to do it.
#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;
}
Upvotes: 5