You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cin_cout.ino 804B

1234567891011121314151617181920212223242526272829303132333435
  1. /*
  2. * Demo of ArduinoInStream and ArduinoOutStream
  3. */
  4. #include <SPI.h>
  5. #include <SdFat.h>
  6. // create serial output stream
  7. ArduinoOutStream cout(Serial);
  8. // input line buffer
  9. char cinBuf[40];
  10. // create serial input stream
  11. ArduinoInStream cin(Serial, cinBuf, sizeof(cinBuf));
  12. //------------------------------------------------------------------------------
  13. void setup() {
  14. Serial.begin(9600);
  15. while (!Serial) {} // wait for Leonardo
  16. }
  17. //------------------------------------------------------------------------------
  18. void loop() {
  19. int32_t n;
  20. cout << "\nenter an integer\n";
  21. cin.readline();
  22. if (cin >> n) {
  23. cout << "The number is: " << n;
  24. } else {
  25. // will fail if no digits or not in range [-2147483648, 2147483647]
  26. cout << "Invalid input: " << cinBuf;
  27. }
  28. cout << endl;
  29. }