Like Tree2Likes
  • 1 Post By QuantumPete
  • 1 Post By whiteflags

Need help

This is a discussion on Need help within the C++ Programming forums, part of the General Programming Boards category; Hey guys, so I'm a beginner in C++ and I need some help. I'm using a website called hypergrade to ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    16

    Need help

    Hey guys, so I'm a beginner in C++ and I need some help. I'm using a website called hypergrade to turn in my programs, and one of my assignments was to create a program regarding plants, where it asks the user to input data such as weight, and how fast he/she would like to travel to the destined planet and it would output the result. My program compiled and it runs fine on microsoft visual 2010 express, however, when turning in my assignment on hypergrade, an error comes up:


    E: empA1AB.tmpPlanets.cpp: In function 'int main()':E: empA1AB.tmpPlanets.cpp:100:20: warning: integer overflow in expressionI've asked my teacher for some help but knowing teachers, she never gave me a direct answer. All my variables are declared as a double, and here is my output result:cout << fixed << setprecision(2);
    cout << "INTERPLANATERY TRAVEL: EARTH TO " << planetName << endl;
    cout << "Your weight on " << planetName << ": " << weightNew << " lbs" << endl;
    cout << "Your travel time to " << planetName << endl;
    cout << "In hours: " << (travelHours) << endl;
    cout << "In days: " << (travelDays) << endl;
    cout << "In years: " << (travelYears) << endl;
    //system("PAUSE");
    return 0;

    The reason why the system(pause) is in comments is because hypergrade doesn't recognize it, but just ignore that. As a result, this is what I get:

    Picking D(for mars), entering 120 (weight) and 10 (miles per hour) i get in hours: 4800000
    in days: 200000
    in years: 547.95

    apparently these numbers are too big for hypergrade, my teacher told me to use a UL code, but I have no idea how to use that and where to put it. Some help is much appreciated, thanks in advance

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,627
    The "UL code" is a suffix applied to hard coded numbers. It would fix the problem you have basically because it changes the type of integer. Take any random number you use in an expression, 3,060,463,673,373 is too large for 32-bit long integers, but not for the unsigned 32-bit long integers.

    You also didn't show any of the actually relevant code. The compiler warns about the overflow on a specific line, and I expected to see some kind of math problem there.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,421
    Please include full source code (or at least the minimal amount of source that demonstrates the problem) and put them into code tags, now and in the future.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    16
    How would I put the UL code? Do I just put like (UL) next to the end result? here is the code:

    Code:
    int main()
    {
    double weight;
    double weightNew;
    double speed;
    double distanceSun;
    double surfaceGravity;
    double travelDistance;
    double distanceEarthSun = 93*1000000;
    double travelHours;
    double travelDays;
    double travelYears;
    string planetName;
    char selection;
    
    
    cout << "INTERPLANATERY TRAVEL MENU" << endl;
    cout << "---------------------------" << endl;
    cout << "a) Mercury" << endl;
    cout << "b) Venus " << endl;
    cout << "c) Earth" << endl;
    cout << "d) Mars" << endl;
    cout << "e) Jupitar" << endl;
    cout << "f) Saturn" << endl;
    cout << "g) Uranus" << endl;
    cout << "h) Neptune" << endl;
    cout << "q) Quit" << endl;
    cout << "Select a planet to travel to or press q to quit the program" << endl;
    cin >> selection;
    if(selection>='a' && selection<='h')
    {
    cout <<"Input your weight followed by how fast you wish to travel at (miles per hour)"<< endl;
    cin >> weight >> speed;
    }
    if(selection == 'a')
    {
    planetName = "Mercury";
    distanceSun = 36*1000000;
    surfaceGravity = 0.27;
    }
    else if(selection == 'b')
    {
    planetName = "Venus";
    distanceSun = 67*1000000;
    surfaceGravity = 0.86;
    }
    else if(selection == 'c')
    {
    planetName = "Earth";
    distanceSun = 93*1000000;
    surfaceGravity = 1.00;
    }
    else if(selection == 'd')
    {
    planetName = "Mars";
    distanceSun = 141*1000000;
    surfaceGravity = 0.37;
    }
    else if(selection == 'e')
    {
    planetName = "Jupitar";
    distanceSun = 483*1000000;
    surfaceGravity = 2.64;
    }
    else if(selection == 'f')
    {
    planetName = "Saturn";
    distanceSun = 886*1000000;
    surfaceGravity = 1.17;
    }
    else if(selection == 'g')
    {
    planetName = "Uranus";
    distanceSun = 1782*1000000;
    surfaceGravity = 0.92;
    }
    else if(selection == 'h')
    {
    planetName = "Neptune";
    distanceSun = 2793*1000000;
    surfaceGravity = 1.44;
    }
    else if(selection =='q')
    {
    cout << "You have decided to quit the program" << endl;
    //system("PAUSE");
    return 0;
    
    
    }
    else
    {
    cout <<"You have entered an invalid selection" << endl;
    //system("PAUSE");
    return 0;
    }
    weightNew = weight * surfaceGravity;
    
    
    if(selection == 'a' || selection == 'b')
    {
    travelDistance = distanceEarthSun - distanceSun;
    }
    else
    {
    travelDistance = distanceSun - distanceEarthSun;
    }
    {
    travelHours = travelDistance/speed;
    travelDays = travelHours/24.0;
    travelYears = travelDays/365.0;
    }
    cout << fixed << setprecision(2);
    cout << "INTERPLANATERY TRAVEL: EARTH TO " << planetName << endl;
    cout << "Your weight on " << planetName << ": " << weightNew << " lbs" << endl;
    cout << "Your travel time to " << planetName << endl;
    cout << "In hours: " << (travelHours) << endl;
    cout << "In days: " << (travelDays) << endl;
    cout << "In years: " << (travelYears) << endl;
    //system("PAUSE");
    return 0;
    
    
    }

  5. #5
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,648

  6. #6
    Registered User
    Join Date
    Mar 2012
    Posts
    16
    Yeah I don't get how you input the suffixes

  7. #7
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by arsenalftw067 View Post
    Yeah I don't get how you input the suffixes
    It's all there in the link that rags_to_riches posted.
    Also, you should indent your code, it'll make it 100x more readable!
    Elysia likes this.
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    It might be better just to make all these into double expressions to begin with.

    distanceSun = 36.0*1000000;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    2,289
    Quote Originally Posted by whiteflags View Post
    3,060,463,673,373 is too large for 32-bit long integers, but not for the unsigned 32-bit long integers.
    this is not correct. for a 32-bit number to allow that 3 trillion figure, it would have to have 3 states per bit (1.853E+15). a 64-bit integer would be more appropriate here, if you want to use only integral types, but like salem says, doubles would be the most appropriate.

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,421
    To be precise, it takes at least 42 bits to represent such a high number ^_^
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,627
    Quote Originally Posted by Elysia View Post
    To be precise, it takes at least 42 bits to represent such a high number ^_^
    Quote Originally Posted by Elkvis View Post
    this is not correct. for a 32-bit number to allow that 3 trillion figure, it would have to have 3 states per bit (1.853E+15). a 64-bit integer would be more appropriate here, if you want to use only integral types, but like salem says, doubles would be the most appropriate.
    I meant billions instead of trillions, but this kind of response is what I get for pulling numbers out of my ass.

    I can't help but think bigger than the rest of you
    Elkvis likes this.

  12. #12
    Registered User
    Join Date
    Oct 2006
    Posts
    2,289
    Quote Originally Posted by Elysia View Post
    To be precise, it takes at least 42 bits to represent such a high number ^_^
    42: the answer to life, the universe, and everything.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21