Hi, i have made a code. Its meant to announce a "Sentence" but i dont know how to make a string into a sentece. The string up to now only announces the first word of the sentece.

//Announce
}else if(memcmp((void*)lpcLine, "/announce ", 10) == 0){
 bRet = false;
 char AMsg[200];
 sscanf(lpcLine, "/announce %s", &AMsg);
 PostAnnounce(AMsg);

eg: /announce Hello Their

and all i see then is

Hello

instead of Hello Their

sscanf is delimited by whitespace. You'll get the first word only unless you use some other trick. Personally, I'd say that you should ditch sscanf altogether and to it manually so that you have more control. But you can also use a scanset if you want. For example, to read anything up to sentence punctuation, you could do something like this.

sscanf(lpcLine, "/announce %[^.!?]", AMsg);

But it's really tricky to get stuff like that right and keep it safe. Once again I'll recommend that you forget about sscanf and do it yourself with a loop or something. :)

Comments
Good posts recently. :) -joeprogrammer

First off the question is about C++ strings and the code is using C style I/O and C style strings.

To read in a string containing whitespace (for example, a sentence containing more than 1 word) using C I'd suggest fgets(), though if you want to endure some heckling from the peanut gallery, gets() is an alternative.

To read in a string containing whitespace using C style string in C++ I'd use the istream getline() method.

To read in a string containing whitespace using C++ style (that is STL) strings using C++ I'd use the getline() function.

The syntax for the 2 getline() routines are different, so you have to specify what you are trying to do with what.

First off the question is about C++ strings and the code is using C style I/O and C style strings.

To read in a string containing whitespace (for example, a sentence containing more than 1 word) using C I'd suggest fgets(), though if you want to endure some heckling from the peanut gallery, gets() is an alternative.

To read in a string containing whitespace using C style string in C++ I'd use the istream getline() method.

To read in a string containing whitespace using C++ style (that is STL) strings using C++ I'd use the getline() function.

The syntax for the 2 getline() routines are different, so you have to specify what you are trying to do with what.

The source is a string, so neither fgets() nor gets() will work. Likewise, the istream getline() method won't work. You can create a stringstream and use the getline functions with it, but there's still a problem in defining a delimiter. I don't think a one liner solution is a good idea if you want it to be robust.

Hi, i have made a code. Its meant to announce a "Sentence" but i dont know how to make a string into a sentece. The string up to now only announces the first word of the sentece.

I would strongly recommend you to go along with Mr. Raye's advice. But still if you insist on doing it the hard way, just for the sake of doing it, here is a untested, quick fix ....

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define LIMIT_LENGTH 6

int main( void )
{
    char ref_str[] = "/announce " ;
    char lpcLine[] = "/announce Hello there" ;
    char AMsg[200]= { '\0' };

    int limit = strlen(lpcLine) - strlen( ref_str ) ;
    char str_limit[LIMIT_LENGTH] = { '\0' } ;
    char format[BUFSIZ] = { '\0' } ;

    strcpy(str_limit,itoa(limit, str_limit, 10)) ;
    strcpy(format, ref_str) ;
    strcat( format, "%" ) ;
    strcat( format, str_limit);
    strcat( format, "c" ) ;

    sscanf( lpcLine, format, AMsg ) ;
    printf( "%s", AMsg ) ;

    return 0 ;
}

So the original poster is trying to prepend the word announce to a C style string called lpcLine storing the new string in AMsg and subsequently printing the value of AMsg to the screen, or wherever, in PostAnnounce()? Fair enough. Sorry for the irrelevent post.

I don't think it was an irrelevant post.

The OP did a pissy poor job of explaining his original intentions. You could have interpretated it to mean anything to be honest. Those who say otherwise are being pedantic.

I don't think it was an irrelevant post.

The OP did a pissy poor job of explaining his original intentions. You could have interpretated it to mean anything to be honest. Those who say otherwise are being pedantic.

I don't think it was an irrelevant post either, but not for the same reason. The title was confusing, but the problem definition was very clear even though it took a little piecing together. ;)

Something that is very clear doesn't require piecing together.

But if you think it was as clear as day good luck to you.

Something that is very clear doesn't require piecing together.

Only in an ideal world. :lol:

But if you think it was as clear as day good luck to you.

Not as clear as day, but certainly not piss poor. He described the problem adequately and the code displayed the error. Whether through luck or planning, the question was answerable. Then again, I might just be good at deciphering these things. ;)

Erm how is the big "c++ strings" title got anything to do with the code he wrote?

So yes it was confusing.

Anyway, like I said well done. What more do you want? A cookie. :sad:

Erm how is the big "c++ strings" title got anything to do with the code he wrote?

Titles along the lines of "Heeeeelp!" aren't any better, but we figure them out from the posts, right? :)

Anyway, like I said well done. What more do you want? A cookie. :sad:

I'm not interested in recognition. I'm trying to explain why I think your comment about the post being piss poor was uncalled for. But I'm also not interested in fighting, so let's just leave it before things risk getting out of hand.

Titles along the lines of "Heeeeelp!" aren't any better, but we figure them out from the posts, right?

Erm I don't understand what you're trying to prove? You say the post was clear enough to understand, even though the OP talks of c++ and strings in his title when he clearly uses c code for his example.

Its meant to announce a "Sentence" but i dont know how to make a string into a sentece

Ok so what does "announce a sentence" mean? That's right it doesn't really mean anything.

So either his post was clear or not? From that statement, I'd say his post was unclear. :cry:

@Ravalon and Iamthwee

Please stop at this right now otherwise this thread would just end up being closed.....

Its good if conversations flow but going in the same flame loop round and round doesn't actually help anyone's cause.

Thank you.

Hi, i have made a code. Its meant to announce a "Sentence" but i dont know how to make a string into a sentece. The string up to now only announces the first word of the sentece.

//Announce
}else if(memcmp((void*)lpcLine, "/announce ", 10) == 0){
 bRet = false;
 char AMsg[200];
 sscanf(lpcLine, "/announce %s", &AMsg);
 PostAnnounce(AMsg);

eg: /announce Hello Their

and all i see then is

Hello

instead of Hello Their

This would be best done using strcpy() and strcat() since you are using C-strings. Leave sscanf() for conversion from strings into numbers.

This article has been dead for over six months. Start a new discussion instead.