Thread: if else problems in C programming

  1. #1
    Registered User
    Join Date
    Feb 2016
    Posts
    44

    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

    if else problems in C programming-hw3question-png

    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. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote 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.

    Quote 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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2016
    Posts
    44
    Quote Originally Posted by laserlight View Post
    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. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote 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.
    Last edited by laserlight; 02-08-2016 at 11:52 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with C programming problems
    By overlord21 in forum C Programming
    Replies: 14
    Last Post: 05-13-2008, 09:59 PM
  2. problems with TCP/IP programming
    By jalnewbie in forum Networking/Device Communication
    Replies: 5
    Last Post: 06-29-2007, 10:53 PM
  3. Two problems on programming
    By rosicky2005 in forum C++ Programming
    Replies: 9
    Last Post: 11-07-2006, 10:08 AM
  4. Programming tasks/problems
    By edd1986 in forum C Programming
    Replies: 2
    Last Post: 03-20-2005, 09:45 AM
  5. TCP Programming problems with recv
    By stovellpaul in forum C++ Programming
    Replies: 1
    Last Post: 09-08-2002, 04:47 PM