C++ How to let the computer know that 0100 is 100?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • clairelee0322
    New Member
    • Jan 2008
    • 13

    C++ How to let the computer know that 0100 is 100?

    As a beginner, I am working on a project that calculates the telephone bill. Thank you all, my program is great.. but I got another problem because my teacher was trying to trick me with this..
    that he enter 0100, he didn't enter 100 so the program's calculation is totally wrong.

    I have tried to use if statements, it works when entering 0100 but it does not work when entering 1000, it got some wrong calculation.. Everytime I enter 0 before the time (like 0800) the program doesn't work properly.. I don't know why!!

    Can you guys help me out with this? I am so confused.


    [CODE=cpp]#include<iostre am.h>
    #include<iomani p.h>

    int main()
    {
    int start_time;
    int length;
    int end_time;
    double gross_cost = length * 0.25;
    double length_discount ;
    double total_discounts ;
    double tax;
    double net_cost;
    double time_discount;
    int length_min, length_hour;
    int end_min, end_hour;
    int call_hour, call_min;
    int i;

    //Any call started at or after 7:00 P.M. (1900 hours) but before 8:00 A.M. (0800 hours) is discounted 60 percent.
    cout << "What was the start time of your phone call? Write as 1900 instead of 7:00 PM\n";
    cin >> start_time;

    //above is the question asking for the time, I just can't enter 0 before the hour time (Like 0800), otherwise it gets problems..


    //Asking for the length.
    cout << "What was the length of your phone call in minutes?\n";
    cin >> length;

    gross_cost = length * 0.25;
    if (start_time >= 1900 || start_time <= 800)
    {
    time_discount = (gross_cost) * 0.6;
    }

    //make time look nice
    length_min = length % 60;
    length_hour = length / 60;

    call_min = start_time % 100;
    call_hour = start_time / 100;

    end_hour = call_hour + length_hour;
    end_min = call_min + length_min;


    if (end_min >= 60)

    {

    end_hour++;

    }

    //end_time = (call_hour + length_hour) * (100) + end_min;

    end_time = (end_hour) * (100) + end_min;


    //Any call at least 20 minutes long receives a 30 percent discount on its cost (after any other discount is subtracted).

    gross_cost = length * 0.25;

    if(length >=20)
    {
    length_discount = (gross_cost - time_discount) * 0.3;
    }


    // if time doesn't apply
    else
    {
    length_discount = 0.3 * gross_cost;
    }


    //calculations
    total_discounts = time_discount + length_discount ;
    gross_cost = length * 0.25;
    tax = (gross_cost - total_discounts ) * 0.08;
    net_cost = gross_cost + tax - total_discounts ;


    // Print the result.
    cout << "Start Time: " << start_time << '\n';
    cout << "End Time: " << end_time << '\n';
    cout << "Length: " << length_hour << ":" << length_min << '\n';
    cout.setf(ios:: fixed);
    cout << "Gross Cost: " << "$" << setprecision(2) << length * 0.25 << '\n';
    cout << "Time Discount: " << setprecision(2) << "$" << time_discount << '\n';
    cout << "Length Discount: " << "$" << length_discount << '\n';
    total_discounts = time_discount + length_discount ;
    cout << "Total Discounts: " << "$" << total_discounts << '\n';

    tax = (gross_cost - total_discounts ) * 0.08;
    cout.setf(ios:: fixed);
    cout << "Tax: " << setprecision(2) << "$" << tax << '\n';
    net_cost = (gross_cost + tax) - (total_discount s);
    cout.setf(ios:: fixed);
    cout << "Net Cost: " << setprecision(2) << "$" << net_cost << '\n';

    return 0;
    }[/CODE]
    Last edited by Ganon11; Jan 17 '08, 09:28 PM. Reason: Please use the [CODE] tags provided.
  • RedSon
    Recognized Expert Expert
    • Jan 2007
    • 4980

    #2
    If you put a debug break near line 23 what to start_time have as it's value, when you enter 0800?

    Comment

    • Laharl
      Recognized Expert Contributor
      • Sep 2007
      • 849

      #3
      0100, or any other number with a leading zero, is interpreted as an octal number rather than a number in base 10. Frankly, if your professor is doing that and hasn't told you about octal, he's being a bit of a jerk.

      Comment

      • RedSon
        Recognized Expert Expert
        • Jan 2007
        • 4980

        #4
        Originally posted by Laharl
        0100, or any other number with a leading zero, is interpreted as an octal number rather than a number in base 10. Frankly, if your professor is doing that and hasn't told you about octal, he's being a bit of a jerk.
        Oh, good call. I forgot about that.
        Last edited by RedSon; Jan 17 '08, 11:09 PM. Reason: Spelling

        Comment

        • RedSon
          Recognized Expert Expert
          • Jan 2007
          • 4980

          #5

          Comment

          • clairelee0322
            New Member
            • Jan 2008
            • 13

            #6
            Originally posted by Laharl
            0100, or any other number with a leading zero, is interpreted as an octal number rather than a number in base 10. Frankly, if your professor is doing that and hasn't told you about octal, he's being a bit of a jerk.


            yeah... He didn't even mention that stuff... he doesn't really teach us...

            I can't understand any of those stuff because I am in the class of "computer programming C++"...

            thanks anyways, I will try to use the if statements....

            Comment

            • NeXIncarno
              New Member
              • Jan 2008
              • 3

              #7
              Hi, was just flicking around try... maybe (i use c# i think thats about right, seems to work for me in vs08c#, it knocks off the leading 0 of the number)

              You should also put in an 'if number is longer than 4 or less than 3 then exit'

              string myinput;
              int start_time;

              cin >> myinput;

              if (!System::Int:: TryParse(myinpu t, start_time)) {
              //isnt a integer
              }


              or use regular expressions to catch him as he enters it. I found this on dotnet.org you'll need to include system.text.reg ularexpressions

              ----

              Andreas Blixt said:


              123..4 (Not a number)

              12Hello (Not a number)

              -12.34 (Number)

              0123 (Number; octal, not decimal)

              Here's a regular expression that only matches valid decimal numbers:

              ^-?(?:0|[1-9][0-9]*)(?:\.[0-9]+)?$

              You would use it like so:

              public static bool IsNumeric(strin g value) {

              return Regex.IsMatch(v alue, @"^-?(?:0|[1-9][0-9]*)(?:\.[0-9]+)?$");

              }

              ------

              hope theres something in there that will help

              -Rob

              Comment

              • RedSon
                Recognized Expert Expert
                • Jan 2007
                • 4980

                #8
                Originally posted by NeXIncarno
                Hi, was just flicking around try... maybe (i use c# i think thats about right, seems to work for me in vs08c#, it knocks off the leading 0 of the number)

                You should also put in an 'if number is longer than 4 or less than 3 then exit'

                string myinput;
                int start_time;

                cin >> myinput;

                if (!System::Int:: TryParse(myinpu t, start_time)) {
                //isnt a integer
                }


                or use regular expressions to catch him as he enters it. I found this on dotnet.org you'll need to include system.text.reg ularexpressions

                ----

                Andreas Blixt said:


                123..4 (Not a number)

                12Hello (Not a number)

                -12.34 (Number)

                0123 (Number; octal, not decimal)

                Here's a regular expression that only matches valid decimal numbers:

                ^-?(?:0|[1-9][0-9]*)(?:\.[0-9]+)?$

                You would use it like so:

                public static bool IsNumeric(strin g value) {

                return Regex.IsMatch(v alue, @"^-?(?:0|[1-9][0-9]*)(?:\.[0-9]+)?$");

                }

                ------

                hope theres something in there that will help

                -Rob
                Using C# to solve a problem in C is like drinking a gallon of soda to paint a picture. One doesn't have anything to do with the other. C# would be useful for illustrating a proof of concept, but telling an OP to use REGEX when they don't even understand what Octal is, isn't going to help.

                And, use CODE tags around your code:
                &#91;CODE]..code goes here..[/CODE]

                Comment

                • RedSon
                  Recognized Expert Expert
                  • Jan 2007
                  • 4980

                  #9
                  Originally posted by clairelee0322
                  I can't understand any of those stuff because I am in the class of "computer programming C++"...
                  What's that supposed to mean? You can't understand an article about octal based numbers with c/c++ examples because you are in a class about computer programming in C++? That doesn't make any sense. It's like your saying "I can't understand an example about how to saw some wood because I am in a class about how to saw wood".

                  Comment

                  • NeXIncarno
                    New Member
                    • Jan 2008
                    • 3

                    #10
                    I only mentioned C# because I tested what the result would be from the include system..... as this is what I have access to at work and I’m not completely comfortable with the C++ notation as its a good 3 years since I used it. The actual solution is in what I thought the correct syntax was, and the C# regex was just another way that they might want to look into using.

                    I don’t believe they have no clue what they are doing, they just need pointing in the right direction so I provided a few examples of what to look for, I don’t believe any solution should be provided as a single option as there are many different ways of doing everything, both with pluses and minuses.

                    I do think that not knowing the bases of different number types is a major concern if you are on a computing course, but when you first start learning a language I know from learning in my first year of uni on my c programming course there was nothing on converting between bases, (Admittedly I did do it at A’ Level). I am assuming they are trying to teach methodology, and then demonstrate the underlying problems that can occur, giving extra marks for solving and understanding these.

                    As for using c# to solve a c++ problem, I’m using the System namespace, which I assumed would be transparent between languages in this case. So in this given example if you passed the same string input you'd get the same output, so these two examples would give the same result (taken from MSDN).

                    Code:
                    C#
                    public static bool TryParse(
                        string s,
                        out int result
                    )
                    
                    Visual C++
                    public:
                    static bool TryParse(
                        String^ s, 
                        [OutAttribute] int% result
                    )
                    
                    J#
                    public static boolean TryParse(
                        String s,
                        /** @attribute OutAttribute */ /** @ref */int result
                    )
                    If I have made a bad assumption please correct me. Also sorry for the lack of code tags in my previous post, it was my first.

                    -Rob (Epic Newbie!)

                    Comment

                    • RedSon
                      Recognized Expert Expert
                      • Jan 2007
                      • 4980

                      #11
                      It's good that you are trying to help out a person by answering their questions. But it is usually considered poor form to answer the question using examples from a language that the asker is not familiar with. However, if the asker wants to do some very complicated stuff that can be accomplished using a higher level or similar programming language it might be a good idea to mention this to the asker. Usually people that have questions don't have the ability to change languages even if they wanted to.

                      I still find if very upsetting that you instructor is trying to trick you by entering in information that is completely bogus. In the courses that I have taken if the assignment does not explicitly ask me to verify input then as a matter of course I say in my pre-condition that the input is assumed to be valid, and of the form blah, blah, and blah. That way my instructor knows that if they start throwing garbage into the method, they are going to get garbage out.

                      Comment

                      Working...