Hi,

I'm new to this site, and new to programming in C. I'm doing the Sam's 21 day tutorial, and had a problem making a calculator. I made one that will add, subtract, multiply, and divide 2 numbers, but i would like to be able to add multiple numbers. The section I'm studying is about making menus, using break statements, and infinate loops. So, I'm not asking anyone to write me code for a calculator, but If anyone has, could I please see the code? It's no big deal, but I would like to see how it would really be written. I'll post my sad attempt just for fun!

Code:

#include <stdio.h>
#include <stdlib.h>
void delay(void);
int menu(void);
float sum(float, float);
float sub(float, float);
float product(float, float);
float quotient(float, float);
float x;
float y;
main()
{
while (1)
{
switch(menu())
{
case 1:
{
puts("Enter two numbers to add:");
sum(x, y);
break;
}
case 2:
{
puts("Enter two numbers to subtract:");
sub(x, y);
break;
}
case 3:
{
puts("Enter two numbers to multiply:");
product(x, y);
break;
}
case 4:
{
puts("Enter two numbers to divide:");
quotient(x, y);
break;
}
case 5:
{
puts("Exiting program now...");
exit(0);
}
default:
{
puts("\nInvalid choice, try again.");
}
}
}
}
float sub(float x, float y)
{
printf("\nEnter your first number:");
scanf("%f", &x);
printf("\nEnter your second number:");
scanf("%f", &y);
printf("\nThe result is: %.2f\n\n", x - y);
return x - y;
}
float product(float x, float y)
{
printf("\nEnter your first number:");
scanf("%f", &x);
printf("\nEnter your second number:");
scanf("%f", &y);
printf("\nThe result is: %.2f\n\n", x * y);
return (x * y);
}
float quotient( float x, float y)
{
printf("\nEnter your first number:");
scanf("%f", &x);
printf("\nEnter your second number:");
scanf("%f", &y);
printf("\nThe result is: %.2f\n\n", x/y);
return x/y;
}
float sum(float x, float y)
{
printf("\nEnter your first number:");
scanf("%f", &x);
printf("\nEnter your second number:");
scanf("%f", &y);
printf("\nThe result is: %.2f\n\n", x + y);
return x + y;
}
int menu(void)
{
int reply;
puts("Enter 1 for addition:");
puts("Enter 2 for subtraction:");
puts("Enter 3 for multiplication:");
puts("Enter 4 for division:");
puts("Enter 5 to exit:");
scanf("%d", &reply);
return reply;
}