Basically the user enters one of the following + - * / % | < > c q
followed by one or more spaces, followed by an integer, and then the newline key to get the answer and the current vaule is set to 0 then it keeps on adding up until the user enter c for clear or q for quit.
however, when I run the program, it goes straight to error message instead of compiling, what am I doing wrong? Thanks.
#include <stdio.h>
#include<stdlib.h>
#include <string.h>
#include <ctype.h>
int main ()
{
int b; //result
int i;
int n;
int a; //number to be calculated
char f; //operator
char s[33]; // number follows c
char *c;
printf("ECE15 Lab2 \n");
printf("Name: Sherry Li\n");
printf("You may enter one of the allowed operations, + - * / % & | < > c q\n");
printf("Press c for clear.\n");
printf("Press q for quit. \n\n");
while (gets(s)!=0)
{
//strncpy(s,f,1);
c = strpbrk(s, "+-*/%&|<>cq");
if (c != 0)
*c = ' ' ; //zero out the operation
a = atoi(s);
switch (a)
{
case '+':
b=b+a;
break;
case '-':
b=b-a;
break;
case '*':
b=b*a;
break;
case '/':
b=b/a;
break;
case '%':
b=b%a;
break;
case '&':
b=b&a;
break;
case '|':
b=b|a;
break;
case '<':
b=b<a;
break;
case '>':
b=b>a;
break;
case 'c':
a = 0;
break;
case 'q':
return 0;
break;
case '\n':
printf("The dec answer= %d\n", b);
printf("The hex answer= %x\n", b);
printf("The oct answer= %o \n\n", b);
break;
default:
printf("error: unknown command %s\n", s);
}
}
}