cin_cout.ino 845 B

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