Thread: Newbie help for a class

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    4

    Newbie help for a class

    Hey all,

    I've just started a C programming class and need some help with an assignment. Here is the problem...

    If your taxable income is between:

    0 and 8500 your tax bracket is 10%
    8500 and 34500 your tax bracket is 15%
    34500 and 83600 your tax bracket is 25%

    Let the user input, yearly gross income and you write a program to compute the federal tax amount.


    All I need to know is how to write an If statement to include a range of numbers, so I don't have to write 83600 if statements. For some reason I can't seem to find this in the book.

    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by unclepickle1
    All I need to know is how to write an If statement to include a range of numbers, so I don't have to write 83600 if statements.
    Make use of the < operator (or <=, etc) in the condition of your if statements.
    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 camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    ex. if(taxVariable<8500)

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by camel-man View Post
    ex. if(taxVariable<8500)
    Code:
    if (income < 8500.00)
     { tax = .10; }
    else if (income < 34500.00)
     { tax = .15; }
    else if(income < 83600.00)
     { tax = .25; }
    else
      { printf("You are one lucky S.O.B.... not only do you get a ton of money, you just got off tax free!");
         tax = 0.0 }

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    CommonTater's example is what I had in mind, though I might write it as:
    Code:
    if (income < 8500.00)
    {
        tax = 0.10;
    }
    else if (income < 34500.00)
    {
        tax = 0.15;
    }
    else if (income < 83600.00)
    {
        tax = 0.25;
    }
    else
    {
        printf("You are one lucky S.O.B.... not only do you get a ton of money, you just got off tax free!");
        tax = 0.0;
    }
    Last edited by laserlight; 09-28-2011 at 09:02 PM.
    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

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Moving the little curly things around doesn't make it any funnier, Lase....

    And it's still missing the semicolon after tax = 0.0 ....

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by CommonTater
    Moving the little curly things around doesn't make it any funnier, Lase....
    Not meant to be funny. unclepickle1, note that CommonTater's indent style is unusual. I do not recommend that you follow it.

    Quote Originally Posted by CommonTater
    And it's still missing the semicolon after tax = 0.0 ....
    That is true. Your mistake.
    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

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by laserlight View Post
    Not meant to be funny. unclepickle1, note that CommonTater's indent style is unusual. I do not recommend that you follow it.
    That is true. Your mistake.
    Indeed... twice.

    However... your "fix" would have been far more credible if you'd actually fixed the code

    EDIT: Tell you what... get 150,000 statements in a file your way and the same code formatted my way... wanna guess who wears out a mouse wheel quicker? I use that style because it's very compact and very easy for me to read. No, I don't recommend it to others... but, lets settle this... "Unusual" is not "wrong".
    Last edited by CommonTater; 09-28-2011 at 09:15 PM.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by CommonTater
    However... your "fix" would have been far more credible if you'd actually fixed the code
    I did not even intend to post any code until unclepickle1 made an attempt, but since you decided to do unclepickle1's homework with an unconventional style, I decided to post your code with a more common style.

    Quote Originally Posted by CommonTater
    get 150,000 statements in a file your way and the same code formatted my way... wanna guess who wears out a mouse wheel quicker?
    The same, since all those 150,000 statements are assignment statements... wait, why do you have 150,000 statements in a file to begin with?

    Even if you really did find the rate of "wearing out a mouse wheel" to be important, another common indent style does not fare that much worse than yours here:
    Code:
    if (income < 8500.00) {
        tax = .10;
    } else if (income < 34500.00) {
        tax = .15;
    } else if(income < 83600.00) {
        tax = .25;
    } else {
        printf("You are one lucky S.O.B.... not only do you get a ton of money, you just got off tax free!");
        tax = 0.0;
    }
    Quote Originally Posted by CommonTater
    "Unusual" is not "wrong"
    Of course.
    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

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by laserlight View Post
    I did not even intend to post any code until unclepickle1 made an attempt, but since you decided to do unclepickle1's homework with an unconventional style, I decided to post your code with a more common style.
    Yeah, no problem, just playing with you a little...

    The same, since all those 150,000 statements are assignment statements... wait, why do you have 150,000 statements in a file to begin with?
    Trust me... you don't want to know... (Hint: The number could be classified as "exaggeration to make a point")


    Even if you really did find the rate of "wearing out a mouse wheel" to be important, another common indent style does not fare that much worse than yours here:
    Code:
    if (income < 8500.00) {
        tax = .10;
    } else if (income < 34500.00) {
        tax = .15;
    } else if(income < 83600.00) {
        tax = .25;
    } else {
        printf("You are one lucky S.O.B.... not only do you get a ton of money, you just got off tax free!");
        tax = 0.0;
    }
    See... I look at that and all the braces just vanish on me... As I've explained before I'm so used to my offbeat style that I find the conventional formatting is actually hard to read... "Old habits" and all that. (Take a look at some old time Turbo Basic programs, you'll see what I mean)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie class question
    By vastgoten in forum C++ Programming
    Replies: 4
    Last Post: 07-31-2008, 01:43 AM
  2. Newbie class question
    By Beowolf in forum C++ Programming
    Replies: 1
    Last Post: 09-22-2007, 09:03 PM
  3. NEWBIE: Class Problem
    By jimboob in forum C++ Programming
    Replies: 4
    Last Post: 06-25-2004, 09:13 AM
  4. Newbie Question: My class
    By jimboob in forum C++ Programming
    Replies: 5
    Last Post: 06-08-2004, 09:58 AM
  5. newbie class templates question
    By daysleeper in forum C++ Programming
    Replies: 2
    Last Post: 09-18-2001, 09:50 AM