1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Can someone look over my C++ code - Simple short code!

  1. Sep 28, 2011 #1
    1. The problem statement, all variables and given/known data

    My computer crashed, so I cant check my code I wrote, and the school linux lab is full, so I really need some help.

    I already wrote the code (ill post it below) so I cant compile it and check for some errors. Can anyone quickly glance it over and see any mistakes?

    The program will read a regular price from standard input and output a saleslip.

    Here is the code:

    //This program computes a regular price from standard input
    //and outputs a saleslip

    #include (isostream>
    using namespace std;


    int main()

    {

    const int HST_RATE = 12;
    double priceItem, total, per, sp, hst, dis;
    char sale;

    cout << " " << endl;
    cout << " This program calculates the total cost of some items " << endl;
    cout << " Enter the Regular Price " ;
    cin >> priceItem;
    Cout << " Is this item on Sale (Y/N)? "
    cin << sale;


    if ( sale == 'y' or sale == 'Y')
    {
    cout << "Enter the percent discount: "
    cin << per;
    dis = (per/100) * price;
    sp price - dis;
    hs = (HST_RATE/100) * sp;
    total = sp + hst;
    cout << "Sale Slip" << endl,
    cout << "===" << endl;
    cout "Regular Price $: " << price << endl;
    cout << "Discount $: " << dis << endl;
    cout << " Sale Price $: " << so << endl;
    cout << "HST $: " << hst << endl;
    cout << "Total $: " << total << endl;
    }


    else
    {

    hst = (HST_RATE/100) * price;
    total = price + hst;
    cout << "Sale Slip" << endl;
    cout << "===" << endl;
    cout << "Regular Price $ " << price << endl;
    cout << "HST $ << hst << endl;
    cout << "Total $ " << total << endl;
    }

    cout << " " << endl;

    return 0;

    }


    2. Relevant equations



    3. The attempt at a solution
     
  2. jcsd
  3. Sep 28, 2011 #2

    Mark44

    Staff: Mentor

    Use (code) and (/code) tags (inside brackets, not parentheses) to preserve your formatting.
    There are several things wrong that will keep this from compiling.
    1) You have Cout where it should be cout.
    2) hs is undeclared. The variable you have is hst.

    When you get those fixed, there is at least one other problem. (I have only scanned your code, so there might be more.)
    HST_RATE/100 == 0, because both are integer constants. Have you learned that there are two kinds of division in C (and in C++, C#, etc.)? Because HST_RATE/100 isn't what you thought, some tax is going to be computed incorrectly.

    Other comment
    Some of your variables have terrible names: HST_RATE, per, sp, hst, dis. What do these mean? You should give your variables meaningful names so that someone else looking at your code has some idea of what they're supposed to represent. Your instructor might deduct points from your code for this. I would.
     
  4. Sep 29, 2011 #3
    Some errors I've noticed:
    - To call the iostream library, you should use <iostream>
    - Many of the lines are missing a colon at the end and some have a comma instead
    - You've messed up some of the operators. Remember, it's cout << and cin >>
    - Some of the lines are meaningless, like "sp price - dis;" (Did you miss an equal sign?)

    In some Canadian provinces there is the Harmonized Sales Tax that combines federal and provincial sales taxes, and is commonly known as the HST. So I'd say HST_RATE and hst are fine, and the others should be changed.
     
  5. Sep 29, 2011 #4

    Mark44

    Staff: Mentor

    The OP has (isostream>, which makes two errors.
    Make that semicolon ;
    This is a colon :
     
  6. Sep 30, 2011 #5
    :rofl: I always get those terms mixed up
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Can someone look over my C++ code - Simple short code!
  1. C++ code, the calender (Replies: 2)

  2. Basic C++ Coding? (Replies: 3)

Loading...