sprintf troubles

I am trying to use sprintf to post to twitter a message+the current power from a ted5000.
i have sprint f in my code like this:

char buffer[80];
sprintf(buffer, "Current power test =  %d", current);

but i keep getting this error message for the sprintf line:

postwpower:99: error: expected constructor, destructor, or type conversion before '(' token

any ideas as to why this is happening?

Maybe the error has to do something earlier in the code. Can you post the whole sketch?

here is the whole code. i can get it to post static messages fine such as “hello world” but with this new method to try and incorporate the current power it is just not working. sorry for the messiness of the code.

#include <SPI.h>
#include <Ethernet.h>
#include <Udp.h>
#include <EthernetDNS.h>
#include <Twitter.h>
#include <stdio.h>


int debugtw = 0;

int currentPower = 0;
// current power that will be recieved from gateway

double minPower = 0;
// thresholds that will be gathered from ui
double maxPower = 0;

int fade = 0;

int currentPower1 = 0;

int currentPower2 = 0;

int currentPower3 = 0;

double cp = 0;
String function;//what action is going to be performed
String number;//what the desired number is
String MaxPower;//the max power threshold
String MinPower;//the min power threshold
int user = 1;
int debug = 0;
int got = 0;
int debugxml = 1;
unsigned long gettime = 0;
unsigned long gettime2 = 0;
int resetcount;
String current;

int aa;
int ab;
int ac;
int ad;
int ae;
int af;
int ag;
int ah;
int ai;
int m = 1;
int l = 1;

int y = 0;
char xm[8] = {
  '<', 'P', 'O', 'W', 'E', 'R', '>'};
int ints[] = {
  aa,ab,ac,ad,ae,af,ag,ah,ai};
int count1;


// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {  
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 
  192,168,1,5 };
//byte server[] = { 173,194,33,104 }; // Google
byte server1[] = { 
  192,168,1,15 }; // TED5000
byte server2[] = {
  72,14,204,141 };
  
  

// Initialize the Ethernet client library
// with the IP address and port of the server 
// that you want to connect to (port 80 is default for HTTP):
Client client1(server1, 80);
Client client2(server2, 80);














// Your Token to Tweet (get it from http://arduino-tweet.appspot.com/)
Twitter twitter("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");

// Message to post


char buffer[80];
sprintf(buffer, "Current power test =  %d", current);













void xml(){
  String r;
  char c = 0;
  int x = 0;
  int powernow = 0;
  //perform these actions while m !=0
  do{
    if(debugxml == 1){
      Serial.println("inside x!=0");
    }
    c = client1.read();//c== the next character in the file  
    if( c == xm[x]){//if the current character == the array
      x++;//if the character is correct then increase x
      if(debugxml == 1){
        Serial.println("c==xm[x]");
      }
      if( c == '>'){
        if(debugxml == 1){
          Serial.println("c == >");
        }
        do{ 
          c = client1.read();//make c the next character
          if(c == '<'){
            if(debugxml == 1){
              Serial.println("c == <");
            }
            l = 0;//set l=0 to get out of this dowhile loop
            m = 0;//set m=0 to get out of other dowhuke loop
            c = 0;
            break;
          }//jump out of the while loop
          r += c;

          /**          ints[y] = d;//set the int in array ints equal to the current character.
           * y = y++;//move to the next int in ints
           * count1 = count1++;
           * Serial.print("count ");
           * Serial.println(count1);
           */          Serial.print("c");
          Serial.println(c);
        } 
        while (l != 0);
        if(debugxml == 1){
          Serial.println("out of l != 0");
        }
      }
    }
    else{
      if(debugxml == 1){
        Serial.println("else");
      }
      x = 0;
    };//if the character in incorrect reset the array counter

  } 
  while (m != 0);
  char t[r.length() + 1];
  r.toCharArray(t, sizeof(t));
  int d = atoi(t);
  current = r;
  powernow = d;
  Serial.println("cp ");
  Serial.println(cp);
  while(client1.available()){
    c = client1.read();
  }
  m = 1;
  l = 1;
  //this part takes the independent intergers and turns them into one number based on the number of ints taken
  /**  Serial.println(ab);
   * Serial.println(ac);
   * Serial.println(ad);
   * Serial.println(ae);
   * Serial.println(af);
   * Serial.println(ag);
   * Serial.println(ah);
   * powernow = pow(10, (count1-1))*aa;
   * if(count1 >1){
   * powernow = powernow + pow(10, (count1-2))*ab;
   * }
   * if(count1 >2){
   * powernow = powernow + pow(10, (count1-3))*ac;
   * }
   * if(count1 >3){
   * powernow = powernow + pow(10, (count1-4))*ad;
   * }
   * if(count1 >4){
   * powernow = powernow + pow(10, (count1-5))*ae;
   * }
   * if(count1 >5){
   * powernow = powernow + pow(10, (count1-6))*af;
   * }
   * if(count1 >6){
   * powernow = powernow + pow(10, (count1-7))*ag;
   * }
   * if(count1 >7){
   * powernow = powernow + pow(10, (count1-8))*ah;
   * }
   * if(count1 >8){
   * powernow = powernow + pow(10, (count1-9))*ai;
   * }
   * currentPower = powernow;
   */
  //  if(!client.connected()){
  //    client.stop();
  //  }
}



