Thread: help please

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    8

    help please

    Hi im kinda new to C++ and im trying to write a program for a challenge site (osix.net), but i seem to be running into problems with my loop which wont even seem to run once...

    Code:
    #include <iostream>
    
    using namespace std;
    
    double Perm = 3;
    int TimeTotal = 94;
    int TimePass = 0;
    
    int main()
    {
        while (TimePass < TimeTotal)
        {
            Perm = Perm * (1+1/3);
            TimePass++;
        }
    
        cout << (int)Perm;
    }
    If i take out the line TimePass++; the loop works but doesn't end......
    any help is appreciated!
    Last edited by comwizz2; 08-02-2007 at 12:07 PM. Reason: added info

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Code:
    Perm = Perm++;
    We just had a discussion about this in the C forums. This is an undefined statement in C and in C++. The compiler may make this line of code do anything it wants.

    Are you just trying to increment Perm by one? Then do this:

    Code:
    Perm++;
    And for the future, turn your compiler's warnings up. It should have caught this.

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    8
    Thnks i used to use c# for a while and that worked, but still my loop doesn't loop... doesn't even seem to run

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Your loop is running 94 times as you want. The problem is that you are doing integer division and so you are multiplying by 1 every time, which doesn't change the value. The literals 1 and 3 are integers, so 1/3 is 0. To do floating point arithmetic, make them doubles by adding a decimal point. 1.0/3.0 is 0.33333.

  5. #5
    Registered User
    Join Date
    Aug 2007
    Posts
    8
    Thanks

    only problem is it spits out -2147483648!!!

  6. #6
    Registered User
    Join Date
    Aug 2007
    Posts
    8
    I cannot get it to spit out a whole number all i get is scientific notation if i dont use int cast, but if i do use int cast, it gives me negative number

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The number is too big to fit into an int, so it becomes negative. There are ways to not show scientific notation when displaying a double, look into the io manipulators from <iomanip>.

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by Daved
    There are ways to not show scientific notation when displaying a double, look into the io manipulators from <iomanip>.
    Yeah, something like this I think if memory serves:
    Code:
    #include <iomanip>
    
    ...
    
    cout << fixed << Perm;
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  9. #9
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by comwizz2 View Post
    I cannot get it to spit out a whole number all i get is scientific notation if i dont use int cast, but if i do use int cast, it gives me negative number
    Code:
    std::cout << std::fixed << Perm << std::endl;

  10. #10
    Registered User
    Join Date
    Aug 2007
    Posts
    8
    Thanks everybody! after that final fix my program ran sucsessfully and i got the correct answer thanks very much

Popular pages Recent additions subscribe to a feed