Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Need Help On C++, It's my first assignment
seanooi
post Jan 19 2006, 07:01 AM
Post #1


Advanced Member
*******

Group: Members
Posts: 132
Joined: 22-November 05
Member No.: 14,730



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.
Go to the top of the page
 
+Quote Post
switch
post Jan 25 2006, 04:23 AM
Post #2


Premium Member
********

Group: Members
Posts: 178
Joined: 13-October 04
From: NSW, Australia
Member No.: 1,713



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! biggrin.gif
Go to the top of the page
 
+Quote Post
saga
post Jan 25 2006, 08:26 AM
Post #3


Premium Member
********

Group: Members
Posts: 165
Joined: 12-September 05
Member No.: 11,777



#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

CODE
#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 smile.gif 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

CODE
int number;

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



i hope this helps.. if not smile.gif
Go to the top of the page
 
+Quote Post
seanooi
post Jan 25 2006, 06:08 PM
Post #4


Advanced Member
*******

Group: Members
Posts: 132
Joined: 22-November 05
Member No.: 14,730



i cam up with something like this:

CODE

#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? biggrin.gif
Go to the top of the page
 
+Quote Post
seanooi
post Jan 29 2006, 07:35 AM
Post #5


Advanced Member
*******

Group: Members
Posts: 132
Joined: 22-November 05
Member No.: 14,730



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 biggrin.gif
This is just the code for listing the data types.

CODE

#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;
}
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. University Assignment Help(7)


 



- Lo-Fi Version Time is now: 30th August 2008 - 11:17 AM