Declaring string variables and functions

A declaration such as:

string dummystring;

or

string makeLineOut(DateTime n, sensors_event_t e, int cycle );
in my Arduino sketch (I’m trying to write this in C++ using platformio)

results in an error like:

Error GCC ‘string’ does not name a type 53:1
Error Build error: ‘string’ does not name a type

At the top of my program, I have included

stdio.h
ctype.h
string.h
Arduino.h

Can someone tell me what I’m doing wrong?

Add:

#include <string>
using namespace std;

Thank you for the suggestion.
I attempted to implement it as you can see in the code below. (Is this the only way to upload code? as a screen capture?)
I must be doing something wrong. The compiler rejects the string variable declaration (dummystring) as well as. the function to build and return the string, (makeLineOut).

I also notice that I must use "#include <string.h> rather than just or else I get a ‘no file found error’.

Ultimately, I am trying to construct a string (record) that is assembled by creating substrings using literals, and converted numeric values from a real time clock and sensor readings. In the loop, a record is created at regular intervals and written to a file on an SD card. (I haven’t gotten that far yet),

I would upload the entire code if I could figure out how to do it.

I think the “string” functions all have to be with a “S” as in “String”.

Maybe you are getting the error because of the capitalization??

I think you has to use #include <string> instead of #include <string.h> for the standard c++ library. Arduino has also a buildin library, you have to use ‘String’ instead of ‘string’, the include of string.h is not necessary when using the Arduino function. Have also a look the Arduino Reference https://www.arduino.cc/en/Reference/StringObject.

Thank you. That explains a lot. I noticed a while ago that using “String” instead of “string” allowed me to do what I wanted. It provided the member functions for things like concatenation, etc. But the uppercase “S” was not consistent with what I was seeing in the reference (e.g., cplusplus.com). I wanted to write the code as “vanilla” as possible in case I wanted to move it to another board. (Another reason why I moved the development from the Arduino IDE to PIO). So I began chopping #includes out and trolling through *.h files to find the library that was exposing that type. I didn’t finish but the trail was leading to Ardiuno.
So I’ll use the Arduino library for now.
Thanks again.

I got into this search I had trouble using snprintf to convert floats to the string component of the output record. I couldn’t get the formatted input to work. Floats would show up as “?” in the result of the call to snprintf.

Actually the String functions as you are using it, are local to Arduino only.
True, C has the string lbrary. the typedef “String” is exclusively an arduino library.

That is a normal behavior on small microcontrollers. Floats on printf and scanf blows up the code.
There are some tricks for the arduino ide, make a search in the Arduino forum.
You can try build_flags = -Wl,-u,_printf_float,-u,_scanf_float in the platformio.ini, it works with mbed but I don’t know if it also works on Arduino.

I finally managed to figure this out. The code below is a routine that builds a char array composed of a loop counter, date & time stamp from the Adafruit Data Logger RTC and x, y & z acceleration values from the Adafruit ADXL345 accelerometer running on Arduino Uno.
Despite the documentation in avr-libc regarding sprintf, I was unable to make char strings to concatenate together - these lines are commented out in the code. Note the “?” in the output below.

/* Make line out /
char
makeLineOut(DateTime now, sensors_event_t e, int nloop)
{
sprintf(kount, “LOOP %05u”, nloop);
strcpy(rcd,kount);
tmp = now.year()-2000;
sprintf(thisYear, " %02u",tmp);
strcat(rcd, thisYear);
sprintf(thisMonth, “%02u”, now.month());
strcat(rcd,thisMonth);
sprintf(thisDay, “%02u”,now.day());
strcat(rcd,thisDay);
tmp8 = now.hour() + 1;
//Add 1 for daylight savings time
sprintf(thisHour, " %02u:“, tmp8);
strcat(rcd, thisHour);
sprintf(thisMinute, “%02u:”, now.minute());
strcat(rcd, thisMinute);
sprintf(thisSecond, “%02u “, now.second());
strcat(rcd, thisSecond);
//Get the sensor readings
// sprintf(x_Val, " %08.3f”, (double)e.acceleration.x);
floatToString(x_Val, e.acceleration.x, prec,width);
strcat(rcd, x_Val);
// sprintf(y_Val, " %08.3f”, (double)e.acceleration.y);
floatToString(y_Val, e.acceleration.y, prec,width);
strcat(rcd, y_Val);
// sprintf(z_Val, " %08.3f”, (double)e.acceleration.z);
floatToString(z_Val, e.acceleration.z, prec,width);
strcat(rcd, z_Val);

    // #if ECHO_TO_SERIAL
    //   Serial.println(rcd);
    // #endif
    return rcd;
  }

Taking SSTAUB’s advice, I searched on the Arduino forum and found the function “floatToString” among other solutions offerend by people facing a similar problem. Using that function to get the 3 float valies for x, y & z in the code above, I now get the correct output below:

Oddly, using the sprintf function, the compiler would throw a warning that I was supplying a float to a function that was expecting a double. Not according to the AVR-libc reference. Casting the float values as doubles got rid of the warning, but still did not produce the correct result. The floatToString function works whether the cast to double is present or not.

According to the Adafruit Sensor template, the acceleration values are given as float.