Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Putting declared const into a print statement

Putting declared const into a print statement

Scheduled Pinned Locked Moved C / C++ / MFC
html
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SEmmett
    wrote on last edited by
    #1

    So, I have this snippet of code that formats an html table data cell:

            if(aSensor\[i\].sensorSwitchState == lowState) {
             client->print("");
            } else {
             client->print(""); //
            }
    

    I have each background-color: hardcoded and the subsequent printing output is as I was expecting. However, I would like to replace the entry - lawngreen and/or red - with a const (or #define) so that the entry is replaced with the color name. I've tried #define GREEN lawngreen ...

    client->print("")

    and that doesn't work. I've tried const char acolor[]="lawngreen";

    client->print("")

    and that doesn't work. I know there has to be a way to do this, but I can't seem to figure it out. Any ideas how I go about making a declaration that will let me insert the in the appropriate place would be greatly appreciated. TNX

    K CPalliniC 2 Replies Last reply
    0
    • S SEmmett

      So, I have this snippet of code that formats an html table data cell:

              if(aSensor\[i\].sensorSwitchState == lowState) {
               client->print("");
              } else {
               client->print(""); //
              }
      

      I have each background-color: hardcoded and the subsequent printing output is as I was expecting. However, I would like to replace the entry - lawngreen and/or red - with a const (or #define) so that the entry is replaced with the color name. I've tried #define GREEN lawngreen ...

      client->print("")

      and that doesn't work. I've tried const char acolor[]="lawngreen";

      client->print("")

      and that doesn't work. I know there has to be a way to do this, but I can't seem to figure it out. Any ideas how I go about making a declaration that will let me insert the in the appropriate place would be greatly appreciated. TNX

      K Offline
      K Offline
      k5054
      wrote on last edited by
      #2

      Try

      \#define GREEN "lawngreen"
      client->print("");

      1 Reply Last reply
      0
      • S SEmmett

        So, I have this snippet of code that formats an html table data cell:

                if(aSensor\[i\].sensorSwitchState == lowState) {
                 client->print("");
                } else {
                 client->print(""); //
                }
        

        I have each background-color: hardcoded and the subsequent printing output is as I was expecting. However, I would like to replace the entry - lawngreen and/or red - with a const (or #define) so that the entry is replaced with the color name. I've tried #define GREEN lawngreen ...

        client->print("")

        and that doesn't work. I've tried const char acolor[]="lawngreen";

        client->print("")

        and that doesn't work. I know there has to be a way to do this, but I can't seem to figure it out. Any ideas how I go about making a declaration that will let me insert the in the appropriate place would be greatly appreciated. TNX

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #3

        This

        #define GREEN "lawngreen"
        client->print("")

        should work. There might be more elegant ways, depending on print function definition.

        In testa che avete, signor di Ceprano?

        S 1 Reply Last reply
        0
        • CPalliniC CPallini

          This

          #define GREEN "lawngreen"
          client->print("")

          should work. There might be more elegant ways, depending on print function definition.

          S Offline
          S Offline
          SEmmett
          wrote on last edited by
          #4

          To both K5054 and Cpallini: Thanks, that gets the job done!! It turns out I had tried #define GREEN "lawngreen" but then wrote the code escaping the quotes:

          client->print("");

          I had also tried

          client->print("");

          but that didn't work. Its the space after the leading quote that does the trick.

          client->print("");

          The trailing quote doesnt need a space before it. Gotta think on that one a bit (why the quotes and their spacing is important but escaping the quotes is not) :)

          K 1 Reply Last reply
          0
          • S SEmmett

            To both K5054 and Cpallini: Thanks, that gets the job done!! It turns out I had tried #define GREEN "lawngreen" but then wrote the code escaping the quotes:

            client->print("");

            I had also tried

            client->print("");

            but that didn't work. Its the space after the leading quote that does the trick.

            client->print("");

            The trailing quote doesnt need a space before it. Gotta think on that one a bit (why the quotes and their spacing is important but escaping the quotes is not) :)

            K Offline
            K Offline
            k5054
            wrote on last edited by
            #5

            In C/C++ consecutive string literals are catenated together

            cout << "hello " "world" << endl;
            cout << "hello world" << endl;

            both produce the same thing. I don't know why the C++11 standard requires a space between a literal and a string macro, but it probably has something to do with ambiguous parsing when no space is present. As for the escaped quotes, if you mean

            client->print("");

            recall that \" embeds a double quote in a string, much like \n or \t embeds a new-line or a tab. If that's not what you were referring to, then maybe you could explain it better?

            S 1 Reply Last reply
            0
            • K k5054

              In C/C++ consecutive string literals are catenated together

              cout << "hello " "world" << endl;
              cout << "hello world" << endl;

              both produce the same thing. I don't know why the C++11 standard requires a space between a literal and a string macro, but it probably has something to do with ambiguous parsing when no space is present. As for the escaped quotes, if you mean

              client->print("");

              recall that \" embeds a double quote in a string, much like \n or \t embeds a new-line or a tab. If that's not what you were referring to, then maybe you could explain it better?

              S Offline
              S Offline
              SEmmett
              wrote on last edited by
              #6

              Frankly, I had forgotten the concatenation example you show. As for your escaped quotes, yes, I was referring to the use of the \ to allow the explicit use of the the following ". I thought that since the contents of the print statement were already delineated by leading and trailing quotes, that I had to use the \ to permit the use of additional quotes between those two "bookend" quotes. Thanks for setting me straight. Its been a good day - I learned something. Thank you!!

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups