HiveBrain v1.2.0
Get Started
← Back to all entries
patterncMajor

Feedback on a programming practice problem in C

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
practiceproblemfeedbackprogramming

Problem

This is a programming practice that our teacher gave us, and I would appreciate if someone can look over my program and tell me if I did a good job.

Basically, the context is I joined a company and am supposed to write a program in C that takes three inputs (ft of steel, number of ball bearings, and lbs of bronze), and returns how many bells and whistles can be made the next day.

Making a whistle requires 0.5 ft of steel and 1 ball bearing. Making a bell requires 1.10 lbs of bronze (.75 lbs of bronze and 1 clapper (.35 lbs of bronze)).

This is the program I have written. I apologize if it seems messy.

```
#include
#include

void PrintLine();
int CalculateWhistles(int steel, int bearings);
int CalculateBells(int bronze);

int main() {
bool runProgram = true;
int runAgain = 0;

while (runProgram == true) {
int ftSteelIn = 0;
int numBallBearingIn = 0;
int ibsBronzeIn = 0;

printf("\n\n"); // Formatting...
PrintLine();

printf(" How many feet of steel was received today: "); // Steel received
scanf("%d", &ftSteelIn);
printf(" How many ball bearings were received today: "); // Ball bearings received
scanf("%d", &numBallBearingIn);
printf(" How many pounds of bronze was received today: "); // Bronze received
scanf("%d", &ibsBronzeIn);

PrintLine(); // Print line

PrintLine(); // Print line

// Print company logo and program title
printf(" Acme Corporation\n");
printf(" Product Report\n");

PrintLine();// Print line

// Print number of bells and whistles
printf(" Number of Bells %d\n", CalculateBells(ibsBronzeIn));
printf(" Number of Whistles %d\n", CalculateWhistles(ftSteelIn, numB

Solution

Roughly 50% of all programming is about dealing with errors. You say your code works, but it doesn't handle errors and therefore doesn't work.

Example 1:

How many feet of steel was received today:  four


Program doesn't say "Error, try again". Instead it displays all prompts without waiting for any user input and exits.

Example 2:

How many feet of steel was received today:  3.7


Program doesn't say "Error, try again". Instead it displays all prompts without waiting for any user input and exits.

Example 3:

How many feet of steel was received today:  3 4


Program doesn't say "Error, try again". Instead it assumes 3 feet of steel; displays "How many ball bearings" and doesn't wait for user input and assumes 4 ball bearings.

Example 4:

Would you like to run the program again? ('1' for yes, '2' for no): yes


Program doesn't say "Error, try again". Instead it exits.

Example 5:

How many feet of steel was received today:               -99
How many ball bearings were received today:              -99
How many pounds of bronze was received today:            -99


Program decides you can make -89 bells and -198 whistles.

Example 6:

How many feet of steel was received today:               999999999999999999
How many ball bearings were received today:              999999999999999999
How many pounds of bronze was received today:            999999999999999999


Program decides you can make -1351471477 bells and -2147483648 whistles.

Example 7:

How many feet of steel was received today:               200
How many ball bearings were received today:              100


Program decides you can make 0 whistles. Note: unlike the previous examples, this is not a "failed to check for invalid input" problem.

Code Snippets

How many feet of steel was received today:  four
How many feet of steel was received today:  3.7
How many feet of steel was received today:  3 4
Would you like to run the program again? ('1' for yes, '2' for no): yes
How many feet of steel was received today:               -99
How many ball bearings were received today:              -99
How many pounds of bronze was received today:            -99

Context

StackExchange Code Review Q#41845, answer score: 30

Revisions (0)

No revisions yet.