HOME       >>       Programming

Need Help On C++ Its my first assignment


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.


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!


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

this two header contains the values;

int minimum = INT_MIN, maximum = INT_MAX
long LONG_MIN or LNG_MIN i forgot...

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


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?


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


Xisto.com offers Free Web Hosting to its Members for their participation in this Community. We moderate all content posted here but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use, cookie and privacy policy. Copyright 2001-2019 by Xisto Corporation. All Rights Reserved.