I need to write a statement that appends the word "Tournament" onto the end of my string variable sport2. I'm not sure exactly what commands to use to do that. If it was an array I know how to do that but its just a string. I tried using the .append() command but that didn't work. I'll take any suggestions.
And the other one I'm having difficult is also a string variable, not array, where I need to find if there is a int inside it. Not sure about how to go about doing that one easier. I know how to do it if its in an array but not just a string.
I went with strcat instead. Above is how I'm trying to use it but am still getting an error that there should be something before the '(' for some reason
Appending is as easy as doing: sport2 += "Tournament";
You can read integers from strings using std::ostringstream the same way as you read integers from std::ifstream and std::cin.
1 2 3 4 5 6 7 8 9 10 11
std::string str("123");
std::ostringstream oss(str);
int num;
if (oss >> num)
{
std::cout << "The number is " << num << ".\n";
}
else
{
std::cout << "No number!\n";
}
@raines883
I was using sport2.append(tourn); // for my append statement
And what diid occur?! Show a simple code where this statement did not work. because it is not clear what does mean the phrase "As far as the sport2 += "Tournament"; goes that didn't work for me."
Lol that was my fault, yea the sport2+= "Tournament"; works. For some reason I was working outside the main brackets.
The other part though is how do I go about seeing if string has a int in it?
I was having compiler problems but after fixing it the append worked for me. That and I accidently deleted a bracket so was working outside the main program.