String & C-String Help

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.
Show how you used string member function append.
strcat(sport2,s9);

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";
}
Nevertheless show how you used string member function append because I can not even imagine how it could be called that it would not work.
As far as the sport2 += "Tournament"; goes that didn't work for me.

And for the second part is there an easier way to read digits using string or c-string references?

I was using sport2.append(tourn); // for my append statement
Last edited on
As far as the sport2 += "Tournament"; goes that didn't work for me.


Seems fine to me.
1
2
3
4
5
6
7
8
9
10
11
12
#include <string>
#include <iostream>

using namespace std;

int main()
{
  string sport2("words ");
  cout << sport2 << endl;
  sport2+="on toast";
  cout << sport2 << endl;
}
@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."
Last edited on
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.
Last edited on
Topic archived. No new replies allowed.