Mrk's Profile User Rating: -----

Reputation: 29 Craftsman
Active Members
Active Posts:
66 (0.03 per day)
03-December 09
Profile Views:
Last Active:
User is offline Jul 06 2015 12:02 AM

Previous Fields

Dream Kudos:

Latest Visitors

Icon   Mrk has not set their status

Posts I've Made

  1. In Topic: What is the best way to begin in C++?

    Posted 2 Nov 2013

    I would recommend taking a basic computer science course somewhere along the way. Having an understanding of bits and bytes, binary and hex, registers and logic gates and assembly code, etc, will help you understand programming better.
  2. In Topic: intro to programming

    Posted 7 Oct 2013

    I assume you know how to print out the answer in lower case right. For example, user inputs m so the programs prints out l and n. Then all you have to do is convert to uppercase. There are at least three ways to do that and the most basic way is to just subtract 32 from the lower case to get the upper case. For example, l has an ASCII value of 108, so subtract 32 to get uppercase L, which has an ASCII value of 76.

    Look at an ASCII chart and see what the relationship is between the values for the lowercase set of letters and the uppercase set of letters. You will see that the lowercase letters are always 32 higher than the uppercase letters.
  3. In Topic: Stuck on basic code

    Posted 27 Sep 2013

    Every variable must have a type:

    int x = 0;
    float y = 0;
    double z = 0;

    Likewise, any literal value also has a type:

    15 means an integer with a value of 15.
    15.0 means a double with a value of 15.
    15.0f means a float with a value of 15.

    Any number that has a decimal point in it is automatically a double value, unless it is followed by an "f" in which case it is a float, and any number without a decimal point is an integer. Notice line 9 in your code:

    tax = (6.75/100)*meal;

    You have a double literal, 6.75 which results in a double result ((6.75/100) *meal) being assigned to "tax" which is a float. The compiler has to convert the double result into a float to fit it inside "tax", so you get this warning: "conversion from 'double' to 'float', possible loss of data". In this case there happens to be no loss of data so no problem. But it's good to eliminate all warnings anyway, so putting an "f" after 6.75 (6.75f) causes the result to be a float value which is assigned to the float "tax", and so no warning.

    Now to address your problem. In line 11 you have:

    tip = (15/100)*total;

    So you are dividing the integer 15 by the integer 100 and getting less than zero, a fractional part for a result. And you know an integer cannot hold fractional parts. So (15/100) = 0 when dealing with integers. So just change those two literals to floats like so:

    tip = (15.0f/100.0f)*total; .... or you could just do this:
    tip = (15.0f/100)*total; .....and that would work just as well. Or you could:
    tip = (15.0/100)*total; ...... but you would get that warning about the double having to be converted into a float to fit within "tip" which isn't good practice (but in this case works just fine).

    And just so you know (and maybe you already do : ) but you are using magic numbers in your code. It's not good practice to use "magic numbers", like what is (6.75/100) supposed to do? And why? Also ---> (15/100) what's this? What magic does that quantity perform? And how? It would be better to do it this way:

    #include <iostream>
    using namespace std;
    int main()
        float meal, tax, tip, total;
        float TaxRate = (6.75f/100);
        float TipRate = (15.0f/100); 
        meal = 445;
        tax = TaxRate * meal;      easier to understand.
        total = meal+tax;
        tip = TipRate * total;     easier to understand.
        cout<<"Total meal cost is " <<total <<endl;
        cout<<"Tax is " <<tax <<endl;
        cout<<"Tip is " <<tip;

    Another advantage to doing it this way is that if the tip rate or the tax rate should change later, and you used those "magic numbers" all over the place (if your program was much larger) then you would have to go throughout your whole program and track down each time you used those magic numbers to change them. But if you used variables instead, all you would have to do is update the variables "TaxRate" and "TipRate" once and the rest of your program would be all set, rather than looking throughout your whole program to find and update all of those magic numbers that you used all over the place!

    It would also be good to make those variables constant, like so:

    const float TaxRate = (6.75f/100);
    const float TipRate = (15.0f/100);

My Information

Member Title:
D.I.C Head
Age Unknown
Birthday Unknown

Contact Information

Click here to e-mail me



Mrk has no profile comments yet. Why not say hello?