Variable Types

Another exercise I'm confused about in this C++ book I'm reading..

What would be the correct variable type in which to store the following
information?
a. Your age
b. The area of your backyard
c. The number of stars in the galaxy
d. The average rainfall for the month of January


I decided to make a little program for this instead of directly answering the questions to get a little more practice in. Code can be seen here...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

int main()
{
	unsigned short int myAge = 20;
	float backyardArea = 1800.5;
	__int64 starsInGalaxy = 200000000000;
	double avgRainfallJanuary = 4.46;

	cout << "My age is:\t\t\t\t" << myAge << endl;
	cout << "Area of my backyard is:\t\t\t" << backyardArea << " Sq. Feet" << endl;
	cout << "Stars in Milky Way (est):\t\t" << starsInGalaxy << endl;
	cout << "Average January Rainfall (local):\t" << avgRainfallJanuary << endl;
return 0;
}


Using that code I got no errors or warnings and it spit out the exact data it was supposed to. Now according to this book, the answers were supposed to be..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

int main()
{
	unsigned short int myAge = 20;
	unsigned float backyardArea = 1800.5;
	unsigned double starsInGalaxy = 200000000000;
	unsigned short int avgRainfallJanuary = 4.46;

	cout << "My age is:\t\t\t\t" << myAge << endl;
	cout << "Area of my backyard is:\t\t\t" << backyardArea << " Sq. Feet" << endl;
	cout << "Stars in Milky Way (est):\t\t" << starsInGalaxy << endl;
	cout << "Average January Rainfall (local):\t" << avgRainfallJanuary << endl;
return 0;
}


That gives me the following warnings...
1
2
3
4
5
6
7
8
9
10
11
12
13
1
1>Compiling...
1>main.cpp
1>c:\users\admin\documents\cpp\study\exercise 3-7\exercise 3-7\main.cpp(25) : warning C4076: 'unsigned' : can not be used with type 'float'
1>c:\users\admin\documents\cpp\study\exercise 3-7\exercise 3-7\main.cpp(25) : warning C4244: 'initializing' : conversion from 'double' to 'unsigned int', possible loss of data
1>c:\users\admin\documents\cpp\study\exercise 3-7\exercise 3-7\main.cpp(26) : warning C4076: 'unsigned' : can not be used with type 'double'
1>c:\users\admin\documents\cpp\study\exercise 3-7\exercise 3-7\main.cpp(26) : warning C4305: 'initializing' : truncation from '__int64' to 'unsigned int'
1>c:\users\admin\documents\cpp\study\exercise 3-7\exercise 3-7\main.cpp(26) : warning C4309: 'initializing' : truncation of constant value
1>c:\users\admin\documents\cpp\study\exercise 3-7\exercise 3-7\main.cpp(27) : warning C4244: 'initializing' : conversion from 'double' to 'unsigned short', possible loss of data
1>Linking...
1>Embedding manifest...
1>Build log was saved at "file://c:\Users\admin\Documents\CPP\Study\Exercise 3-7\Exercise 3-7\Debug\BuildLog.htm"
1>Exercise 3-7 - 0 error(s), 6 warning(s)
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========


That gives me the following output..
1
2
3
4
My age is: 20
Area of my backyard is: 1800 Sq. Feet //Lost decimal using this type
Stars in Milky Way (est): 2431504384 //Should be 2B not sure how this happened
Average January Rainfall (local): 4 //Lost decimal once again 


Sorry if I'm asking really noob questions, but I want to make sure I'm learning this right and figure out how and why this book is telling me to do these things. The only thing I can guess is that he assumed a given area would be a whole number, average rainfall would be whole number, and stars in a galaxy would be a lower number than 2 Billion. Also keep in mind that this is "chapter in review" and he never even went over __int64 so I originally tried to use long (the types that he DID give, none of them went as high as 2B) and I got a warning saying truncation from __int64 to long, so I simply changed it to __int64 to avoid the warning.

Thoughts/Ideas? Am I just thinking too much here or did the book make a mistake?Thanks!
The output doesn't match the types given by the book, i.e. float for the backyard shouldn't result in any truncation.
But in any case, your choices for the data types are appropriate.
Thanks!
unsigned short int avgRainfallJanuary = 4.46;
This is the only part that's really wrong here- either the value (average as an integer, maybe in another unit than the one you are using), or the datatype (then you need a floating point number again). Well, except for the unsigned in front of the floats - floats and doubles can not be unsigned, so maybe your compiler just decides that since doubles and floats can't be unsigned, you must have meant to write int.
Last edited on
Topic archived. No new replies allowed.