Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
sprintf(rule_payload, "{\"state"\:{\"reported"\:{\"co2"\:\" %d "\, \"temperature"\:\" %f "\, \"humidity"\:\" %f" \, \"pressure"\ :\" %f "\, \"battery"\ :\" %d" \, \"name"\ :\" co2-sensor" \, \"timestamp"\ : \" %d" \, \"ttl"\: \" %d " \ }}}",co2,temperature,humidity,pressure,battery,now,delete_time);

What I have tried:

I've researched online on sprintf but I'm not finding the mistake. I know its probably going to be simple but i'm fairly new to this and trying to get my program running.
Posted
Updated 26-Jan-23 7:14am
Comments
KarstenK 26-Jan-23 10:44am    
it is better to split such "code worm" in pieces for maintainability and bug hunting.

sprintf(rule_payload, "{\"state"\:{
                               ^^ -- the escape should be before the quote.

There are a quite few others similarly misplaced:
sprintf(rule_payload, "{\"state"\:{\"reported"\:{\"co2"\:\" %d "\, \"temperature"\:\" %f "\, \"humidity"\:\" %f" \, \"pressure"\ :\" %f "\, \"battery"\ :\" %d" \, \"name"\ :\" co2-sensor" \, \"timestamp"\ : \" %d" \, \"ttl"\: \" %d " \ }}}",co2,temperature,humidity,pressure,battery,now,delete_time);
 
Share this answer
 
v2
To add to what Richard has said, you should expect to get syntax errors every day, probably many times a day while you are coding - we all do regardless of how much experience we have! Sometimes, we misspell a variable, or a keyword; sometimes we forget to close a string or a code block. Sometimes the cat walks over your keyboard and types something really weird. Sometimes we just forget how many parameters a method call needs.

We all make mistakes.

And because we all do it, we all have to fix syntax errors - and it's a lot quicker to learn how and fix them yourself than to wait for someone else to fix them for you! So invest a little time in learning how to read error messages, and how to interpret your code as written in the light of what the compiler is telling you is wrong - it really is trying to be helpful!

So read this: How to Write Code to Solve a Problem, A Beginner's Guide Part 2: Syntax Errors[^] - it should help you next time you get a compilation error!
 
Share this answer
 
I realize you have already solved your issue but I wanted to point something out to you. In the C language a single string can span multiple line. You have to start and end each substring with a quote and there can be no commas between substrings. In your case you have this monstrosity of a string :
"{\"state"\:{\"reported"\:{\"co2"\:\" %d "\, \"temperature"\:\" %f "\, \"humidity"\:\" %f" \, \"pressure"\ :\" %f "\, \"battery"\ :\" %d" \, \"name"\ :\" co2-sensor" \, \"timestamp"\ : \" %d" \, \"ttl"\: \" %d " \ }}}"
If I were you, I would split it into substrings with each label and value on a line. This would make it much easier to see the cause of the problem you had. Here is a start for the first few items :
"{"
    "\"state"\:"
    "{"
        "\"reported"\:"
        "{"
             "\"co2"\:\" %d "
             "\, \"temperature"\:\" %f "
             // more items here - comments can also be at the ends of lines
        "}"
    "}"
"}"
and so on... This is all one string and the newline characters are not included in it. I am not sure I did that correctly because commas and colons do not need to be escaped with backslash, only quotes and other special characters do. When each item is separated I think it is much easier to see the problems.
 
Share this answer
 
cout << R"("<- no need for escape ->"<-can even hit new line just watch
" see i hit new line and bingo presto no problemo special characters all over the place w/o escape ->' ->")";
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900