string - Make a C program that performs only three valid commands -
the commands case sensitive. following commands:
- add x,y; x , y literal integer values , add capital letters. add command not display result merely performs addition on whatever values there x , y. result of operation saved on special variable called ax.
- prt ax - display value of ax.
- exit – exits program.
your program should continue asking valid commands user until user typed exit.
example of valid inputs user:
- add 3,5
- add 4, 3
- prt ax
- exit
example of invalid inputs user:
- add 3,4
- add x,4
- add x,y
- display ax
- prt ax
- prt y
- prt 4
- exit
i tried making unfortunately made add command. can't seem prt ax command. me please :( here code
#include <stdio.h> #include <stdlib.h> #include <string.h> char str[20]; char cmd[10], x[10], y[10], zip[32], set[32]; int ax, i, j; int main (void) { while (str != 'exit') { scanf ("%[^\n]%*c", str); if (str != 'prt') { calc(); } } return 0; } int add(int x, int y){ return x + y; } int calc() { strcpy(cmd, strtok(str , " ")); strcpy(x, strtok(null, ",")); strcpy(y , strtok(null, ",")); = atoi(x); j = atoi(y); if ((strcmp (cmd, "add") == 0) && (strcmp (cmd, "add") < 0)) { ax = add(i,j); } return ax; } // dont know did or did t.t void print() { strcpy(cmd, strtok(str , " ")); strcpy(zip, strtok(null, " ")); if (strcmp (cmd, "prt") == 0 && strcmp (zip, "ax") == 0) { printf("%d\n", ax); } }
let em guess - seeing no output?
strcpy(zip, strtok(null, " ")); if (strcmp (cmd, "prt") == 0 && strcmp (zip, "ax") == 0) { printf("%d\n", ax); }
what trying achieve zip
? assign empty string , print ax if equal zip, never be.
note, should have bracketed better (read on operator precedence), so
strcpy(zip, strtok(null, " ")); if ((strcmp (cmd, "prt") == 0) && (strcmp (zip, "ax") == 0)) { printf("%d\n", ax); }
but, really, need forget zip ,
void print() { printf("%d\n", ax); }
Comments
Post a Comment