473,413 Members | 1,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

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(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......
Apr 26 '08 #1
4 2345
JosAH
11,448 Expert 8TB
Have a look at these lines:

Expand|Select|Wrap|Line Numbers
  1. int min = a * .8, max = a * 1.2; 
  2.  
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
Apr 26 '08 #2
Have a look at these lines:

Expand|Select|Wrap|Line Numbers
  1. int min = a * .8, max = a * 1.2; 
  2.  
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????
Apr 26 '08 #3
JosAH
11,448 Expert 8TB
How do i do that????
That should be in your book somewhere:

Expand|Select|Wrap|Line Numbers
  1. double doubleValue= ...;
  2. int intValue= (int)doubleValue; // a cast to the int type
  3.  
kind regards,

Jos
Apr 26 '08 #4
weaknessforcats
9,208 Expert Mod 8TB
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.
Apr 26 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: GujuBoy | last post by:
i want to check to see if a certain program is installed on my windows box using python. how can i do that...(ie, i want to see if "word" is installed) please help
6
by: hoover_richard | last post by:
I am a newbie to C++ and I need help with a simple program I am trying to write. My program is designed to print all of the odd integers contained in an array and output the sum of the odd...
2
by: atreide_son | last post by:
hello all... yes i'm a newbie and i need help. i have an assignment due for class, but I don't know i'm stuck and can't get out from under myself. here's the focus of the program: Write...
10
by: Vince | last post by:
The following is code for the classic tortoise and hare C assignment. I am posting this for educational purposes only. Please do not plagiarize as comp sci instructors regularly read newsgroups....
7
by: tyler_durden | last post by:
thanks a lot for all your help..I'm really appreciated... with all the help I've been getting in forums I've been able to continue my program and it's almost done, but I'm having a big problem that...
66
by: genestarwing | last post by:
QUESTION: Write a program that opens and read a text file and records how many times each word occurs in the file. Use a binary search tree modified to store both a word and the number of times it...
1
by: Unebrion | last post by:
Alright im working on a program that prints out user imput in a frame, along with a barcode.. it is like the front of an envelope. Here is the description for the program. This...
4
by: Ram | last post by:
Hi All, Firstly i am a newbie and trying to learn C. The background of the problem is Program: Presently I am working on a program of numerology and the I/P will be the name and output...
6
by: priyajohal | last post by:
#include<fstream.h> #include<process.h> #include<stdlib.h> #include<conio.h> #include<string.h> #include<dos.h> #include<ctype.h> #include<stdio.h> void setup() void help();
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.