1. ## Calculator program.

Code:
```#include <stdio.h>
#include <stdlib.h> //for exit function.
void multiply();
void divide();
void substract();
/* The floats should be changed maybe to be rounded off. */
int main()
{
int choise;
printf("Calculator.\n");
do                      //Should be changed possibly.
{
puts("What would you like to do: \n");
puts("1 to multiply.");
puts("2 to divide.");
puts("4 to substract.");
puts("5 to exit.");
scanf(" %d", &choise);
switch (choise)
{
case(1) :
multiply();
break; //prevents other cases from executing
case(2) :
divide();
break;
case(3) :
break;
case(4) :
substract();
break;
case(5) : exit(1); //stlib.h included
break; //Not neccesary just looks neat.
}
}
while((choise < 1) ||(choise > 5));

getchar();
getchar();
return 0;
}

void multiply()
{
float a, b, c;
puts("Enter numbers: ");
scanf(" %f", &a);
scanf(" %f", &b);
c = a * b;
return;
}

void divide()
{
float a, b, c;
puts("Enter numbers: ");
scanf(" %f", &a);
scanf(" %f", &b);
c = a / b;
return;
}

{
float a, b, c;
puts("Enter numbers: ");
scanf(" %f", &a);
scanf(" %f",  &b);
c = a + b;
return;
}

void substract()
{
float a, b, c;
puts("Enter numbers: ");
scanf(" %f", &a);
scanf(" %f", &b);
c = a - b;
}```
This is the code I have, and I'm quite pleased with it. Somebody suggested that the loop should be changed. I could of course make it run infinately until the user decides to exit, but is something else wrong with it?

2. Looks good to me, i would suggest that you notice the fact of repeating code within your functions.
It generally is a good practice, to define a new function the moment you find the need to repeat code.

You could use that fact to write a function, eg: readAB(float *a,float *b); which would read two floats, and call that function instead of repeating code.

That way if you ever feel something needs to change about user input, for example some error checking, you have one place to edit and not 4.

3. I should be passing by adress and not by value to do this?

4. I think it looks fine to, you could make it more optimal so a user can do multiple calculations without having to exit. This could maybe be accomplished by use of a boolean variable to replace the exit statement and add an or condition in the do-while that exits the loop when the boolean variable is false. Other than that, the previous suggestion about doing away with repeated code was good to.

5. Thanks for the suggestions.
A little proof of concept:
Code:
```#include <stdio.h>
void function();

int main()
{
int i;
puts("Enter an integer: ");
scanf("%d", &i);
function(&i);
printf("%d", i);
getchar();
getchar();
return(0);
}

void function(int * i)
{
*i = 1 + *i;
return;
}```
I think I should be able to apply this on the calculator program.

6. Looks good, but there is some redundant code you code get rid of. One improvement would be to allow the user to enter an expression in one go, then work it out in one go. That would be a lot more user friendly. The easiest way to do this would be to get the bits as command line parameters instead of breking the string down yourself. Heres an example I made:
Code:
```#include <stdio.h>
#include <stdlib.h>

double Calc(double left, char op, double right)
{
switch(op)
{
case '+': return left + right;
case '-': return left - right;
case 'x': return left * right;
case '/': return left / right;
default:
printf("Invalid operator.\nValid ops: + - x /\n");
exit(EXIT_FAILURE);
}
}

int main(int argc, char* argv[])
{
if(argc < 4)
{
printf("Error: Not enough parameters.\n");
exit(EXIT_FAILURE);
}
if(argc % 2)
{
printf("Error: Invalid parameters.\n");
exit(EXIT_FAILURE);
}
double left=strtod(argv[1], NULL), right;
int i;
for(i=2; i<argc; i+=2)
{
right = strtod(argv[i+1], NULL);
left = Calc(left, argv[i][0], right);
}
printf("%f\n", left);
exit(EXIT_SUCCESS);
}```
Its nothing amazing, but it might give you some ideas