I am a newbie in a programming, and I just randomly chose a task for training from some group on Facebook. The task is to calculate the cost of cement for a construction project. The input is the number of pounds of cement required (guaranteed not to be a multiple of 120). The store sells cement in 120-pound bags, each costing $45.
Example input: 295.8
Output: 135
#include <stdio.h>
#include <stdlib.h>
#define PRICE 45
#define CAPACITY 120
#define MAXDIGITS 5
int sum(int);
int main(int argc, char *argv[])
{
int val = 0;
char inp[MAXDIGITS];
if ((argc > 1) && (argv[1] > 0))
val = strtol(argv[1], NULL, 0);
else
{
do
{
printf("Please input the value of cement: ");
scanf("%s", inp);
val = strtol(inp, NULL, 0);
}
while (!val);
}
if (val)
printf("Money you need: %d\n", sum(val));
return 0;
}
int sum(int need)
{
int mon = PRICE;
int n = CAPACITY;
while (n < need)
{
n += CAPACITY;
mon += PRICE;
}
return mon;
}
I'm interested in code style, rational memory usage, etc.