Jump to content
xisto Community
Sign in to follow this  
seanooi

Need Help On C++ Its my first assignment

Recommended Posts

well guys, I'm very new to C++, and I've just received my new assignment today but don't know what does it mean, so I was hoping that you could help me out with it.In my assignment, I'm required to write a program that does the following.- It first lists each of the basic data types, stating the number of bits they use, their min and max values, and for floating point types, their min and max exponent values.- Then have the program prompt the user to enter a number and reply stating what number the user entered. - Must be presented all output to the user in a nice, userfriendly manner.My lecturer said something about do not hard code it out at the end of the class.

Share this post


Link to post
Share on other sites

you can use the 'sizeof' command to determine the size of a variable. I'm not sure if it's in bits or bytes (probably bytes) but there are 8 bits for every byte. As for input and output, there's heaps of basic tutorials around that'll get you what you need. Here's a good place to start: http://www.cprogramming.com/tutorial/lesson1.html.

Good Luck! :(

Share this post


Link to post
Share on other sites

#include <float.h>
#include <limit.h>

this two header contains the values;


char CHAR_MIN, CHAR_MAX
int minimum = INT_MIN, maximum = INT_MAX
long LONG_MIN or LNG_MIN i forgot...
LONG_MAX or LNG_MAX
float FLT_MIN, FLT_MAX
double DBL_MIN, DBL_MAX


heres how to use this constants in c++ style

#include <limit.h>#include <iostream>void main(){cout << "INTEGER MINIMUM = " << INT_MIN << " INTEGER MAXIMUM = " << INT_MAX;}

this simple code will display the minimum and maximum value an integer can hold and don't be startled if long and int will have the same value :( since most implementation uses 4 bytes for long and 4 bytes for int. If you want the 2 bytes integer.. use the short


to prompt user use this code

int number;cout << "enter number: ";cin >> number;count << endl << "the number u entered is " << number;


i hope this helps.. if not :(

Share this post


Link to post
Share on other sites

i cam up with something like this:

#include <iostream>#include <climits>using namespace std;int main(){	int n_int = INT_MAX;	short n_short = SHRT_MAX;	long n_long = LONG_MAX;	cout << "int is " << sizeof (int) << "bytes. " << endl;	cout << "short is " << sizeof n_short << "bytes. " << endl;	cout << "long is " << sizeof n_long << "bytes. " << endl;	cout << "Maximum values:" << endl;	cout << "int: " << n_int << endl;	cout << "short: " << n_short << endl;	cout << "long: " << n_long << endl;	cout << "Minimum int value = " << INT_MIN << endl;	cout << "Bits per byte = " << CHAR_BIT << endl << endl;	int number;	cout << "Enter number: ";	cin >> number;	cout << endl << "The number u entered is " << number;	cin.get();	cin.get();	return 0;}

But I'm not sure if it complies with what my lecturer wants, so can someone please check it out for me? :(

Share this post


Link to post
Share on other sites

OK, this is what I got now, and i'm pretty sure that this is what the assignment is suppose to be like. Hope that someone will look it over for me and tell me if i'm doingmething wrong here :)
This is just the code for listing the data types.

#include <iostream>#include <climits>#include <cfloat>using namespace std;int main(){	cout << "bool\t" << sizeof(bool)*8 << "\t" << 0 << "\t" << 1 << endl;	cout << "char\t" << sizeof(char)*8 << "\t" << CHAR_MIN << "\t" << CHAR_MAX << endl;	cout << "short\t" << sizeof(short)*8 << "\t" << SHRT_MIN << "\t" << SHRT_MAX << endl;	cout << "int\t" << sizeof(int)*8 << "\t" << INT_MIN << "\t" << INT_MAX << endl;	cout << "long\t" << sizeof(long)*8 << "\t" << LONG_MIN << "\t" << LONG_MAX << endl;	cout << "float\t" << sizeof(float)*8 << "\t" << FLT_MIN << "\t" << FLT_MAX << endl;	cout << "double\t" << sizeof(double)*8 << "\t" << DBL_MIN << "\t" << DBL_MAX << endl;	cout << "double long\t" << sizeof(double long)*8 << "\t" << LDBL_MIN << "\t" << LDBL_MAX << endl;	cin.get();	cin.get();	return 0;}

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  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.