void getxml(){

  //  got = 1;

  delay(1000);
  if(debugxml == 1){  
    Serial.println("connecting...");
  }
  // if you get a connection, report back via serial:
  if (client1.connect()) {
    got = 1;
    resetcount = 0;

    Serial.println("connected");

    // Make call to TED500 to get LiveData
    // http://TED5000/api/LiveData.xml
    //http://TED5000/history/secondhistory.xml?MTU=1&COUNT=100&INDEX=10

    char getRequest[100];
    //  strcpy(getRequest, "GET /history/secondhistory.xml?MTU=1&COUNT=100&INDEX=10");
    //  strcpy(getRequest, "GET /api/LiveData.xml");
    strcpy(getRequest, "GET /history/minutehistory.xml?MTU=0&COUNT=10&INDEX=1");

    strcat(getRequest, " HTTP/1.0");
    client1.println(getRequest);
    //    client.println("GET /search?q=arduino HTTP/1.0");
    client1.println();
    //    got = 1;
  } 
  else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
    resetcount = resetcount + 1;


    //    got = 0;
  }
  if(debugxml == 1){  
    Serial.println("outget");

  }

}



void tw(){

if(debugtw = 1){
  Serial.println("connecting ...");}
  if (twitter.post(buffer)) {
    // Specify &Serial to output received response to Serial.
    // If no output is required, you can just omit the argument, e.g.
    // int status = twitter.wait();

    int status = twitter.wait();
    if(debugtw = 1){
      int status = twitter.wait(&Serial);
    if (status == 200) {
      Serial.println("OK.");
    } }
    else {
      if(debugtw = 1){
      Serial.print("failed : code ");
      Serial.println(status);}
    }
  } 
  else {if(debugtw = 1){
    Serial.println("connection failed.");}
  }
}






void setup()
{
  Serial.begin(9600);
  Ethernet.begin(mac, ip);
  Serial.println(gettime);
    getxml();
    xml();
    tw();
}

void loop(){}

the statement - sprintf(buffer, "Current power test = %d", current); - is outside any function. This is not allowed in C .

Furthermore - current - is defined as String and you try to use it with %d in the sprintf(). %d is for formatting integer values. for a string you should use %s .

Solution is to define a function that fills the buffer appropiately.

thanks i did that but now i have another problem. here is my new code:

#include <SPI.h>
#include <Ethernet.h>
#include <Udp.h>
#include <EthernetDNS.h>
#include <Twitter.h>
#include <stdio.h>

//char buffer[]= "";
int debugtw = 1;

int currentPower = 0;
// current power that will be recieved from gateway

double minPower = 0;
// thresholds that will be gathered from ui
double maxPower = 0;

int fade = 0;

int currentPower1 = 0;

int currentPower2 = 0;

int currentPower3 = 0;

double cp = 0;
String function;//what action is going to be performed
String number;//what the desired number is
String MaxPower;//the max power threshold
String MinPower;//the min power threshold
int user = 1;
int debug = 0;
int got = 0;
int debugxml = 1;
unsigned long gettime = 0;
unsigned long gettime2 = 0;
int resetcount;
String current;

int aa;
int ab;
int ac;
int ad;
int ae;
int af;
int ag;
int ah;
int ai;
int m = 1;
int l = 1;

int y = 0;
char xm[8] = {
  '<', 'P', 'O', 'W', 'E', 'R', '>'};
int ints[] = {
  aa,ab,ac,ad,ae,af,ag,ah,ai};
int count1;


// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {  
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 
  192,168,1,5 };
//byte server[] = { 173,194,33,104 }; // Google
byte server1[] = { 
  192,168,1,15 }; // TED5000
byte server2[] = {
  72,14,204,141 };
  
  

// Initialize the Ethernet client library
// with the IP address and port of the server 
// that you want to connect to (port 80 is default for HTTP):
Client client1(server1, 80);
Client client2(server2, 80);








String currentmsg = "Currentpowertest = " + current;


//int sprintf(	char * 	__s, const char * __fmt, ... );



// Your Token to Tweet (get it from http://arduino-tweet.appspot.com/)
Twitter twitter("XXXXXXXXXXXXXXXXXXXXXXXXXXXXX");

// Message to post


char buffer[80];



