ELEC350 - Team Q / Mbed OS z_compare_commands_NEW_METHOD

Dependencies:   mbed-os

Fork of z_compare_commands by ELEC350 - Team Q

Committer:
osmith2
Date:
Fri Jan 05 16:53:06 2018 +0000
Revision:
1:81f52f1beb21
Parent:
0:f46e16e62b0c
Child:
2:690dd6bdb9a7
updated with code to try and read a number as well as a command

Who changed what in which revision?

UserRevisionLine numberNew contents of line
osmith2 0:f46e16e62b0c 1 //http://www.cplusplus.com/reference/cstring/strcmp/
osmith2 0:f46e16e62b0c 2
osmith2 0:f46e16e62b0c 3 #include "mbed.h"
osmith2 0:f46e16e62b0c 4 #include <string>
osmith2 0:f46e16e62b0c 5
osmith2 0:f46e16e62b0c 6 Serial pc(USBTX, USBRX);
osmith2 0:f46e16e62b0c 7
osmith2 0:f46e16e62b0c 8
osmith2 0:f46e16e62b0c 9 DigitalOut myled(LED1);
osmith2 0:f46e16e62b0c 10
osmith2 0:f46e16e62b0c 11 //THIS CODE DOESN'T WORK YET.
osmith2 0:f46e16e62b0c 12 //This may help http://www.cplusplus.com/reference/string/string/compare/
osmith2 0:f46e16e62b0c 13
osmith2 0:f46e16e62b0c 14
osmith2 0:f46e16e62b0c 15 int main() {
osmith2 0:f46e16e62b0c 16 char key[] = "apple";
osmith2 0:f46e16e62b0c 17 char key2[] = "pear";
osmith2 0:f46e16e62b0c 18 char puttyBuffer[20];
osmith2 1:81f52f1beb21 19 char numBuffer[20];
osmith2 0:f46e16e62b0c 20
osmith2 0:f46e16e62b0c 21 while(1) {
osmith2 0:f46e16e62b0c 22
osmith2 0:f46e16e62b0c 23 myled = 1; // LED is ON
osmith2 0:f46e16e62b0c 24 wait(1.0); // 200 ms
osmith2 0:f46e16e62b0c 25 myled = 0; // LED is OFF
osmith2 0:f46e16e62b0c 26 wait(1.0); // 1 sec
osmith2 0:f46e16e62b0c 27
osmith2 0:f46e16e62b0c 28 //pc.printf("What is the password?\n");
osmith2 0:f46e16e62b0c 29 pc.printf ("Please choose a fruit?\n");
osmith2 0:f46e16e62b0c 30 fflush (stdout);
osmith2 1:81f52f1beb21 31 scanf ("%79s",puttyBuffer);
osmith2 1:81f52f1beb21 32 //scanf ("%79s",numBuffer);
osmith2 0:f46e16e62b0c 33
osmith2 0:f46e16e62b0c 34 if (strcmp (key,puttyBuffer) == 0){
osmith2 0:f46e16e62b0c 35 puts ("Apple is delicious!\n");
osmith2 1:81f52f1beb21 36 /*
osmith2 1:81f52f1beb21 37 pc.printf ("The command entered is %u characters long.\n",(unsigned)strlen(key));
osmith2 1:81f52f1beb21 38 int i =0;
osmith2 1:81f52f1beb21 39 int keysize = (unsigned)strlen(key);
osmith2 1:81f52f1beb21 40 int endfor = keysize;
osmith2 1:81f52f1beb21 41
osmith2 1:81f52f1beb21 42 //check for number here
osmith2 1:81f52f1beb21 43
osmith2 1:81f52f1beb21 44 for(i=0; i<endfor; i++)
osmith2 1:81f52f1beb21 45 {
osmith2 1:81f52f1beb21 46 pc.printf("%c", puttyBuffer[i]);
osmith2 1:81f52f1beb21 47 }
osmith2 1:81f52f1beb21 48 */
osmith2 0:f46e16e62b0c 49 }
osmith2 0:f46e16e62b0c 50 else if (strcmp (key2,puttyBuffer) == 0){
osmith2 0:f46e16e62b0c 51 puts ("Eww, pear is icky.\n");
osmith2 0:f46e16e62b0c 52 }
osmith2 0:f46e16e62b0c 53 else{
osmith2 0:f46e16e62b0c 54 puts ("try again loser!\n");
osmith2 0:f46e16e62b0c 55 }
osmith2 0:f46e16e62b0c 56 }
osmith2 0:f46e16e62b0c 57
osmith2 0:f46e16e62b0c 58 }