0

I have tried Googling and trying everything I can think of, but I can't overcome the problem. So I have come to the masters at StackOverflow!

I am having trouble using the .at() string method:

std::string letters = "QWERTYUIO";

// &(letters.at(0)) now evaluates to QWERTYUIO
// &(letters.at(5)) now evaluates to YUIO
// etc.

What I want is the nth character of the string, not a string starting at the nth character.

I tried the [] operator, but I now have a new problem. The code is currently (using the Allegro library, but I don't think that is affecting this):

std::string letters = "QWERTYUIO";
char letter;
char StringY[5];
int row = 0;

for(int l = 0; l <= 2; ++l){
    letter = letters[row + l];

    textout_ex(buffer, font, &letter, l * 30, 20, makecol(255, 255, 255), -1);

    sprintf(StringY, "%d", inventoryNum[row + l + 9]);
    textout_ex(buffer, font, StringY, l * 30, 20, makecol(255, 255, 255), -1);
}

What is happening is the letter variable seems to be appending the StringY of the previous iteration to itself for no apparent reason. This is driving me insane.

Any help is appreciated.

Many thanks,

Will.

1
  • @swegi Sorry, forgot to put that in my question! The value of row is 0. Commented Jul 3, 2011 at 13:06

4 Answers 4

9

at() returns a reference to the specified character in the string, so when you take its address with &, you get a pointer to that character (of type char *).

The problem is that you are then giving this pointer to something that expects a pointer to an array of chars. This also has the type char * so it type checks just fine, but it gets treated as a character array rather than a single character.

You probably want to just drop the & and use letters.at(5).


Some people suggested using operator[] instead. It would still cause the same issue if you wrote &(letters[5]). The difference between at() and operator[] is that the former does bounds checking while the latter does not.

Sign up to request clarification or add additional context in comments.

3 Comments

@hammar What a fantastic answer. Thank for your help! The only problem is that the Allegro function 'textout_ex' expects a 'const char*' parameter, so it fires up an error message when attempting to put 'letters.at(row + l)' in. Many thanks, though!
@Will: In that case, you simply need to convert your single character to a string containing a single character. One way of doing this is char foo[2] = { letters.at(5), '\0' }; and then just give foo to textout_ex.
@hammar It worked! Thanks for all your help, I am eternally grateful!
3

see string::at

use it just as:

(letters.at(0));
(letters.at(5));

Comments

2

Use operator[].

char c = letters[5];

Edit: Your problem is the Allegro library. It's decades old and written in C. You really need something from the current century. For example, with your sprintf, then I sure hope it doesn't need more than 5 characters, and you never wrote a 0 terminator or anything like that.

You should move to something more modern, like Direct3D itself. D3DX has a Font class that can render text transparently without the user having to manually write it to a texture, and you can use your own C++ string handling instead of the disgusting handling in the code posted. This is at least a small improvement:

std::string letters = "QWERTYUIO";
std::stringstream strstream;    
for(int l = 0, row = 0; l <= 2; ++l){
    char letter;
    letter = letters[row + l];

    textout_ex(buffer, font, &letter, l * 30, 20, makecol(255, 255, 255), -1);

    strstream << inventoryNum[row + l + 9];
    textout_ex(buffer, font, strstream.str().c_str(), l * 30, 20, makecol(255, 255, 255), -1);
    strstream.clear();
}

1 Comment

This has caused a new problem, which I have edited into the initial question. Thanks very much for your quick response, and any further help is also appreciated.
0

Just say letters[5], or whatever position you need.

2 Comments

This has caused a new problem, which I have edited into the initial question. Thanks very much for your quick response, and any further help is also appreciated.
@Will: You're using textout_ex, whose third argument has to be a pointer to a null-terminated char array. But letters[5] is just a single char! You could say something like char letter[2] = { letters[5], '\0' };, or be fancy and use substr as in letters.substr(5, 1).c_str().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.