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(int);
int puttdistance(int);
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 << "Disappointing. You are "<< x-5 << " over par." ;
else
cout << "Congratulations. You are "<< 5 - x << " under par." ;
}
cout << endl << endl;
system ("pause");
return 0;
}
int shotdistance(int 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(int 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......