C calculator help

cmpeters

n00b
Joined
Dec 2, 2006
Messages
17
This is a homework problem for school. I would like some help and suggestions, possible point me in the right direction because i'm lost.

The homework assignment is to write a simple "printing" calculator. The program should allow the user to type in expressions of the form [number] [operator] . The following operators should be recognized, +,-,*,/,S,E. The S operator sets the "accumulator" to the typed in value and E exits. The program should check for division by zero and if the entered operator is not one of the valid operators.

This is what i have so far.

#include <stdio.h>

int main(void)
{
int exec = 1;
float sum = 0, num;
char oper;

printf("\nBegin Calculations \n");

while(exec == 1){
scanf("&#37;f %c", &num, &oper);

switch(oper){
case ('+') : sum += num;
break;
case ('-') : sum -= num;
break;
case ('*') : sum = sum * num;
break;
case ('/') :
if(num==0){
printf("Cannot divide by zero");
break;
}
else{
sum = sum / num;
break;
}
case ('s') :
case ('S') : sum = num;
break;
case ('e') :
case ('E') :
printf("= %f \n", sum);
printf("End of Calculations");
exec = 0;
break;
default :
printf("Not a valid operator");
break;
}
printf("= %f \n", sum);
}
return 0;
}


Using gcc compiler on a Solaris workstation.

I get nothing when i run the program. This is my 4th revision of my code, I can't really get anything to work.

Thanks
 
How do i keep formatting in my post? The code I entered was a lot easier to read that what it looks like in my post.

I read the rules about posting homework help, i'm not looking for an answer, just a point in the right direction.
 
Ok, so I played around with my program and removed the if/else statement from my switch and now the program partially works. It will not detect and prevent division by zero.

Are if/else statements allowed within a case of a switch?
 
if/else statements are allowed in a case, yes.

You can use the
Code:
 tag to keep your formatting in your posts here.
 
Code:
#include <stdio.h>
 
int main(void)
{
  int exec = 1;

  float sum = 0, num;
  char oper;
 
  printf("\nBegin Calculations \n");
 
  while(exec == 1){

  scanf("&#37;f %c", &amp;num, &amp;oper);
 
  switch(oper){
    case ('+') : sum += num;
                  break;

    case ('-') : sum -= num;
                  break;
    case ('*') : sum = sum * num;
                 break;
    case ('/') :
      if(num==0){

        printf("Cannot divide by zero");
        break;
      }
     else{
       sum = sum / num;

       break;
     }
    case ('s') :
    case ('S') : sum = num;
                 break;
    case ('e') :

    case ('E') : 
      printf("= %f \n", sum);
      printf("End of Calculations");
      exec = 0;
      break;

    default :
      printf("Not a valid operator");
      break;
   }
  printf("= %f \n", sum);

  }
  return 0;
}

Your code in a more readable format.

regarding the if/else: you can put the break after that block, you don't need to include it inside each if statement.
 
Oh wow, thanks. It makes sense to put the break; after the if/else statement.

Another thing I noticed; when exiting the program I get the output "= number" twice.

Begin Calculations
4 S
= 4.000000
0 E
= 4.000000
End of Calculations
= 4.000000

When the user enters "E" I set the variable exec = 0, so why does the loop iterate again?
 
It does that because you told it to. You print "End of Calculations", then exit the case statement. The last statement in your loop body is to print the number. After that, the exec flag is checked, found false, and the loop exits.
 
Another question. Does it make more sense to create a separate function to do the match calculations? Then call the funcation in a loop?

Code:
/]

#include <stdio.h>

/* function prototypes */
float calcSum(float num, char oper, float sum);

int main(void)
{
  float sum=0, num;  
  char oper;

  printf("\nBegin Calculations\n");
  scanf("%f %c", &num, &oper); 

  sum = calcSum(num, oper, sum);
  
  printf("= %f \n", sum);

  return 0;
}

float calcSum(float num, char oper, float sum)
{
  switch(oper){
    case ('+') : sum += num;
               break;
    case ('-') : sum -= num;
               break;
    case ('*') : sum = sum * num;
               break;
    case ('/') : sum = sum / num;
               break;
    case ('s') : sum = num;   
               break;
    default  :
               break;
  }
  return sum;
}

This was another version that I wrote, minus a loop. I gave up when I couldn't figure out an easy way to check the input values. To ensure that they were (+,-,/,*,S,E)
 
It does that because you told it to. You print "End of Calculations", then exit the case statement. The last statement in your loop body is to print the number. After that, the exec flag is checked, found false, and the loop exits.

Oh, ok. Is there a way to change that, or is it a fault in the way I'm approaching the problem? I guess I could do a printf after each case instead of outside of the switch, but is that considered proper?
 
I figured it out. I moved the printf("End of Calculations"); outside of the loop.

This might seem annoying, posting questions and then answering my own questions, but talking about this has really helped me see what's wrong with my code.
 
This is what I have for my semi-final code. Any comments or criticisms?

Code:
#include <stdio.h>

int main(void)
{
  int exec = 1;
  float sum = 0, num;
  char oper;

  printf("\nBegin Calculations \n");
  
  while(exec == 1){
  scanf("%f %c", &num, &oper);
  
  switch(oper){
    case ('+') : sum += num;
      break;
    case ('-') : sum -= num;
      break;
    case ('*') : sum = sum * num;
      break;
    case ('/') :
      if(num==0){
        printf("Cannot divide by zero \n");
      }
      else{
        sum = sum / num;
      }
      break;
    case ('s') :
    case ('S') : sum = num;
      break;
    case ('e') :
    case ('E') : 
      exec = 0;
      break;
    default :
      printf("Not a valid operator \n");
      break;
  }
  printf("= %f \n", sum);
   
  }

  printf("End of Calculations \n");
  return 0;
}
 
Back
Top