Thread: if else problems in C programming

1. if else problems in C programming

So Im trying to write a code that represents this
Weight - less than 10 = \$2.00 per 500 miles
Weight - more than 10 = \$4.50 per 500 miles
Weight - more than 50 = Does not ship

This is what I have so far, but I cant get it just right to answer the question for all types of data input.

I know Im missing some sort of concept to be able to write this code right and I was wondering if anyone had any links/help to learn how to do this!

2. Originally Posted by Rednally
This is what I have so far, but I cant get it just right to answer the question for all types of data input.
Be careful with your wording: "all types of data input" gives the impression that you are not only testing with valid input, but invalid input too (e.g., negative numeric input even though it does not make sense for shipping weight, alphabetic input even though the weight is numeric), but I do not see any code to handle invalid input, e.g., you do not check the result of scanf.

Originally Posted by Rednally
I know Im missing some sort of concept to be able to write this code right and I was wondering if anyone had any links/help to learn how to do this!
Based on the code that you have written, it looks like your misunderstanding lies at the level of understanding the problem, rather than with formulating an algorithm.

Take another look at what you wrote concerning the problem:
Weight - less than 10 = \$2.00 per 500 miles
Weight - more than 10 = \$4.50 per 500 miles
Weight - more than 50 = Does not ship

What this means is that you are dealing with four values:
• Weight: numeric input from the user
• Miles: numeric input from the user
• Cost per 500 miles: based on where the weight input falls into the weight ranges
• Total cost: (Miles / 500.0) * Cost per 500 miles, presumably rounded to two decimal places

So, the idea is to read the weight and miles from the user, then use if/else statements to determine the cost per 500 miles. You have to account for a weight more than 50, in which case perhaps you could set the cost per 500 miles to be negative, and use this to check if you should compute the total cost, or inform the user that the item does not ship.

3. Originally Posted by laserlight
Be careful with your wording: "all types of data input" gives the impression that you are not only testing with valid input, but invalid input too (e.g., negative numeric input even though it does not make sense for shipping weight, alphabetic input even though the weight is numeric), but I do not see any code to handle invalid input, e.g., you do not check the result of scanf.

Based on the code that you have written, it looks like your misunderstanding lies at the level of understanding the problem, rather than with formulating an algorithm.

Take another look at what you wrote concerning the problem:
Weight - less than 10 = \$2.00 per 500 miles
Weight - more than 10 = \$4.50 per 500 miles
Weight - more than 50 = Does not ship

What this means is that you are dealing with four values:
• Weight: numeric input from the user
• Miles: numeric input from the user
• Cost per 500 miles: based on where the weight input falls into the weight ranges
• Total cost: (Miles / 500.0) * Cost per 500 miles, presumably rounded to two decimal places

So, the idea is to read the weight and miles from the user, then use if/else statements to determine the cost per 500 miles. You have to account for a weight more than 50, in which case perhaps you could set the cost per 500 miles to be negative, and use this to check if you should compute the total cost, or inform the user that the item does not ship.
Thank you so much @laserlight

However, your info hasnt helped me with this problem haha! I guess if I had to break it down to simplest terms, I need to know this.
How can I used if/else statements to represent two different types of data that compound? (500 miles = \$2, 1000 miles = \$4, for a 10 lb package) BUT also (500 miles = \$4.5, 1000 miles = \$9.0, for a 10+lb package) Just some pseudocode or anything to point me in the right direction would be greatly appreciated!

4. Originally Posted by Rednally
How can I used if/else statements to represent two different types of data that compound? (500 miles = \$2, 1000 miles = \$4, for a 10 lb package) BUT also (500 miles = \$4.5, 1000 miles = \$9.0, for a 10+lb package) Just some pseudocode or anything to point me in the right direction would be greatly appreciated!
I would expect something like this:
Code:
```if the weight is less than 10 lb
cost per 500 miles = 2
else if the weight is less than 50 lb
cost per 500 miles = 4
else
cost per 500 miles = -1

if the cost per 500 miles is negative
inform the user that the item does not ship
else
compute total cost and inform the user```
This is basically what I wrote in my previous post though.

EDIT:
You need to be clear what are the boundaries though. Some people (including an old prof of mine) have a bad habit of using "less than" when they mean "less than or equal to", or using "more than" when they mean "more than or equal to". Look again:
Weight - less than 10 = \$2.00 per 500 miles
Weight - more than 10 = \$4.50 per 500 miles

Strictly speaking, the above requirements do not define what happens if the weight is exactly 10 lb.