a very simple calculator, im just typing it in here so you might have to correct some small compile errors, but the logic should be sound.
That would make a good debugging exercise for the trainees
I tried leaving pointers out of it .. but hell thats what c runs on you have to learn them eventually.
You will have to break out though its infinate
Code:
#include <stdio.h>
int main()
{
float oprand;
float accumulator;
char buf=' ';
while (1)
{
printf("Enter a the first number\n");
scanf("%f", &accumulator);
buf = ' ';
while (buf != '=')
{
printf("Enter +,-,*, / Or enter = to get result\n");
scanf("%s", &buf);
if (buf != '=')
{
printf("Enter next number.\n");
scanf("%f", &oprand);
}
switch (buf)
{
case '+':
accumulator+=oprand;
break;
case '-':
accumulator-=oprand;
break;
case '*':
accumulator*=oprand;
break;
case '/':
accumulator/=oprand;
break;
case '=':
printf("Result: %f\n", accumulator);
break;
default:
printf("invalid operator\n"); // have to have it to prevent the program from crashing, you may wish to put debugging info in here
}
}
}
} // err lost track of braces this might not be needed
well its started out simple .. hope it helps actually it turned out to be c instead of c++ .. eeh it will work in c++ .. replace the printfs and scanfs with cin and cout stream redirections like your use to ..then it be c++
