Program check - help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zelmila19
    New Member
    • Apr 2008
    • 16

    Program check - help

    Hi anyone,
    Could someone check this program for me, I've given it a go, it runs well but gives me 6 of this warning:

    warning C4244: 'initializing' : conversion from 'double' to 'int', possible loss of data

    Please check this and let me know if I've left out anything or any additions, anything:


    #include <iomanip>
    #include <iostream>
    #include <string>
    #include <time.h>

    using namespace std;

    int shotdistance(in t);
    int puttdistance(in t);

    int main()
    {
    int w = 280; //distance from the hole
    int x = 0; //the number of strokes
    int y = 0; //distance ball was hit
    char z;//club selection

    cout << "CP1200 GOLF GAME 2008 \n By <name>";
    cout << endl << "The hole is a par 5 and is 280 meters in length. \n You may use a driver, iron, or a putter.";
    cout << endl << "The average distance each club can hit is: \n Driver = 100m, Iron = 30m, Putter = 10m.";
    cout << endl << "START GAME" << endl<<endl;

    do
    {
    cout << "You are " << w << " metre/s from the hole, after " << x << " stroke/s." << endl;
    cout << "Please select: [ (D) river-100m, (I) ron-30m, (P) utter-10m ] :";

    cin >>z;

    if ((z =='D') || (z =='d')) // Driver
    {
    y = shotdistance( 100 );
    w = w - y;
    x++;} else
    if ((z =='I') || (z =='i')) // Iron
    {
    y = shotdistance( 30 );
    w = w - y;
    x++;} else
    if ((z == 'P') || (z == 'p')) // Putter
    {
    if (w < 10)
    {
    y = puttdistance( w );
    w = w - y;
    x++;
    } else
    {
    y = shotdistance( 10 );
    w = w - y;
    x++;}
    }else {
    cout << "Invalid club selection... Air Swing!";
    x++;}

    cout << endl << "You hit " << y << " metres ..."<< endl <<endl;

    if (w < 0)
    w = w * -1;

    } while (w != 0);

    if (w == 0)
    {
    cout << "Clunk... The ball is in the hole. ";
    if (x == 5)
    cout << "Well done. You have achieved par.";
    else
    if (x > 5)
    cout << "Disappoint ing. You are "<< x-5 << " over par." ;
    else
    cout << "Congratulation s. You are "<< 5 - x << " under par." ;

    }


    cout << endl << endl;

    system ("pause");
    return 0;
    }


    int shotdistance(in t a)
    {
    int min = a * .8, max = a * 1.2; //Defines the minimum and maximum range of the club.
    srand ( time(NULL) );
    a = min + (rand() % (max -min)) ; //calculates the random shot distance.

    return a; //returns the distance to main().
    }

    int puttdistance(in t a) //Putting distance if the distance to the hole is < 10m
    {
    int min = a * .8, max = a * 1.2; //Defines the minimum and maximum distance to the hole.
    srand ( time(NULL) );
    a = min + (rand() % (max -min)) ; //calculates the random putting distance.

    if (a == 0)
    a = 1;

    return a; //returns the putting distance to main().
    }


    Also I would like to know how i could format:

    cout << "CP1200 GOLF GAME 2008 \n By <name>";
    cout << endl << "The hole is a par 5 and is 280 meters in length. \n You may use a driver, iron, or a putter.";
    cout << endl << "The average distance each club can hit is: \n Driver = 100m, Iron = 30m, Putter = 10m.";
    cout << endl << "START GAME" << endl<<endl;


    so that
    CP1200 GOLF GAME 2008 By <name>
    and
    START GAME

    are kind of in the same alignment like center.......

    Thank you......
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Have a look at these lines:

    [code=c]
    int min = a * .8, max = a * 1.2;
    [/code]

    Here you multiply an int (a) by a double (0.8 or 1.2) so the result of the expression
    has type double. You assign that result to an int again. That's what the compiler
    whines about. To keep its mouth shut you should explicitly cast the type of the
    result (double) back to int again.

    kind regards,

    Jos

    Comment

    • zelmila19
      New Member
      • Apr 2008
      • 16

      #3
      Originally posted by JosAH
      Have a look at these lines:

      [code=c]
      int min = a * .8, max = a * 1.2;
      [/code]

      Here you multiply an int (a) by a double (0.8 or 1.2) so the result of the expression
      has type double. You assign that result to an int again. That's what the compiler
      whines about. To keep its mouth shut you should explicitly cast the type of the
      result (double) back to int again.

      kind regards,

      Jos
      How do i do that????

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by zelmila19
        How do i do that????
        That should be in your book somewhere:

        [code=c]
        double doubleValue= ...;
        int intValue= (int)doubleValu e; // a cast to the int type
        [/code]

        kind regards,

        Jos

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          And, of course, the possible loss of data is still there. All the typecast does is tell the compiler to shut up: that you really know what you are doing.

          Integers and floating point do not mix well. Stick to one family or the other.

          As yourself a question: Is this scientific where extreme accuracy is not required?

          If yes, use floating point. If no, use integers.

          Comment

          Working...