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);
	}
}
}

Don't use gets , because it never checks the buffer size and is consider to be unsafe -- fgets is what you're supposed to use.

Output values that are key to the operation of the code (like a, b, and c) at important decision points to see what values they contain.

it goes straight to error message instead of compiling

what compiler are you using, because on VS2005 pro, your program compiles fine..

ok so I changed gets to fgets, however, it's still not working?? What I mean is when I try to enter calculation such as + 3 it goes straight to default in switch function -- "error: unknown command", did I do something wrong there?

#include <stdio.h>
#include<stdlib.h>
#include <string.h>
#include <ctype.h>


int main ()
{
	int b; //result
	int d; //binary to integer 
	int i; //integer to be converted
	int n;
	int a; //number to be calculated
	//char f; 
	char s[34]; //entered value
	char *c;
	char *p;


	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"); 

	printf("Decimal \t Octal \t Hex\t Binary\t Op Integer: ");     // while(gets(s)!=0)
	fgets(s, sizeof(s), stdin);
    	// check for and remove trailing \n
    	if ((p = strchr(s,'\n')) != NULL)
   	 {
    	  *p = '\0';
   	 }
		c = strpbrk(s, "+-*/%&|<>cq"); //strncpy(s,f,1);
		if (c != 0)
		*c = ' ' ; //zero out the operation 
		a = atoi(s);
		//d = b2i(c); //binary to integer 
          	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 '\n':
			printf("The dec answer= %d \t The hex answer= %x\t The oct answer= %o \n\n", b, b, b);                
			break;
		case 'c':
			b = 0;
			break;
		case 'q':
			break;
    default:
      printf("error: unknown command %s\n", s);
			break;
	}
}

I repeat:

Output values that are key to the operation of the code (like a, b, and c) at important decision points to see what values they contain.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.