Public Group

Data Type Value Memorization Project help.

This topic is 3968 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

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 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 on other sites
Quote:
 Original post by HazardxIt 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 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 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 on other sites
So, basically what i am memorizing is un-neccessary?

Share on other sites
Quote:
 Original post by HazardxSo, 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 on other sites
Ok i understand thanks but what are these bits you guys been talking about. Im curious.

Share on other sites
Quote:
 Original post by HazardxOk i understand thanks but what are these bits you guys been talking about. Im curious.

Bit

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5

• 14
• 12
• 9
• 12
• 37
• Forum Statistics

• Total Topics
631428
• Total Posts
3000027
×