Skip to content

Commit

Permalink
Add example that read from stdin
Browse files Browse the repository at this point in the history
  • Loading branch information
cooper-ece160 committed Oct 25, 2017
1 parent 567696b commit 79006b4
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions stdin_ex.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[]) {
// create a buffer that can hold
// up to 255 characters
char buf[];
buf = "Hello";
char buffer[256];


// read one line at a time and send it to calc function for processing
while(1) {
char *s;
char op;
float x, y;
fscanf(stdin, "%c %f %f", &op, &x, &y);
switch (op) {
case '+' : printf("%.2f\n", x + y); break;
case '-' : printf("%.2f\n", x - y); break;
case 'x' : printf("%.2f\n", x * y); break;
case '/' : printf("%.2f\n", x / y); break;
}
}

/*
while ((s = fgets(buffer, 255, stdin)) != NULL) {
float ans = calc(buffer);
printf("%.2f\n", ans);
}
*/
return 0;
}

0 comments on commit 79006b4

Please sign in to comment.