Data Type Value Memorization Project help.

Started by
7 comments, last by fpsgamer 16 years, 8 months ago
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;
}
I'm a novice C++ programmer, wanting to succeed and learn. If you ever want to be my friend or just talk add me.MSN IM: hazardx_anime@hotmail.com
Advertisement
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.
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?

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;	}

You didn't come into this world. You came out of it, like a wave from the ocean. You are not a stranger here. -Alan Watts

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;}
So, basically what i am memorizing is un-neccessary?
I'm a novice C++ programmer, wanting to succeed and learn. If you ever want to be my friend or just talk add me.MSN IM: hazardx_anime@hotmail.com
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.
Ok i understand thanks but what are these bits you guys been talking about. Im curious.
I'm a novice C++ programmer, wanting to succeed and learn. If you ever want to be my friend or just talk add me.MSN IM: hazardx_anime@hotmail.com
Quote:Original post by Hazardx
Ok i understand thanks but what are these bits you guys been talking about. Im curious.


Bit

This topic is closed to new replies.

Advertisement