String to int Example


i'm beginner c/c++ code i'm working through "examples" given under "learning". 1 of these called 'string int' doesn't seem work if cut , paste compiler. here code
code: [select]


/*
  string integer conversion

reads serial input string until sees newline, converts
string number if characters digits.

circuit:
no external components needed.

created 29 nov 2010
tom igoe

example code in public domain.
*/

string instring = "";    // string hold input

void setup() {
  // open serial communications , wait port open:
  serial.begin(9600);
  while (!serial) {
    ; // wait serial port connect. needed leonardo only
  }

  // send 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 incoming byte char
      // , add string:
      instring += (char)inchar;
    }
    // if newline, print string,
    // string's value:
    if (inchar == '\n') {
      serial.print("value:");
      serial.println(instring.toint());
      serial.print("string: ");
      serial.println(instring);
      // clear string new input:
      instring = "";
    }
  }
}


i think wrong should have #include< ctype.h> isdigit work i've tried , still no output serial monitor after entering numbers in.  thought ctype library included anyway maybe instring.toint  @ fault. please suggest reason why snippet fails run?  thank you

what out put , input did give it?

mark


Arduino Forum > Using Arduino > Programming Questions > String to int Example


arduino

Comments

Popular posts from this blog

VIDIOC_S_FMT error 16, Device or resource busy - Raspberry Pi Forums

using a laptop skeleton to build a pi laptop - Raspberry Pi Forums

Forum for Joomla? - Joomla! Forum - community, help and support