473,425 Members | 1,829 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,425 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 2346
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();
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.