Thread: What's wrong in this line of code?

  1. #1
    Registered User marCplusplus's Avatar
    Join Date
    Nov 2001
    Posts
    68

    Post What's wrong in this line of code?

    Hi,

    I created a small Class that gets 1 time (format: 00:00) and minuses it from another time (same format).

    This is the member function I had:

    void sub(airtime t1, airtime t2)
    {
    int min1 = t1.hours * 60 + t1.minutes;
    int min2 = t2.hours * 60 + t2.minutes;
    int answer = min1 - min2;
    hours = answer / 60;
    minutes = answer - hours * 60;
    }

    At first, when I was entering certain numbers, it was crashing ( continuosly looping).

    Then I changed:

    int min1 = t1.hours * 60 + t1.minutes;
    int min2 = t2.hours * 60 + t2.minutes;

    to:

    int min1 = (t1.hours * 60) + t1.minutes;
    int min2 = (t2.hours * 60) + t2.minutes;

    It worked perfectly when I edited the above code.

    However, I still do not know what change it might have made.
    I thought that in C++, the * is automatically worked before the +, so why did I need those brackets?

    Thanks!
    Marc

    No matter how much you know, you NEVER know enough.

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    70
    Post more code.

  3. #3
    Registered User marCplusplus's Avatar
    Join Date
    Nov 2001
    Posts
    68
    #include <iostream.h>
    #include <iomanip.h>
    class airtime
    {
    private:
    int hours;
    int minutes;
    public:
    void set()
    {
    char dummy;
    for( ; ; )
    {
    cout<<"Enter Time: (Format: 00:00)\t";
    cin>>hours>>dummy>>minutes;
    if ( hours <= 23 && minutes <= 59)
    break;
    else
    cout<<"\nThe format you entered is not correct.\n\n";
    };
    }
    void display()
    {
    cout<<hours<<":"<<setfill('0')<<setw(2)<<minutes;
    }
    void sub(airtime t1, airtime t2)
    {
    int min1 = (t1.hours * 60) + t1.minutes;
    int min2 = (t2.hours * 60) + t2.minutes;
    int answer = min1 - min2;
    hours = answer / 60;
    minutes = answer - hours * 60;
    }
    };
    void main()
    {
    char choice;
    airtime t1, t2, t3;
    do
    {
    cout<<"Setting T1....\n";
    t1.set();
    cout<<"Setting T2....\n";
    t2.set();
    cout<<"T1 minus T2....\n";
    cout<<"\nAnswer....";
    t3.sub(t1,t2);
    t3.display();
    cout<<"\n\nWould you like to try again? y\\n?";
    cin>>choice;
    cout<<"\n";
    }while(choice != 'n');
    }
    No matter how much you know, you NEVER know enough.

  4. #4
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    I just compiled your code, (both versions), and neither crashed with valid input. (MSVC++ 6.0).

    As an observation...

    >>> if ( hours <= 23 && minutes <= 59)

    ...your range checking is flawed, remember -1 is a perfectly valid integer...
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    70
    >>>
    void set()
    {
    char dummy;
    for( ; ; )
    {

    >>>>

    First logical error:

    No need to use infinite loop here, because it breaks after you enter first hour, minute values within bounds.

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    70
    Sorry, it is not necessarily logical error.

    I don't see any error in your program.

  7. #7
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    >>> I don't see any error in your program.

    I'm playing with it, (actually got to go now, but I was...), compile it, (either version), and when it asks for the first time enter "d" then press return!
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  8. #8
    Registered User marCplusplus's Avatar
    Join Date
    Nov 2001
    Posts
    68
    adrianxw: try running the old code and enter: 12:06 as the first value and 3:56 and the second. It will crash

    golem: I did that loop so that it checks that the time entered is correct. If not, it will keep looping until the user finally enters the correct input. Only then will it work well.

    I don't know what was wrong. I didn't think those brackets were neccessary. But I guess they are

    Marc
    Last edited by marCplusplus; 12-12-2001 at 08:47 AM.
    No matter how much you know, you NEVER know enough.

  9. #9
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    Tried. Both versions give me 8:10. You've got problems, but I don't think they are where you think they are!
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  10. #10
    Registered User
    Join Date
    Nov 2001
    Posts
    53
    Works fine for me too..... both versions do. * is before +

    Brackets
    Of
    Division
    Multiplication
    Addition
    Subtraction

    (i think that's right...)

    I believe this applies to everything.... C++ included

    Almosthere
    "Technology has merely provided us with a more efficient means for going backwards" - Aldous Huxley

  11. #11
    Registered User
    Join Date
    Dec 2001
    Posts
    70
    >> I did that loop so that it checks that ....

    Sorry again, I was confused by things out of this debate.

    >> I didn't think those brackets were neccessary. But I guess they are.

    No way, multiplication has precedence before addition. No brackets are needed.

  12. #12
    Registered User marCplusplus's Avatar
    Join Date
    Nov 2001
    Posts
    68
    hmph..then i guess only I had the problems...
    hmphh..

    Marc
    No matter how much you know, you NEVER know enough.

  13. #13
    Registered User
    Join Date
    Dec 2001
    Posts
    70
    Try this, if it helps.

    #include <stdlib.h>

    void sub (airtime t1, airtime t2)
    {
    int min1 = (t1.hours * 60) + t1.minutes;
    int min2 = (t2.hours * 60) + t2.minutes;
    int answer = labs (min1 - min2);
    hours = answer / 60;
    minutes = answer % 60;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What's wrong with this code?
    By Finchie_88 in forum Networking/Device Communication
    Replies: 10
    Last Post: 05-27-2005, 09:46 AM
  2. What's wrong with my Win32 Wrapper code?
    By miica in forum Windows Programming
    Replies: 9
    Last Post: 02-22-2005, 08:55 PM
  3. What's wrong with this code?
    By ylzhang in forum C Programming
    Replies: 3
    Last Post: 06-20-2003, 07:09 AM
  4. What's wrong with this code?
    By Tride in forum C Programming
    Replies: 5
    Last Post: 05-26-2003, 12:40 PM
  5. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM