Sign in to follow this  
Hazardx

Data Type Value Memorization Project help.

Recommended Posts

Hello there, i am using Microsoft's Visual C++ as my compiler but im having trouble with my source code. I am making a Memorization Project that will help myself learn the values of data types such as int ect. It has greatly helped me and now i know the min and max value of int by heart. 2147483647 and -2147483648 Everything is fine but it seems i am unable to enter the value of unsigned int. I enter it correctly but it is outputed otherwise as "Sorry that is an invalid Input".
#include <iostream>
#include <climits>
using namespace std;

int main()
{
	int inputint = 0;
	unsigned int inputuint = 0;
	double inputdouble = 0; 
	float inputfloat = 0;

	cout<< "Memorization Test Project" << endl;

	cin>>inputint;
	cin>>inputuint;

	if (inputint == -2147483648)
	{
		cout<< "That is the correct input for the minimal value of int" <<endl;
	}

	else if (inputint == 2147483647)
	{
		cout<< "That is the correct input for the maximal value of int" <<endl;
	}

	else if (inputuint == 4294967295)
	{
		cout<< "That is the correct input for the maximal value of unsigned int" <<endl;
	}

	else
	{
		cout<< "Sorry that is an invalid input" <<endl;
	}
	return 0;
}

Share this post


Link to post
Share on other sites
Without addressing your actual question (sorry, I don't see the problem), I'll give you a bit of advice.

Don't focus on those sorts of specifics. Those values that you have memorized are not necessarily always true... the size of an int is implementation specific. On some systems, for example, an int will only be a 16-bit value.

What you should understand is why those values are what they are. For example, in VC++ ints are 32-bits, with a range of -(2^31) to (2^31-1), or -2147483648 to 2147483647. Why not 2^32? Because the first bit is a signed bit. Of course, the unsigned int doesn't need that bit, so it can go from 0 to 2^32-1.

It's good to know the sizes used by your compiler/system, but I wouldn't go out of my way to memorize the base-10 versions of them, unless you are just doing it as a programming exercise.

Share this post


Link to post
Share on other sites
Quote:
Original post by Hazardx
It has greatly helped me and now i know the min and max value of int by heart. 2147483647 and -2147483648


Not to be gratuitously evil and annoying, but the min and max values of int (in C++) are std::numeric_limits<int>::min() and std::numeric_limits<int>::max(), which may or may not be equal to the values you learned—on recent 64-bit platforms, the min and max values are usually the much larger 9223372036854775807 and -9223372036854775808.

Quote:
Everything is fine but it seems i am unable to enter the value of unsigned int. I enter it correctly but it is outputed otherwise as "Sorry that is an invalid Input".


I entered "1" followed by "4294967295" and it told me that I entered the correct maximal value, as expected... What input did you provide that caused your error?

Share this post


Link to post
Share on other sites
maybe because you are using the if statement in a wrong way, first you ask a user to input a signed integer. Then the unsigned. If the user enters the signed integer correctly then the first or second if will be true, and no more ifs will be tested (thus not even getting to testing whether the unsigned int was right). If the user simply hist enter when asked for a signed int, no value is input and the inputint stays at 0. The first if tests if the value is right it fails and immediately goes to else which displays the error string.

EDIT: you could try this

if (inputint == -2147483648)
{
cout<< "That is the correct input for the minimal value of int" <<endl;
}

else if (inputint == 2147483647)
{
cout<< "That is the correct input for the maximal value of int" <<endl;
}
else
{
cout<< "Sorry that is an invalid input" <<endl;
}

if (inputuint == 4294967295)
{
cout<< "That is the correct input for the maximal value of unsigned int" <<endl;
}
else
{
cout<< "Sorry that is an invalid input" <<endl;
}

Share this post


Link to post
Share on other sites
cin>>inputint;
cin.clear();
cin>>inputuint;


As for logic, you have more than just boolean condition (true_min, true_max, invalid). As such, you're best off handling each type on its own.


std::cin >> inputint;
std::cin.clear();
std::cin >> inputuint;

switch (inputint) {
case -2147483648 : std::cout << "Valid max" << std::endl; break;
case 2147483647 : std::cout << "Valid min" << std::endl; break;
default : std::cout << "Invalid signed int" << std::endl;
}
switch (inputuint ) {
case 4294967295 : std::cout << "Valid unsigned max" << std::endl; break;
default : std::cout << "Invalid unsigned int" << std::endl;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Hazardx
So, basically what i am memorizing is un-neccessary?


You'll eventually memorize these things from having to use them.

Memorizing too many specifics is also of limited value since what you'll memorize will only be true for x86 32-bit architectures.

It is probably more helpful to know the minimum sizes of types according to the C, C++ standards. For example that an int is defined to be at least 16-bits and a fixed point number.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this