assigment toInt() is not giving a real integer

I want to convert a string to an integer.
I use the example program with the toInt () command.
If I then want to write the converted value in an integer then I get the below error.

cannot convert ‘String’ to ‘int’ in initialization

What can I do to process the neiwue value as an integer?

see example program…

/*
String to Integer conversion

Reads a serial input string until it sees a newline, then converts the string
to a number if the characters are digits.

The circuit:

  • No external components needed.

created 29 Nov 2010
by Tom Igoe

This example code is in the public domain.

*/

String inString = “”; // string to hold input

void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}

// send an intro:
Serial.println("\n\nString toInt():");
Serial.println();
}

void loop() {
// Read serial input:
while (Serial.available() > 0) {
int inChar = Serial.read();
if (isDigit(inChar)) {
// convert the incoming byte to a char and add it to the string:
inString += (char)inChar;
}
// if you get a newline, print the string, then the string’s value:
if (inChar == ‘\n’) {
Serial.print(“Value:”);
Serial.println(inString.toInt());
Serial.print("String: ");
Serial.println(inString);
// clear the string for new input:
inString = “”;

int integer = inString;
}
}
}

You need to go look more closely at the example again. You are not using toInt() to assign to an int. You are directly assigning a Sting to an int, which you cannot do...

Regards,
Ray L.

Try 'integer = inString.toint()'. You converted it inside in the Serial.println but you didn't save the converted value anywhere.

Steve

When is give in the commend integer = inString.toint()

I become the eror ‘class String’ has no member named 'toint

Exercise care using the String class with Arduinos with small memories. The Evils of String page tells why. If you use c_strings (null terminated character arrays or strings [small s]) there will not be memory problems like Strings can cause.

Th atoi() function will convert a c_string to an integer.

The serial input basics tutorial shows how to receive serial data into a c_string in a non blocking fashion.

When is give in the commend integer = inString.toint()

I become the eror 'class String' has no member named 'toint

Post the code, all of it.

Post the error message. Please include the entire error message. It is easy to do. There is a button (lower right of the IDE window) called "copy error message". Copy the error and paste into a post in code tags. Paraphrasing the error message leaves out important information.

Case is important...

integer = inString.toInt()

Arduino:1.8.10 (Windows 10), Board:"WEMOS LOLIN32, 80MHz, Default, 240MHz (WiFi/BT), 921600"

C:\Users\wille\AppData\Local\Temp\arduino_modified_sketch_790802\convert_string_to_interger_met__toInt.ino: In function 'void loop()':

convert_string_to_interger_met__toInt:49:21: error: cannot convert 'String' to 'int' in initialization

int integer = inString;

^

exit status 1
cannot convert 'String' to 'int' in initialization

Dit rapport zou meer informatie bevatten met
"Uitgebreide uitvoer weergeven tijden compilatie"
optie aan in Bestand -> Voorkeuren.

Yes? Where's the toInt()?

NOTE WITH A CAPITAL I:

int integer = inString.toInt();

dear pcbbc

I do not follow you concerning the capital.
all the text is as it should be ??

the problem is that when i compile the programn and i leave the line
int integer = inString;
out than the program is working as i show above in the serial monitor.

As far as i expected the outcome from the .tolInt funcie should nb ean integer. as a test I have made the line

int integer = inString; to check if it is realy an integer. and when i wony to assign the valeu toan integer the command is giving the compiler error.

that is my problem, i thnig the outcome from .oInt() is not giving a interger ?
is my toutch wrong ?

Sorry I got the case wrong but plenty of people have corrected it for you.

In case you really still haven't got the idea, the line 'int integer = inString.toInt();' should REPLACE your line 'int integer = inString;'. It's that easy.

Steve

“REPORT TO MODERATOR” is not a regular reply option ;D

I think you meant to write something like this:

    if (inChar == '\n') 
    {
      int integer = inString.toInt();
      Serial.print("Value:");
      Serial.println(integer);

      Serial.print("String: ");
      Serial.println(inString);

      // clear the string for new input:
      inString = "";
    }