void xml(){
  String r;
  char c = 0;
  int x = 0;
  int powernow = 0;
  //perform these actions while m !=0
  do{
    if(debugxml == 1){
      Serial.println("inside x!=0");
    }
    c = client1.read();//c== the next character in the file  
    if( c == xm[x]){//if the current character == the array
      x++;//if the character is correct then increase x
      if(debugxml == 1){
        Serial.println("c==xm[x]");
      }
      if( c == '>'){
        if(debugxml == 1){
          Serial.println("c == >");
        }
        do{ 
          c = client1.read();//make c the next character
          if(c == '<'){
            if(debugxml == 1){
              Serial.println("c == <");
            }
            l = 0;//set l=0 to get out of this dowhile loop
            m = 0;//set m=0 to get out of other dowhuke loop
            c = 0;
            break;
          }//jump out of the while loop
          r += c;

          /**          ints[y] = d;//set the int in array ints equal to the current character.
           * y = y++;//move to the next int in ints
           * count1 = count1++;
           * Serial.print("count ");
           * Serial.println(count1);
           */          Serial.print("c");
          Serial.println(c);
        } 
        while (l != 0);
        if(debugxml == 1){
          Serial.println("out of l != 0");
        }
      }
    }
    else{
      if(debugxml == 1){
        Serial.println("else");
      }
      x = 0;
    };//if the character in incorrect reset the array counter

  } 
  while (m != 0);
  char t[r.length() + 1];
  r.toCharArray(t, sizeof(t));
  int d = atoi(t);
  current = r;
  powernow = d;
  Serial.println("cp ");
  Serial.println(cp);
  while(client1.available()){
    c = client1.read();
  }
  m = 1;
  l = 1;
  //this part takes the independent intergers and turns them into one number based on the number of ints taken
  /**  Serial.println(ab);
   * Serial.println(ac);
   * Serial.println(ad);
   * Serial.println(ae);
   * Serial.println(af);
   * Serial.println(ag);
   * Serial.println(ah);
   * powernow = pow(10, (count1-1))*aa;
   * if(count1 >1){
   * powernow = powernow + pow(10, (count1-2))*ab;
   * }
   * if(count1 >2){
   * powernow = powernow + pow(10, (count1-3))*ac;
   * }
   * if(count1 >3){
   * powernow = powernow + pow(10, (count1-4))*ad;
   * }
   * if(count1 >4){
   * powernow = powernow + pow(10, (count1-5))*ae;
   * }
   * if(count1 >5){
   * powernow = powernow + pow(10, (count1-6))*af;
   * }
   * if(count1 >6){
   * powernow = powernow + pow(10, (count1-7))*ag;
   * }
   * if(count1 >7){
   * powernow = powernow + pow(10, (count1-8))*ah;
   * }
   * if(count1 >8){
   * powernow = powernow + pow(10, (count1-9))*ai;
   * }
   * currentPower = powernow;
   */
  //  if(!client.connected()){
  //    client.stop();
  //  }
}



void getxml(){

  //  got = 1;

  delay(1000);
  if(debugxml == 1){  
    Serial.println("connecting...");
  }
  // if you get a connection, report back via serial:
  if (client1.connect()) {
    got = 1;
    resetcount = 0;

    Serial.println("connected");

    // Make call to TED500 to get LiveData
    // http://TED5000/api/LiveData.xml
    //http://TED5000/history/secondhistory.xml?MTU=1&COUNT=100&INDEX=10

    char getRequest[100];
    //  strcpy(getRequest, "GET /history/secondhistory.xml?MTU=1&COUNT=100&INDEX=10");
    //  strcpy(getRequest, "GET /api/LiveData.xml");
    strcpy(getRequest, "GET /history/minutehistory.xml?MTU=0&COUNT=10&INDEX=1");

    strcat(getRequest, " HTTP/1.0");
    client1.println(getRequest);
    //    client.println("GET /search?q=arduino HTTP/1.0");
    client1.println();
    //    got = 1;
  } 
  else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
    resetcount = resetcount + 1;


    //    got = 0;
  }
  if(debugxml == 1){  
    Serial.println("outget");

  }

}



void tw(){

if(debugtw = 1){
  Serial.println("connecting ...");}
  if (twitter.post(buffer)) {
    // Specify &Serial to output received response to Serial.
    // If no output is required, you can just omit the argument, e.g.
    // int status = twitter.wait();


      int status = twitter.wait(&Serial);
    if (status == 200) {
      Serial.println("OK.");
    } 
    else {
      if(debugtw = 1){
      Serial.print("failed : code ");
      Serial.println(status);}
    }
  } 
  else {if(debugtw = 1){
    Serial.println("connection failed.");}
  }
}






void setup()
{
    Serial.begin(9600);
  Serial.println("start");
  sprintf(buffer, "Current power test =  %s", current);
  Serial.println("start2");

  Ethernet.begin(mac, ip);

    getxml();
    xml();
    tw();
}

void loop(){}

it compiles and uploads fine and prints start but never gets to print start2.

however i have discovered something that may be the cause. i dont seem to have stdio.h. i googled it and found it on the site nongnu.org but i am unable to open their download files. (if that even is the right file) where else can i get it from?

The call to sprintf needs to happen inside a function.

isn't setup a function?

In setup(), you call sprintf to convert the value that is in current to a string. The current variable is a String object that has not been assigned a value. What exactly are you expecting sprintf to do?

i am trying to do what you had talked about in a previous post. Arduino Forum

i can make current either a string or an int what ever it needs to be to work. and again i still dont have stdio.h, where can i get it?

thanks, i figured it out.